0

I have list containing votes (smth like this votes = [1, 2, 3, 4, 1, 1, 3, 4, 4, 4]). How to use pyplot to plot a graph showing the count of each vote (i.e. 1 -> 3 votes, 2 -> 1 vote, 3 -> 2 votes, 4 -> 4 votes)

Here is how I am doing it:

from collections import Counter
import matplotlib.pyplot as plt

votes = [1,1,1,2,3,3,4,4,4,4,4]
tmp_votes_count = Counter (votes)
votes_count = []

for i in tmp_votes_count:
    votes_count.append ([i, tmp_votes_count[i]])

plt.plot([row[0] for row in votes_count], [row[1] for row in votes_count])
plt.axis([0,4,0,20])
plt.show()

Is there a more optimized way to do it? Also how to style the graph as bar chart instead of the continuous line? I mean smth similar to this:

enter image description here

instead of what I am getting right now: enter image description here

vishes_shell
  • 20,161
  • 6
  • 65
  • 77
A_Matar
  • 1,960
  • 3
  • 24
  • 50

2 Answers2

1

Just do

plt.bar([row[0] for row in votes_count], [row[1] for row in votes_count])
vishes_shell
  • 20,161
  • 6
  • 65
  • 77
1

If you know pandas , it will be very easy.

votes=pd.DataFrame(data=votes,columns=['List'])
votes.List.hist()
vishes_shell
  • 20,161
  • 6
  • 65
  • 77
BENY
  • 296,997
  • 19
  • 147
  • 204