简体   繁体   English

如何使用colormap为matplotlib散点图中的特定点设置标记类型

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

I have a user case that, let's say I have three series data: x,y,z. 我有一个用户案例,假设我有三个系列数据: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. 我想使用(x,y)作为坐标,使用plt.scatter的cmap关键字作为散点的颜色制作散点图。 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? 如果我想为(x [5],y [5],z [5])使用不同的标记类型,我该怎么做? 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]. 我能想到的唯一方法是使用plt.scatter([x [5],y [5])再次绘制此点,但通过手动查找与z [5]对应的colormap颜色来定义颜色。 However this is quite tedious. 然而,这非常繁琐。 Is there a better way? 有没有更好的办法?

Each scatterplot has one single marker, you cannot by default use different markers in a single scatterplot. 每个散点图都有一个标记,默认情况下,您不能在单个散点图中使用不同的标记。 Hence, if you are happy to only change the markersize and leave the marker the same, you can supply an array of different sizes to the scatter 's s argument. 因此,如果您乐意只更改markerize并使标记保持不变,则可以为scatter s参数提供不同大小的数组。

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()

在此输入图像描述

If you really need a different marker style, you can to plot a new scatter plot. 如果您确实需要不同的标记样式,则可以绘制新的散点图。 You can then set the colorlimits of the second scatter to the ones from the first. 然后,您可以将第二个散点图的颜色限制设置为第一个散点图的颜色限制。

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

x,y,z = np.random.randn(3,10)
xs, ys, zs = [x[5]], [y[5]], [z[5]]
print xs, ys, zs
y[5] = np.nan

sc = plt.scatter(x,y,c=z,s=36, cmap=plt.cm.jet)
climx, climy = sc.get_clim()
plt.scatter(xs,ys,c=zs,s=121, marker="s", cmap=plt.cm.jet, vmin=climx, vmax=climy  )  

plt.colorbar()

plt.show()

在此输入图像描述

Finally, a bit of a complicated solution to have several different markers in the same scatter plot would be given in this answer . 最后,在这个答案中将给出在同一散点图中有几个不同标记的复杂解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM