1

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
  • 11,147
  • 5
  • 36
  • 50
Mohit
  • 21
  • 1
  • 3

3 Answers3

5

You can simply use:

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

enter image description here

yatu
  • 80,714
  • 11
  • 64
  • 111
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
  • 25,410
  • 6
  • 56
  • 79
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
  • 11,147
  • 5
  • 36
  • 50