Skip to content

Instantly share code, notes, and snippets.

@erikaris
Last active November 8, 2017 10:32
Show Gist options
  • Save erikaris/1fd7e170e9820e4705de8329a8507168 to your computer and use it in GitHub Desktop.
Save erikaris/1fd7e170e9820e4705de8329a8507168 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
languages =['Python', 'SQL', 'Java', 'C++', 'JavaScript']
pos = np.arange(len(languages))
popularity = [56, 39, 34, 34, 29]
# change the bar colors to be less bright blue
bars = plt.bar(pos, popularity, align='center', linewidth=0, color='lightslategrey')
# make one bar, the python bar, a contrasting color
bars[0].set_color('#1F77B4')
# soften all labels by turning grey
plt.xticks(pos, languages, alpha=0.8)
plt.ylabel('% Popularity', alpha=0.8)
plt.title('Top 5 Languages for Math & Data \nby % popularity on Stack Overflow', alpha=0.8)
# remove all the ticks (both axes), and tick labels on the Y axis
plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on')
'''
============Alternative way====================
colors = ['red', 'blue', 'blue', 'blue', 'blue']
plt.bar(pos, popularity, align='center', color=colors, alpha = 0.5, linewidth = 0)
plt.xticks(pos, languages)
plt.ylabel('% Popularity', color='grey')
plt.title('Top 5 Languages for Math & Data \nby % popularity on Stack Overflow', color='grey')
plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on', colors='grey')
'''
# TODO: direct label each bar with Y axis values
for a, b in enumerate(popularity):
plt.gca().text(a, b-5, str(b)+'%', color='white', fontweight='bold', ha='center')
'''
============Alternative way====================
# From Coursera
for bar in bars:
plt.gca().text(bar.get_x() + bar.get_width()/2, bar.get_height() - 5, str(int(bar.get_height())) + '%',
ha='center', color='w', fontsize=11)
'''
# remove the frame of the chart
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment