Matplotlib散点图;颜色作为第三个变量的函数 [英] Matplotlib scatterplot; colour as a function of a third variable

查看:105
本文介绍了Matplotlib散点图;颜色作为第三个变量的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个散点图(使用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 datapoints 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.预制了几种不同的灰度颜色图(例如graygist_yargbinary等).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆