简体   繁体   English

为散点图中的每个点分配颜色

[英]assign color to every point in scatter plot

I need to assign a different color to every point, I have done a list with tuples that contains the coordinates (x,y).我需要为每个点分配不同的颜色,我已经做了一个包含坐标(x,y)的元组列表。 I need to assign a color for every tuple/point, and my question is, is it possible to assign the color directly to the tuple like this (x,y,'green') ?我需要为每个元组/点分配一个颜色,我的问题是,是否可以像这样(x,y,'green')直接将颜色分配给元组? Thanks in advance.提前致谢。

I include my code:我包括我的代码:

import numpy as np
import matplotlib.pyplot as plt

nodos=[(10,30,'21'),(30,30),(40,20),(50,70),(65,70),(70,50),(90,40),(100,20),(110,30)]
sta=[(10,40),(30,20),(50,20),(40,70),(75,70),(70,40),(90,50),(90,20),(120,30)]
etiquetasNode=['A','B','C','D','E','F','G','H','I']
etiquetasSTA=['STA_A','STA_B','STA_C','STA_D','STA_E','STA_F','STA_G','STA_H','STA_I']


x=list(map(lambda x: x[0],nodos))
y=list(map(lambda x: x[1],nodos))

a=list(map(lambda x: x[0],sta))
b=list(map(lambda x: x[1],sta))

plt.scatter(x,y)
plt.scatter(a,b)

for xi,yi,ei in zip(x,y,etiquetasNode):
    plt.text(xi,yi,ei,horizontalalignment='center',verticalalignment='bottom')


for xi,yi,ai in zip(a,b,etiquetasSTA):
    plt.text(xi,yi,ai,horizontalalignment='center',verticalalignment='top')


plt.scatter(x,y,linewidth=5,color=(1, 0, 0))
plt.scatter(a,b,linewidth=5,color=(0, 0, 1)) 

plt.title('Escenario Fuerza Bruta numero 0 cero')
plt.xlabel('distancia en x (mts)')
plt.ylabel('distancia en y (mts)')
plt.xlim(0,130)
plt.ylim(0,100)

t_data = ((0,2,0,0),(0,4,0,0),(0,2,0,0),(0,4,4,4),(0,2,6,5),(0,4,3,2),(0,2,12,2),(0,4,2,2),(0,8,24,2))
table=plt.table(cellText = t_data, 
                  colLabels = ('Canal', 'Throughput','packets_sent','packets lost'),
                  rowLabels = ('nodo A', 'nodo B','nodo C', 'nodo D','nodo E', 'nodo F','nodo G', 'nodo H','nodo I'),
                  loc='bottom', bbox=[0.0,-0.45,1,0.35])
plt.subplots_adjust(bottom=0.3)

plt.grid(True)
plt.show()

Yes, it should be possible to assign individual color to each point.是的,应该可以为每个点分配单独的颜色。 You can do it by assigning a color vector of length n (same length as your x and y vector).您可以通过分配长度为 n 的颜色向量(与 x 和 y 向量的长度相同)来实现。

https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.scatter.html https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.scatter.html

Small example:小例子:

import matplotlib.pyplot as plt
import numpy as np
x, y, c = np.random.rand(3, n)
#plt.scatter(x,y,color=c) #this works for different format of color vector
plt.scatter(x,y,c=c)
plt.show()

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

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