Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python bar plot from list of tuples

Very newbie question:

I need to draw a bar plot from a list of tuples. The first element is a name (categorical) for the x axis, the second element is float type (for the y axis). I'd also like to order the bars in descending order, and add a trendline. Here is some sample code:

In [20]: popularity_data
Out[20]: 
[('Unknown', 10.0),
 (u'Drew E.', 240.0),
 (u'Anthony P.', 240.0),
 (u'Thomas H.', 220.0),
 (u'Ranae J.', 150.0),
 (u'Robert T.', 120.0),
 (u'Li Yan M.', 80.0),
 (u'Raph D.', 210.0)]
like image 854
user1799242 Avatar asked Oct 27 '25 10:10

user1799242


1 Answers

If you have a list of tuples, you can try the below code to get what you want.

import numpy as np
import matplotlib.pyplot as plt
popularity_data = [('Unknown', 10.0),
     (u'Drew E.', 240.0),
     (u'Anthony P.', 240.0),
     (u'Thomas H.', 220.0),
     (u'Ranae J.', 150.0),
     (u'Robert T.', 120.0),
     (u'Li Yan M.', 80.0),
     (u'Raph D.', 210.0)]

# sort in-place from highest to lowest
popularity_data.sort(key=lambda x: x[1], reverse=True) 

# save the names and their respective scores separately
# reverse the tuples to go from most frequent to least frequent 
people = zip(*popularity_data)[0]
score = zip(*popularity_data)[1]
x_pos = np.arange(len(people)) 

# calculate slope and intercept for the linear trend line
slope, intercept = np.polyfit(x_pos, score, 1)
trendline = intercept + (slope * x_pos)

plt.plot(x_pos, trendline, color='red', linestyle='--')    
plt.bar(x_pos, score,align='center')
plt.xticks(x_pos, people) 
plt.ylabel('Popularity Score')
plt.show()

This will give you a plot like the one below, although it doesn't make sense to plot a trend line on a bar plot when you aren't using a time series.

Bar plot of popularity_data

References:

  • Python: plot list of tuples
  • matplotlib api example code: barchart_demo.py
  • An Exercise With Matplotlib and Numpy
like image 129
moue Avatar answered Oct 28 '25 23:10

moue



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!