python bar plot from list of tuples


Question

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)]
1
1
12/1/2015 7:03:28 AM

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:

10
5/23/2017 12:34:12 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon