0

I need to assign a different color to every point, I have done a list with tuples that contains the coordinates (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')? 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()
ilke444
  • 2,641
  • 1
  • 17
  • 31
  • Does this answer your question? [Setting different color for each series in scatter plot on matplotlib](https://stackoverflow.com/questions/12236566/setting-different-color-for-each-series-in-scatter-plot-on-matplotlib) – Suraj Mar 25 '20 at 05:16
  • not really, i recenty have to probe it , but dont work in my case, i notice i that link , is used a list with only the values , but i have a tuple with the coordinates – Edwin Reyes Mar 25 '20 at 05:37

1 Answers1

1

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

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()
paloman
  • 160
  • 9