0

I have a set:

K = {'A', 'E', 'R', 'T'}

and a dictionary called:

Dic = {'A': 25, 'C': 35, 'E': 10, 'R': 7, 'S': 9, 'T': 11}

I know to plot the dictionary Dic by

X = np.arange(len(Dic))
plt.bar(X,Dic.values(),width=0.5)
plt.xticks(X,list(Dic.keys()))

enter image description here

Now I want to change the colours of the bar 'A', 'E' , 'R', 'T' in the graph. Simply like Dic intersection K.

But, I don't know how to compare the set items with the dictionary.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Rangooski
  • 825
  • 1
  • 11
  • 29

1 Answers1

-1
K.intersection(Dic)

That will get you the keys that are in both.

The color changing part of your question is answered here:

Setting Different Bar color in matplotlib Python

Dic = {'A': 25, 'C': 35, 'E': 10, 'R': 7, 'S': 9, 'T': 11}
K = {'A', 'E', 'R', 'T'}
values = [Dic[key] for key in sorted(Dic)]
intersection = K.intersection(Dic)
X = range(len(Dic))


bar_graph = plt.bar(X, values,width=0.5)
print intersection, 'inter'
for i, thing in enumerate(sorted(Dic)):
    if thing in intersection:
        bar_graph[i].set_color('r')
plt.xticks(X,sorted(Dic.keys()))
plt.show()

You have to do some sorting to make sure the keys are in the same place as plotted. Python dictionaries do not keep the keys in any sorted order.

Community
  • 1
  • 1
Garrett R
  • 2,687
  • 11
  • 15