How to plot bar graph with matplotlib + Counter()

Asked

Viewed 82 times

0

Hello!

I’m trying to plot a bar graph from Data Science from Zero, but it’s very different from the book. Note: the book is in Python 2 and I am using Python 3

In the book, is:

from collections import Counter
friends_counts = Counter(num_friends)
xs = range(101)
ys = [friends_counts[x] for x in xs]
plt.bar(xs,ys)
plt.axis([0, 101, 0, 25])
plt.title('Histograma da contagem de amigos')
plt.xlabel('# de amigos')
plt.ylabel('# de pessoas')
plt.show()

Which results in:

Histograma de Contagem de amigos

Mine, it’s like this:

inserir a descrição da imagem aqui

The variable "friends_counts" stores:

friends_counts
Counter({100: 1, 49: 1, 41: 1, 40: 1, 25: 1})

I’m not getting the result of the book... Thank you very much!!

  • Looking through the code, since the variable friends_counts stores only Counter({100: 1, 49: 1, 41: 1, 40: 1, 25: 1}), the result you had was expected. By the graph generated in the book, this variable has much more values.

  • If you look in the book, the correct description for num_friends is [100, 49, 41, 40, 25, and many more]. The problem is "and many more". In chapter 1 there is an explanation that it is a graph problem (who is friends with whom) and shows how to calculate the complete list of friends (which has 204 elements).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.