Matplotlib 散点图;颜色作为第三个变量的函数 [英] Matplotlib scatterplot; color as a function of a third variable
问题描述
我想制作一个散点图(使用 matplotlib),其中根据第三个变量对点进行着色.我对此非常了解:
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')
其中 w 和 M 是数据点,p 是我想要着色的变量.
但是我想用灰度而不是彩色来做.有人可以帮忙吗?
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?
推荐答案
无需手动设置颜色.相反,指定灰度颜色图...
There's no need to manually set the colors. Instead, specify a grayscale colormap...
import numpy as np
import matplotlib.pyplot as plt
# Generate data...
x = np.random.random(10)
y = np.random.random(10)
# Plot...
plt.scatter(x, y, c=y, s=500)
plt.gray()
plt.show()
或者,如果您更喜欢更广泛的颜色图,您也可以指定 cmap
kwarg 到 scatter
.要使用其中任何一个的反向版本,只需指定其中任何一个的_r
"版本.例如.gray_r
而不是 gray
.有几种不同的预制灰度颜色图(例如 gray
、gist_yarg
、binary
等).
Or, if you'd prefer a wider range of colormaps, you can also specify the cmap
kwarg to scatter
. To use the reversed version of any of these, just specify the "_r
" version of any of them. E.g. gray_r
instead of gray
. There are several different grayscale colormaps pre-made (e.g. gray
, gist_yarg
, binary
, etc).
import matplotlib.pyplot as plt
import numpy as np
# Generate data...
x = np.random.random(10)
y = np.random.random(10)
plt.scatter(x, y, c=y, s=500, cmap='gray')
plt.show()
这篇关于Matplotlib 散点图;颜色作为第三个变量的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!