matplotlib scatter plot colour as function of third variable

假如想象 提交于 2019-11-27 04:22:56

This works for me, using matplotlib 1.1:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
y = np.sin(x)

plt.scatter(x, y, marker='+', s=150, linewidths=4, c=y, cmap=plt.cm.coolwarm)
plt.show()

Result:

Alternatively, for n points, make an array of RGB color values with shape (n, 3), and assign it to the edgecolors keyword argument of scatter():

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 20, 100)
y = np.sin(x)
z = x + 20 * y

scaled_z = (z - z.min()) / z.ptp()
colors = plt.cm.coolwarm(scaled_z)

plt.scatter(x, y, marker='+', edgecolors=colors, s=150, linewidths=4)
plt.show()

Result:

That example gets the RGBA values by scaling the z values to the range [0,1], and calling the colormap plt.cm.coolwarm with the scaled values. When called this way, a matplotlib colormap returns an array of RGBA values, with each row giving the color of the corresponding input value. For example:

>>> t = np.linspace(0, 1, 5)
>>> t
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])
>>> plt.cm.coolwarm(t) 
array([[ 0.2298,  0.2987,  0.7537,  1.    ],
       [ 0.5543,  0.6901,  0.9955,  1.    ],
       [ 0.8674,  0.8644,  0.8626,  1.    ],
       [ 0.9567,  0.598 ,  0.4773,  1.    ],
       [ 0.7057,  0.0156,  0.1502,  1.    ]])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!