Learning by cloning with matplotlib -1-

In [1]:
# An article from NPR.org shows a graph about most and least lucrative
# college majors.
# I will try to clone the part of the graph for the most lucrative majors.
from IPython.core.display import Image
Image(url="http://www.npr.org/news/graphics/2013/10/pm-top_lowest-624.gif")
# Image Source: Georgetown Center On Education And The Workforce
# Credit: Quoctrung Bui / NPR
Out[1]:

The Most And Least Lucrative College Majors, In 2 Graphs Image Source: Georgetown Center On Education And The Workforce Credit: Quoctrung Bui / NPR

In [17]:
# Imports are the same as last time
import matplotlib.pyplot as plt
%matplotlib inline

# this time I'm going to start with the ticks and labels
majors = [('Electrical Engineering', 94),
          ('Chemical Engineering', 95),
          ('Naval Architecture And\nMarine Engineering', 96),
          ('Aerospace Engineering', 96),
          ('Mathematics And\nComputer Science', 98),
          ('Pharmacology', 100),
          ('Pharmaceutical Sciences\nAnd Administration', 107),
          ('Nuclear Engineering', 110),
          ('Health And Medical\nPreparatory Programs', 120),
          ('Petroleum Engineering', 120)]

xincome = [0, 20, 40, 60, 80, 100, 120]

# create the figure
fig, axis1 = plt.subplots(figsize=(9, 6), dpi=75)

# set the Y ticks and set the limits of the graph
plt.yticks(range(len(majors)), [ x[0] for x in majors ], size=11)
plt.ylim((-0.5, 9.6))
plt.xlim((0, 121))

# set the X ticks
plt.xticks(xincome)
from matplotlib.ticker import FormatStrFormatter
# This is a printf like formatter, I am using it to display extra zeros and a '$'
currency_format = FormatStrFormatter('$%d,000')
# ant tell it where to use apply it
axis1.xaxis.set_major_formatter(currency_format)

# draw the horizontal bars
m_earnings = [mj[1] for mj in majors]
container = plt.barh(range(len(majors)), m_earnings, height=0.7,
                     align='center', color='#28546e')

# write the value on each bar
for bar in container:
    x = int(bar.get_width())
    y = bar.get_y() + bar.get_height()*.3
    bar_text = str("$%s,000" % bar.get_width())
    axis1.text(x, y, bar_text, color='white',
               ha='right', size=13)
    
# set the vertical grid along the x axis
axis1.xaxis.grid(True, linestyle=':', which='major', color='grey', alpha=1)

# name the graph
plt.text(-47, 9.7, 'Majors With The Highest Earnings', size=14, weight='bold')

# clean the spines
axis1.spines['top'].set_visible(False)
axis1.spines['right'].set_visible(False)
axis1.yaxis.set_ticks_position('none') # This line is the best part!
axis1.xaxis.set_ticks_position('none')

# I realized that the first xtick is a 0
plt.xticks(xincome[1:])
# place the 0 just like any text since I could not override the formatter
plt.text(0, -.87, '0')

axis1.set_axisbelow(True)
In [11]:
# And just to make sure I can change the font family
plt.text(0, 0, 'Majors With The Highest Earnings', size=20, weight='bold', family='fantasy')
Out[11]:
<matplotlib.text.Text at 0x106097810>

Things missing:

  1. The font family is not it. But I am happy knowing just how to change it.

I like it. On a different subject, I made the code in one cell because you see what happen when I to break it, some times the graph won't appear and others it would simply start over, like in the last one.

Another thing missing:

  1. figure out how to use the cells properly.

sbuj