Fran Martinez
Fran Martinez

Reputation: 679

Plot a list of tuples into a histogram

I'm struggling with the formation of a chart. I have a list of the following values:

[('1', 2434), ('10', 6792), ('11', 5214), ('12', 3354), ('2', 2854), ('3', 5571), ('4', 5602), ('5', 5768), ('6', 7320), ('7', 7341), ('8', 7198), ('9', 6878)]

The values range from 1 to 12 for the first value of the tuple, because I'm working with monthly data.

I'm forming a histogram the following way:

lists = [('1', 2434), ('10', 6792), ('11', 5214), ('12', 3354), ('2', 2854), ('3', 5571), ('4', 5602), ('5', 5768), ('6', 7320), ('7', 7341), ('8', 7198), ('9', 6878)]
x, y = zip(*lists) # unpack a list of pairs into two tuples

plt.hist(lists)
plt.title('Monthly Trends in Chicago City')
plt.xlabel('Monthly')
plt.ylabel('Rides')
plt.show()

This is the chart the previous code is generating.

Any help is greatly appreciated.

chart image

Upvotes: 0

Views: 2281

Answers (1)

Fran Martinez
Fran Martinez

Reputation: 679

x, y = zip(*lists) # unpack a list of pairs into two tuples
x_months=['Jan', 'Oct', 'Nov', 'Dec', 'Feb', 'March', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept']

plt.bar(x_months, y, color='b')
plt.xticks(x_months, x_months, rotation='vertical')
plt.tight_layout()
plt.show()

This solved it.

Upvotes: 2

Related Questions