7

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.

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,

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

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

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()
Community
  • 1
  • 1
user3262210
  • 117
  • 1
  • 3
  • 7
  • Where are you getting the titles from? And are you asking for the exact syntax of Matplotlib? Or are you asking how to get from a dictionary into that format? – ForgetfulFellow Feb 17 '14 at 07:54
  • Can you show use any code of what you have tried? This question currently reads as 'please do my work for me'. You will get much better help if you question reads as 'here is what I have tried, I want it to do X, but it does Y, what is wrong' – tacaswell Feb 17 '14 at 14:21
  • Sorry for providing not enough info... this is what I have tried.. – user3262210 Feb 17 '14 at 14:45

2 Answers2

8

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()
Alvaro Fuentes
  • 16,937
  • 4
  • 56
  • 68
  • How can this possibly work? Isn't the order of keys() and values() random? How can they possibly line up correctly? – robru Mar 07 '15 at 06:28
  • 3
    @Robru I think here is the answer [https://docs.python.org/2/library/stdtypes.html#dict.items]. TLTR; they are random but in correspondence with each other if no modification of the dictionary is made between calls. – Alvaro Fuentes Mar 09 '15 at 13:27
  • 3
    Note that this does not work in python 3.x. You'd have to write something like `list(data_dict.keys())` (same for the values). See http://stackoverflow.com/questions/31755900/python-3-xs-dictionary-view-objects-and-matplotlib for further details. – j-i-l Jul 31 '15 at 22:50
2

Try this:

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

plt.scatter(*zip(*data.items()))
plt.show()

The items() method returns a view object that displays a list of dictionary's (key, value) tuple pairs.

Using zip() would aggregate the tuple pairs in a tuple of x's and y's, which you would then plot.

  • 3
    Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adriaan Nov 09 '20 at 10:59