How to set marker type for a specific point in a matplotlib scatter plot with colormap

时间:2016-07-11 20:57:18

标签: python matplotlib

I have a user case that, let's say I have three series data: x,y,z. I would like to make a scatter plot using (x,y) as coordinates and z as the color of scatter points, using cmap keyword of plt.scatter. However, I would like to highlight some specific point by using a different marker type and size than other points.

​A minimum example is like below:

x,y,z = np.random.randn(3,10)
plt.scatter(x,y,c=z,cmap=matplotlib.cm.jet)
plt.colorbar()​

​If I want to use a different marker type for (x[5],y[5],z[5]), how could I do that? The only way I can think of is to plot again for this point using plt.scatter([x[5],y[5]) but define the color by manually finding the colormap ​color corresponding to z[5]. However this is quite tedious. Is there a better way?

1 个答案:

答案 0 :(得分:3)

每个散点图都有一个标记,默认情况下,您不能在单个散点图中使用不同的标记。因此,如果您乐意只更改标记大小并使标记保持不变,则可以为scatter的{​​{1}}参数提供不同大小的数组。

s

enter image description here

如果您确实需要不同的标记样式,则可以绘制新的散点图。然后,您可以将第二个散点图的颜色限制设置为第一个散点图的颜色限制。

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(10)

x,y,z = np.random.randn(3,10)

sizes = [36]*len(x)
sizes[5] = 121
plt.scatter(x,y,c=z,s=sizes, cmap=plt.cm.jet)

plt.colorbar()

plt.show()

enter image description here

最后,在this answer中将给出在同一散点图中有几个不同标记的复杂解决方案。