tags:

views:

243

answers:

8

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?

+2  A: 

Maybe something like:

d = {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]}
result = []
for key, values in d.items():
    result.append(([key]*len(values), values))
Virgil Dupras
+3  A: 
d = {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]}

res = [([x]*len(y), y) for x, y in d.iteritems()]

res will be a list of tuples, where the first element in the tuple is your list of x-values and second element in the tuple is your list f y-values

sdtom
I think he asked for two separate lists for each X/Y value, not a list of tuples.
Edan Maor
He asked for two lists for each key in the dict. Each tuple in res contains the two lists he asked for.
sdtom
@Edan: figures = sum((pylab.plot(x, y) for x, y in res), [])
J.F. Sebastian
A: 

the map function in python will allow this

x = [1,2,4]
y = [1,24,2]
c = zip(x,y)
print c
d = map(None,x,y)
print d

check it out. This will give you

[(1, 1), (2, 24), (4, 2)]

In the case of zip(), if one of the lists are smaller then the others, values will be truncated:

x = [1,2,4]
a = [1,2,3,4,5]
c = zip(x,a)
print c
d = map(None,x,a)
print d

[(1, 1), (2, 2), (4, 3)]
[(1, 1), (2, 2), (4, 3), (None, 4), (None, 5)]
George
He has `{ a: b, c, d, e }` and wants `[a, a, a, a]` `[b, c, d, e]` (the key stretched across the length of the value) -- not this.
Jed Smith
+11  A: 
for k, v in dictionary.iteritems():
    x = [k] * len(v)
    y = v
    pyplot.plot(x, y)
John Paulett
+1 for readability. The other solutions are good, but this one is readable -- which is always a plus.
Jed Smith
Simple is better. Love this solution!
jathanism
@Jed, thanks for changing that list comphresion into [k] * len(v). You taught me something new!
John Paulett
+2  A: 

Use this list comprehension:

[([k]*len(v), v) for k, v in D.iteritems()]

Here's an example of it being used:

>>> from pprint import pprint
>>> D = {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]}
>>> LL = [([k]*len(v), v) for k, v in D.iteritems()]
>>> pprint(LL)
[([1, 1, 1, 1], [1.2, 2.2999999999999998, 4.9000000000000004, 2.0]),
 ([2, 2, 2], [4.0999999999999996, 5.0999999999999996, 6.2999999999999998]),
 ([3, 3, 3, 3, 3],
  [4.9000000000000004,
   6.7999999999999998,
   9.5,
   1.1000000000000001,
   7.0999999999999996])]
Roger Pate
A: 

I guess that a wizard will put something nicer, but I would do something like:

map(lambda x: ([x]*len(a[x]),a[x]),a)

for a tuple, or

map(lambda x: [[x]*len(a[x]),a[x]],a)

for a list.

btw: a is the dictionary, of course!

I assume that you work with the 2.x series... Regards

cpicada
+1  A: 
>>> d = {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]}
>>> result = [ ([k] * len(d[k]), d[k]) for k in d.keys() ]
>>> print result
[([1, 1, 1, 1], [1.2, 2.2999999999999998, 4.9000000000000004, 2.0]), ([2, 2, 2],
[4.0999999999999996, 5.0999999999999996, 6.2999999999999998]), ([3, 3, 3, 3, 3],
[4.9000000000000004, 6.7999999999999998, 9.5, 1.1000000000000001, 7.0999999999999996])]
K. Brafford
+2  A: 

As a list comprehension:

r = [([k]*len(v), v) for k,v in d.items()]

If your dictionary is very large, you'd want to use a generator expression:

from itertools import repeat
r = ((repeat(k, len(v)), v) for k,v in d.iteritems())

...though note that using repeat means that the first item in each tuple the generator returns is itself a generator. That's unnecessary if the dictionary's values don't themselves have many items.

Robert Rossney
I like this for the information, I learned from it, but John Paulette gets the tick.
Peter Stewart
You've missed parenthesis: `(repeat(k, len(v)), v)`.
J.F. Sebastian
`pylab.plot` doesn't accept iterators therefore `repeat()` is not applicable in this case.
J.F. Sebastian
Missing paren fixed. And boo on `pylab.plot` for not accepting generators, though I imagine there's probably a reason.
Robert Rossney