简体   繁体   English

在matplotlib中更改散点图点的颜色

[英]Change colors of scatter plot points in matplotlib

I'm totally new to pythons graph visualization tools and I'm trying to show a scatter plot. 我是python图形可视化工具的新手,并且试图显示散点图。 The problem is that I can't seem to be able to obtain colors for my points different from grayscale values. 问题是我似乎无法获得不同于灰度值的点的颜色。 This is my code: 这是我的代码:

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()

which provides this result: 提供以下结果: 在此处输入图片说明

I would like to color my points with all different colors based on the x axis value (but even random colors are ok). 我想根据x轴值为我的点涂上所有不同的颜色(但即使是随机颜色也可以)。

Thanks. 谢谢。

The colormaps take certain input values (eg 0-255 in RGB ), so the DataFrame column that you are using to assign the colors needs to correspond to these input values, ie. colormaps采用某些输入值(例如RGB 0-255 ),因此用于分配颜色的DataFrame列需要对应于这些输入值,即。 you need to map your categories to any of the accepted color values. 您需要将categories映射到任何可接受的颜色值。 See here and here for more implementation detail. 有关更多实现细节,请参见此处此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM