Shey
Shey

Reputation: 25

Fast ways to plot points in a list of lists

I have a dataset that includes (x,y) of nearly 30 different points named "point_1", "point_2" and so on. The following shows a sample of a list of lists that I have:

list_p = [['point_1',[2,3]],['point_2',[3,3]],['point_1',[2,4]],['point_3',[4,5]],['point_4',[4,7]]]

where the first element in each list is the name of a point and the second element is its (x,y). I am wondering if it is possible to have a plot that each point name has its (x,y) colored differently than (x,y) of other points. I mean for example all (x,y) of "point_1" have blue color, all (x,y) of "point_2" have red color and so on.

Upvotes: 2

Views: 1024

Answers (3)

gboffi
gboffi

Reputation: 25033

You wrote:

... is there a faster way to do this?
I mean for example creating different lists for different points in list_p and then coloring them?

Well, there is such a possibility but I'd leave to you the investigation of which alternative is faster... let's start with your list (note: I have added another (x,y) to "point_1") and an empty dictionary:

In [56]: import matplotlib.pyplot as plt 
    ...:  
    ...: list_p = [['point_1',[2,3]], 
    ...:           ['point_2',[3,3]], 
    ...:           ['point_1',[2,4]], 
    ...:           ['point_3',[4,5]], 
    ...:           ['point_4',[4,7]], 
    ...:           ['point_1',[2,1]]] 
    ...: d = {}                             

To plot with scatter we need, for each point type, a list of xes and a list of ys, and for labelled data the natural choice is a dictionary

In [57]: for p, (x, y) in list_p: 
    ...:     xs, ys = d.setdefault(p, [[],[]]) 
    ...:     xs.append(x), ys.append(y)                                                                                                                      

In [58]: d                                                                                                                                                   
Out[58]: 
{'point_1': [[2, 2, 2], [3, 4, 1]],
 'point_2': [[3], [3]],
 'point_3': [[4], [5]],
 'point_4': [[4], [7]]}

Finally we can plot our lists — note that now ① it is possible to add appropriate labels to the point categories and ② the different colors for different categories are dealt with automatically.

In [60]: for p, (xs, ys) in d.items(): 
    ...:     plt.scatter(xs, ys, label=p) 
    ...: plt.legend()                                                                                                                                        

enter image description here

Upvotes: 2

Patol75
Patol75

Reputation: 4547

An example solution using a dictionary to store the mapping between name and color.

import matplotlib.pyplot as plt

list_p = [['point_1', [2, 3]], ['point_2', [3, 3]], ['point_1', [2, 4]],
          ['point_3', [4, 5]], ['point_4', [4, 7]]]
nameCol = {'point_1': 'blue', 'point_2': 'red', 'point_3': 'green',
           'point_4': 'black'}
for name, loc in list_p:
    plt.scatter(loc[0], loc[1], color=nameCol[name])
plt.show()

enter image description here

EDIT:

import matplotlib.pyplot as plt

list_p = [['point_1', [2, 3]], ['point_2', [3, 3]], ['point_1', [2, 4]],
          ['point_3', [4, 5]], ['point_4', [4, 7]]]
nameCol = {'point_1': 'blue', 'point_2': 'red', 'point_3': 'green',
           'point_4': 'black'}
for key in nameCol.keys():
    plt.scatter([value[0] for name, value in list_p if name == key],
                [value[1] for name, value in list_p if name == key],
                color=nameCol[key])
plt.show()

Upvotes: 2

Abhishek Prashant
Abhishek Prashant

Reputation: 375

You can try this

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np

#Get list of distinct points
distinct_points = set([p[0] for p in list_p])
#Map each distinct point to a different color
map_color_points = {p:c for p,c in zip(distinct_points, cm.rainbow(np.linspace(0,1,len(distinct_points)))}
for p in list_p:
    plt.scatter(p[1][0], p[1][1], color = map_color_points[p[0]])
    plt.annotate(p[0], (p[1][0], p[1][1]))
plt.show()

Resultenter image description here

Upvotes: 1

Related Questions