如何根据第三个变量对分散标记进行着色
我想制作一个散点图(使用 matplotlib),其中点根据第三个变量进行着色。我已经非常接近这个:
plt.scatter(w, M, c=p, marker='s')
其中 w 和 M 是数据点,p 是我想要相对于其进行着色的变量。
不过我想用灰度而不是彩色来做。有人可以帮忙吗?
I want to make a scatterplot (using matplotlib) where the points are shaded according to a third variable. I've got very close with this:
plt.scatter(w, M, c=p, marker='s')
where w and M are the data points and p is the variable I want to shade with respect to.
However I want to do it in greyscale rather than colour. Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无需手动设置颜色。相反,指定灰度颜色图...
或者,如果您更喜欢 更广泛的颜色图,您还可以指定
cmap
kwarg分散
。要使用其中任何一个的反向版本,只需指定其中任何一个的“_r
”版本即可。例如,用gray_r
代替gray
。有几种不同的预先制作的灰度颜色图(例如gray
、gist_yarg
、binary
等)。There's no need to manually set the colors. Instead, specify a grayscale colormap...
Or, if you'd prefer a wider range of colormaps, you can also specify the
cmap
kwarg toscatter
. To use the reversed version of any of these, just specify the "_r
" version of any of them. E.g.gray_r
instead ofgray
. There are several different grayscale colormaps pre-made (e.g.gray
,gist_yarg
,binary
, etc).在 matplotlib 中,灰色可以作为 0-1 之间的数值字符串给出。
例如
c = '0.1'
然后您可以将第三个变量转换为该范围内的值,并使用它为您的点着色。
在以下示例中,我使用点的 y 位置作为确定颜色的值:
In matplotlib grey colors can be given as a string of a numerical value between 0-1.
For example
c = '0.1'
Then you can convert your third variable in a value inside this range and to use it to color your points.
In the following example I used the y position of the point as the value that determines the color:
有时您可能需要根据 x 值情况精确绘制颜色。例如,您可能有一个包含 3 种类型的变量和一些数据点的数据框。您想要执行以下操作,
在这种情况下,您可能必须编写短函数以将 x 值作为列表映射到相应的颜色名称,然后将该列表传递给 plt.scatter 命令。
Sometimes you may need to plot color precisely based on the x-value case. For example, you may have a dataframe with 3 types of variables and some data points. And you want to do following,
In this case, you may have to write to short function to map the x-values to corresponding color names as a list and then pass on that list to the
plt.scatter
command.一个非常简单的解决方案也是这样:
A pretty straightforward solution is also this one: