繁体   English   中英

Python:在同一图中绘制

[英]Python : Plotting in the same graph

我有一个像这样的数据集,但有很多id:

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]))}]

我想将它们绘制在一张图上,其中x轴上的a和y轴上的b的许多不同的ID。

我可以这样做吗?

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

但是我该如何在所有地块上做呢?

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

您可以遍历id,并为每个子结构创建图:

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

在此处输入图片说明

您可以遍历所有id并绘制它们:

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

输出:

在此处输入图片说明

暂无
暂无

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

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