Learning by cloning with matplotlib -3-

In [1]:
# An article from techpinions.com titled "When Genuine Data Leads to Disingenuous Conclusions"
# shows a graph where they try to show the Android vs iOS market share in the tablet world.
from IPython.core.display import Image
Image(url="http://techpinions.com/wp-content/uploads/2013/10/Screen-Shot-2013-10-25-at-7.10.08-AM.png")
# Image Source: techpinions.com
Out[1]:
In [2]:
# Imports are the same as last time
import matplotlib.pyplot as plt
%matplotlib inline

# I will plot the middle line and then fill the areas above and below with
# a gradient.

# first! the X and Y axis
share = [0, 25, 50, 75, 100]
# I really should start picking graphs without this Quarter in the axis
# I will be lazy again and compose the quarters like this:
quarters = ['Q' + qrt + ' \'' + year for year in ['11','12','13'] for qrt in '1234']
quarters = quarters[1:-2]

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

# set the Y ticks and set the limits of the graph
plt.yticks(share, size=13)
plt.ylim((0, 100))
#plt.xlim((0, len(quarters)))

# set the X ticks
plt.xticks(range(len(quarters)))
axis.set_xticklabels(quarters, ha='center', size=11)
from matplotlib.ticker import FormatStrFormatter
# This is the printf like formatter, I am using it to display the percent sign
currency_format = FormatStrFormatter('%d%%')
# ant tell it where to use apply it
axis.yaxis.set_major_formatter(currency_format)

# clean the spines
for _spine in axis.spines.values():
    _spine.set_visible(False)

axis.yaxis.set_ticks_position('none') # my good friend ticks position
axis.xaxis.set_ticks_position('none')

# For this graph I don't have the exact market share value for each quarter,
# but unlike [Exercise 2], this data could be easily guessed.
x_range = range(len(quarters))
ios_share = [60, 68, 53, 56, 58, 38, 35, 37, 29]
andr_share = [ _ios + 0.5 for _ios in ios_share]

# simple plot with color and label
plt.plot(x_range, ios_share, color='#247cdf', label='Apple', linewidth=9.5)
plt.plot(x_range, andr_share, color='#00aa26', label='Android', linewidth=10)

# The linewidth of the plot will be reflected in the legend. In this case, since
# the graph is filled with color, the width will not be noticeable.
# This width with the handlelength parameter of the legend will yield squares in the
# legend.

# The legend
# using a bbox to set a position and a columnspacing to separate the entries
# bbox.position, trial and error
plt.legend(frameon=False, fontsize=13, ncol=2, bbox_to_anchor = (0.75, 1.15),
           columnspacing=14, handlelength=0.5)

# add padding to the x and y
axis.yaxis.set_tick_params(pad=12)
axis.xaxis.set_tick_params(pad=10)

# place the copyright mark using LaTeX
plt.text(7.1, -8, r'$\copyright$ Tech.pinions', size=10) # position, trial and error

# now the filling, first fill from 0 and second from value to 100
plt.fill_between(x_range, ios_share, color='#247cdf')
plt.fill_between(x_range, andr_share, [100 for _100 in x_range], color='#00aa26')

plt.show()

Things missing:

  1. Exact values are missing.
  2. The gradient is difficult to get.

Still this thing missing:

  1. figure out how to use the cells properly in the IPython notebook.

Answers for last missing things

  1. I found here a table with parameters to adjust padding between elements of the legend. Some of them may not work if the mode of the legend is set to 'expand'

sbuj