2

I want to plot the following dictionary on python using matplotlib.pyplot as a histogram. How can I code it up?

{'G': 198, 'T': 383, 'C': 260, 'A': 317}
Joe
  • 12,057
  • 5
  • 39
  • 55
Mohit
  • 31
  • 1
  • 3

3 Answers3

5

You can simply use:

plt.bar(data.keys(), data.values())

enter image description here

yatu
  • 86,083
  • 12
  • 84
  • 139
2

The histogram would be needed if you did not know the frequency of each item. That is, if you had data of the form

G G T G C A A T G

Since you already know the frequencies, it is just a simple bar plot

{'G': 198, 'T': 383, 'C': 260, 'A': 317}
labels, values = zip(*data.items())
plt.bar(labels, values)
blue_note
  • 27,712
  • 9
  • 72
  • 90
1

With pandas:

d = {'G': 198, 'T': 383, 'C': 260, 'A': 317}
df = pd.DataFrame({x:[y] for x,y in d.iteritems()}).T
df.plot(kind='bar')
plt.show()

enter image description here

Joe
  • 12,057
  • 5
  • 39
  • 55