Python散点图,其颜色对应于字符串 [英] Python scatter plot with colors corresponding to strings

查看:356
本文介绍了Python散点图,其颜色对应于字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用python matplotlib绘制散点图,其中点的颜色应与数据文件中的特定字符串相对应,所以类似这样:

I want to make a scatter plot with python matplotlib where the color of the dot should correspond with a particular string from a data file, so something like this:

data = np.genfromtxt('filename.txt', delimiter=',', dtype=None, names=['a', 'b', 'c'])
plt.scatter(data['a'], data['b'])

文件"a"的第一列为浮点数,第二列"b"为浮点数,第三列"c"为字符串.字符串列包含5个不同的词,我想绘制它们,因为散点图有5种不同的颜色.有任何想法吗?非常感谢!

Whereby the first column of the file 'a' is a float, the second column 'b' is a float and the third column 'c' is a string. The string column contains 5 different words which I would like to plot as 5 different colors is the scatter plot. Any ideas? Thanks a lot!

推荐答案

遵循这些原则可以解决问题:

Something along these lines should do the trick:

color_dict = { 'Allan':'red', 'Betty':'blue', 'Chris':'black', 'Diane':'green','Eugene':'purple' }

plt.scatter( data['a'], data['b'], color=[ color_dict[i] for i in data['c'] ] )

这篇关于Python散点图,其颜色对应于字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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