点的手动设置颜色我正在做散点图,看起来像这样:传说
(在问题的底部MWE)
正如上面图片中可以看出通过matplotlib
,图例中点的颜色自动设置为蓝色。我需要将这些点设置为颜色映射中不存在的其他颜色(即:黑色),以免它们与所述颜色映射关联的颜色混淆。
我环顾四周,但matplotlib.legend模块似乎并不接受color
关键字。有没有办法做到这一点?
这里的MWE:
import matplotlib.pyplot as plt
import numpy as np
def rand_data():
return np.random.uniform(low=0., high=1., size=(100,))
# Generate data.
x, y, x2, x3 = [rand_data() for i in range(4)]
# This data defines the markes and labels used.
x1 = np.random.random_integers(7, 9, size=(100,))
# Order all lists so smaller points are on top.
order = np.argsort(-np.array(x2))
# Order x and y.
x_o, y_o = np.take(x, order), np.take(y, order)
# Order list related to markers and labels.
z1 = np.take(x1, order)
# Order list related to sizes.
z2 = np.take(x2, order)
# Order list related to colors.
z3 = np.take(x3, order)
plt.figure()
cm = plt.cm.get_cmap('RdYlBu')
# Scatter plot where each value in z1 has a different marker and label
# assigned.
mrk = {7: ('o', '7'), 8: ('s', '8'), 9: ('D', '9')}
for key, value in mrk.items():
s1 = (z1 == key)
plt.scatter(x_o[s1], y_o[s1], marker=value[0], label=value[1],
s=z2[s1] * 100., c=z3[s1], cmap=cm, lw=0.2)
# Plot colorbar
plt.colorbar()
# Plot legend.
plt.legend(loc="lower left", markerscale=0.7, scatterpoints=1, fontsize=10)
plt.show()
谢谢你,这样做的伎俩。 – Gabriel
当你用'plt.plot'而不是'plt.scatter'绘制点时,你有没有试过这样做?不适合我。运行mpl v 2.0.0。 – saintsfan342000
@ saintsfan342000我还没有在这个特定的情况下尝试过。我会尽快回复您 –