1

I'm struggling with the following problem in Python:

I got two dictionaries and I want to achieve a special plot using matplotlib.pylab. The dictionaries got the same length and holding the same keys in the same order, but holding different values for each of these keys. All keys and all values are integers.

I want to achieve a plot, where the x-axis shows the keys, in my case this would be 0 to 200, and the y axis should be some kind of histogram where for every x i want to display both values of the dictionary entries related to this x.

I think histogram ist the wrong word here, because in that case we would talk about a distribution. But this is not the case. I just want to display the value of both dicts for each key.

Any ideas?

2
  • 1
    You'll have better luck probably if order matters to you with lists - dictionaries are unordered (that's how they can be so fast). Commented Apr 22, 2016 at 21:27
  • right, that seems to be the problem Commented Apr 22, 2016 at 21:30

1 Answer 1

1

You can iterate simultaneously on both your dictionaries like this:

for key in sorted(dict1.keys()):    # iterkeys() in Python2
    value1 = dict1[key]
    value2 = dict2[key]
    plot(key, value1)
    plot(key, value2)
Sign up to request clarification or add additional context in comments.

3 Comments

this isn't going to work since, x is unknown. I think you meant "plot(key, value)", this is kinda solving my problem but not perfect.
I did mean that indeed, thanks for pointing it out. You can do it shorter: plot([(dict1[k], dict2[k]) for k in dict1.keys()]) if that looks more perfect to you. (zip if necessary)
Ok i played a lot around with and i seems ok to me now ! Thanks for the help !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.