更改matplotlib中散点图的颜色

时间:2016-05-19 13:12:48

标签: python pandas matplotlib colormap

我对pythons图形可视化工具完全陌生,我正试图展示一个散点图。问题是我似乎无法获得与灰度值不同的点的颜色。这是我的代码:

cmap = cm.get_cmap('Spectral')
cat_length_1 = len(categories[cat1])
cat_length_2 = len(categories[cat2])
fig = plt.figure()
ax = fig.add_subplot(111)
col = [0.123, 2598.35, 32.66, 669.3]
df.plot(cat1, cat2, kind='scatter', ax=ax, s=65, c =df[cat1] , linewidth=0, colormap=cmap)
plt.xticks(np.arange(cat_length_1 + 1), categories[cat1])
plt.yticks(np.arange(cat_length_2 + 1), categories[cat2])

if label_list != None:
    for k, v in df.iterrows():
        if k in label_list:
            ax.annotate(k, (v[cat1], v[cat2]), xytext=(rnd.randint(-30, 30), rnd.randint(-30, 30)), textcoords='offset points',
                    family='sans-serif', fontsize=12, color='darkslategrey',
                    arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))

plt.tight_layout()
plt.show()

提供此结果: enter image description here

我想基于x轴值为所有不同颜色着色我的点(但即使是随机颜色也可以)。

感谢。

1 个答案:

答案 0 :(得分:0)

colormaps获取某些输入值(例如0-255中的RGB),因此您用于分配颜色的DataFrame列需要与这些输入相对应价值观,即。您需要将categories映射到任何可接受的颜色值。有关更多实施细节,请参阅herehere

相关问题