使用带有字典的matplotlib在python中绘制散点图 [英] Plot a scatter plot in python with matplotlib with dictionary

查看:60
本文介绍了使用带有字典的matplotlib在python中绘制散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绘制字典的帮助,下面是数据样本数据集.我想创建一个散点图,其中x:y是(x,y)坐标,标题"x"是该图的图例..

I need help plotting a dictionary, below is the data sample data set. I want to create a scatter graph where x:y are (x,y) coordinates and title'x' would be the legend of the graph.. I want to create graphs of below data set so combine all the below data in one graph.

例如:以红色(或任何其他颜色)绘制 title1':{x:y, x:y} 制作一个图例(键),说红色(或任何颜色)用于 title1,

for example: plot title1':{x:y, x:y} in red( or any other color) make a legend(key) saying red(or whatever color) is for title1,

对title2:{x:y,x:y}(用不同的颜色)...做同样的操作.

do same for title2:{x:y, x:y} (in a different color)....and so on.

任何帮助将不胜感激.谢谢.

Any help would be greatly appreciated. Thank you.

data = {'title1':{x:y, x:y},title2:{x:y,x:y,x:y},'title3':{x:y,x:y}....}

我也遵循了这个建议,但它是针对单个图形的.在 Myplotlib python 中的字典中绘制字典

I also followed this advise, but it was for individual graph. Plotting dictionaries within a dictionary in Myplotlib python

这是我尝试过的,我在 matplotlib 方面没有太多经验,也找不到任何有用的在线信息.任何帮助将不胜感激.

This is what I have tried, i don't have much experience in matplotlib and couldn't find anything useful onlline. Any help would be greatly appreciated.

import matplotlib.pyplot as plt
import numpy as np
d ={'5000cca234c1c445': {382877: 7, 382919: 3},
'5000cca234c94a2e': {382873: 1, 382886: 1},
'5000cca234c89421': {383173: 1, 383183: 2, 382917: 1, 382911: 1},
'5000cca234c5d43a': {382889: 1, 382915: 1, 382917: 8},
'5000cca234c56488': {382909: 2, 382911: 5}}

xval = []
yval= []
ttle = []
print d
for title, data_dict in d.iteritems():
   x = data_dict.keys()
   #print 'title is', title
   #print 'printing x values',x   
   xval = xval + x
   print xval
   y = data_dict.values()
   yval = yval+y
   ttle.append(title)
   print  yval
#print 'printing y values', y         
#plt.figure()
print xval
print yval
print ttle
plt.scatter(xval,yval)
plt.show()

推荐答案

您可以尝试在循环上绘图,然后显示图例,如下所示:

You can try to plot on the loop, and after that show the legend, something like this:

import matplotlib.pyplot as plt

d ={'5000cca234c1c445': {382877: 7, 382919: 3},
'5000cca234c94a2e': {382873: 1, 382886: 1},
'5000cca234c89421': {383173: 1, 383183: 2, 382917: 1, 382911: 1},
'5000cca234c5d43a': {382889: 1, 382915: 1, 382917: 8},
'5000cca234c56488': {382909: 2, 382911: 5}}

colors = list("rgbcmyk")

for data_dict in d.values():
   x = data_dict.keys()
   y = data_dict.values()
   plt.scatter(x,y,color=colors.pop())

plt.legend(d.keys())
plt.show()

这篇关于使用带有字典的matplotlib在python中绘制散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆