Amar Agrawal
Amar Agrawal

Reputation: 141

Using dictionary as parameter in matplotlib in python

how can i use dictionary key or value in matplotlib instead of tuple

tuple=(1,2,3,4)
rects1 = ax.bar(ind, tuple, width, color='r')

but i want to do it like this

dic={'1':1,'2':2,'3':3,'4':4}
rects1 = ax.bar(ind, dic[key], width, color='r')

Upvotes: 2

Views: 373

Answers (2)

Daren Thomas
Daren Thomas

Reputation: 70314

Try this:

rects1 = ax.bar(ind, (dic[key] for key in dic.keys()), width, color='r')

Or even:

rects1 = ax.bar(ind, dic.values(), width, color='r')

based on Amars comment, if the dict is something like this:

dic={'a':(1,2),'b':(2,2),'c':(3,2)}

and you only want to use the values (1, 2, 3) you need to create an ordered set:

rects1 = ax.bar(ind, sorted(set(dic.values())), width, color='r')

Upvotes: 2

tacaswell
tacaswell

Reputation: 87376

I supect you want to do something like this

rects1 = ax.bar(ind, (dic[k] for k in ind), ...)

so that there is a mapping between your index and the values in your dictionary.

Upvotes: 0

Related Questions