Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to make two lists from a dictionary

Tags:

python

I have a dictionary.

{1 : [1.2, 2.3, 4.9, 2.0],  2 : [4.1, 5.1, 6.3],  3 : [4.9, 6.8, 9.5, 1.1, 7.1]}

I want to pass each key:value pair to an instance of matplotlib.pyplot as two lists: x values and y values.

Each key is an x value associated with each item in its value.

So I want two lists for each key:

[1,1,1,1] [1.2,2.3,4.9,2.0]

[2,2,2] [4.1,5.1,6.3]

[3,3,3,3,3] [4.9,6.8,9.5,1.1,7.1]

Is there an elegant way to do this?

Or perhaps there is a way to pass a dict to matplotlib.pyplot?

like image 681
Peter Stewart Avatar asked Nov 30 '22 06:11

Peter Stewart


1 Answers

for k, v in dictionary.iteritems():
    x = [k] * len(v)
    y = v
    pyplot.plot(x, y)
like image 62
John Paulett Avatar answered Dec 05 '22 02:12

John Paulett