在python中绘制散点图,其中z值对应于颜色键

时间:2018-01-31 14:47:38

标签: python matplotlib plot colors

我想绘制散点图,其中每个点的颜色对应于第三个数组的值。 颜色需要特定于称为颜色的键。 因此,如果z值为4,那么颜色应为'#9C6BE1'

colors= ["#049FBB", "#5DF10A", "#BF5B3F", "#A95058", "#9C6BE1","#6CC43C","#7B3001", "#B3D407","#97CE66"] #hex colours

x = [1,2,3,4]
y = [3,4,8,6]
z= [4,6,8,2]

plt.scatter(x,y,c=c)

如何将每个点绘制为与z的值对应的颜色?

2 个答案:

答案 0 :(得分:0)

试试这个:通过使用列表推导,您可以使用z中的值作为索引,创建一个包含所需颜色的列表。

colors= ["#049FBB", "#5DF10A", "#BF5B3F", "#A95058", "#9C6BE1","#6CC43C","#7B3001", "#B3D407","#97CE66"] #hex colours

x = [1,2,3,4]
y = [3,4,8,6]
z= [4,6,8,2]

plt.scatter(x,y,c=[colors[index] for index in z])

产生了这张照片

enter image description here

答案 1 :(得分:0)

您可以尝试使用列表推导和列表索引

colors= ["#049FBB", "#5DF10A", "#BF5B3F", "#A95058", "#9C6BE1","#6CC43C","#7B3001", "#B3D407","#97CE66"] #hex colours

x = [1,2,3,4]
y = [3,4,8,6]
z= [4,6,8,2]

plt.scatter(x,y,c=[colors[i] for i in z])