2

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')
1
  • isn't dic.keys() what you are looking for? Commented Dec 2, 2013 at 15:14

2 Answers 2

2

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')
2
  • There is no reason for the values to come out in a sensible order. Commented Dec 2, 2013 at 16:05
  • will u please tell me how to do it if have dictionary like this dic={'a':(1,2),'b':(2,2),'c':(3,2)} and i want to use only (1,2,3) Commented Dec 2, 2013 at 16:44
0

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.

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.