简体   繁体   中英

Python : Plotting in the same graph

I have a dataset like this but for many ids :

Information = [{'id' : 1,
'a' : array([0.7, 0.5 , 0.20 , 048 , 0.79]),
'b' : array([0.1, 0.5 , 0.96 , 08 , 0.7]))}, 
{'id' : 2,
'a' : array([0.37, 0.55 , 0.27 , 047 , 0.79]),
'b' : array([0.1, 0.5 , 0.9 , 087 , 0.7]))}]

I would like to plot these in one graph a on x axis and b on y axis for many different ids.

I can make one plot by doing this?

a_info = information[1]['a'] 
b_info = information [2]['b]
plt.scatter(a_info , b_info) 
plt.show()

but how do I do it for all plots?

e = [d['id'] for d in information]
for i in e:
  a_info = information[i]['a'] 
  b_info = information [i]['b]
  plt.scatter(a_info , b_info) 
  plt.show()

You can loop over the ids, and create plots for each substructure:

import matplotlib.pyplot as plt
from numpy import array
information = [{'id' : 1, 'a':array([0.7, 0.5 , 0.20 , 0.48 , 0.79]), 'b':array([0.1, 0.5 , 0.96 , 0.8 , 0.7])}, {'id':2, 'a':array([0.37, 0.55, 0.27 , 0.47 , 0.79]), 'b':array([0.1, 0.5 , 0.9 , 0.87 , 0.7])}]
colors = iter(['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'])
for i in information:
   plt.scatter(i['a'], i['b'], label = 'id{}'.format(i['id']), color=next(colors))

plt.legend(loc='upper left')
plt.show()

在此处输入图片说明

You can loop over all ids and plot them:

for i in Information:
    plt.scatter(i['a'], i['b'], label=i['id'])
plt.legend()
plt.show()

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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