matplotlib - Create "The Economist" style graphs from python -
using python , marplotlib , tool seaborn, i'd create graph 1 economist (because think style great.)

it's time series graph , key things i'd reproduce horizontal grid lines labels matched lower horizontal axis tick marks. different colored labels @ either end of grid lines bonus, corresponding titles (left , right justified). annotations double bonus.
i tried make using seaborn, not first step.
not perfect (i've haven't had long play it), give idea of kind of matplotlib methods need use customize plot way want, there's code below.
note fine-tune plot hard keep content , presentation separate (you may have manually set tick labels , like, won't work automatically if change data). the economist's graphics people because seem have got top left hand tick label wrong (280 should 260).
# -*- coding: utf-8 -*- import numpy np import matplotlib.pyplot plt import matplotlib.dates mdates import matplotlib.ticker ticker datetime import datetime # load in sample data bond_yields = np.loadtxt('bond_yields.txt', converters={0: mdates.strpdate2num('%y-%m-%d')}, dtype = {'names': ('date', 'bond_yield'), 'formats': (datetime, float)}) bank_deposits = np.loadtxt('bank_deposits.txt', converters={0: mdates.strpdate2num('%y-%m-%d')}, dtype = {'names': ('date', 'bank_deposits'), 'formats': (datetime, float)}) # bond yields line in light blue, bank deposits line in dark red: bond_yield_color = (0.424, 0.153, 0.090) bank_deposits_color = (0.255, 0.627, 0.843) # set figure, , twin x-axis can have 2 different y-axes fig = plt.figure(figsize=(8, 4), frameon=false, facecolor='white') ax1 = fig.add_subplot(111) ax2 = ax1.twinx() # make sure gridlines don't end on top of plotted data ax1.set_axisbelow(true) ax2.set_axisbelow(true) # light gray, horizontal gridlines ax1.yaxis.grid(true, color='0.65', ls='-', lw=1.5, zorder=0) # plot data l1, = ax1.plot(bank_deposits['date'], bank_deposits['bank_deposits'], c=bank_deposits_color, lw=3.5) l2, = ax2.plot(bond_yields['date'], bond_yields['bond_yield'], c=bond_yield_color, lw=3.5) # set y-tick ranges: chosen ax2 labels match ax1 gridlines ax1.set_yticks(range(120,280,20)) ax2.set_yticks(range(0, 40, 5)) # turn off spines left, top, bottom , right (do twice because of twinning) ax1.spines['left'].set_visible(false) ax1.spines['right'].set_visible(false) ax1.spines['top'].set_visible(false) ax2.spines['left'].set_visible(false) ax2.spines['right'].set_visible(false) ax2.spines['top'].set_visible(false) ax1.spines['bottom'].set_visible(false) ax2.spines['bottom'].set_visible(false) # want ticks on bottom x-axis ax1.xaxis.set_ticks_position('bottom') ax2.xaxis.set_ticks_position('bottom') # remove ticks y-axes ax1.tick_params(axis='y', length=0) ax2.tick_params(axis='y', length=0) # set tick-labels 2 y-axes in appropriate colors tick_label in ax1.yaxis.get_ticklabels(): tick_label.set_fontsize(12) tick_label.set_color(bank_deposits_color) tick_label in ax2.yaxis.get_ticklabels(): tick_label.set_fontsize(12) tick_label.set_color(bond_yield_color) # set x-axis tick marks two-digit years ax1.xaxis.set_major_locator(mdates.yearlocator()) ax1.xaxis.set_major_formatter(mdates.dateformatter('%y')) # tweak x-axis tick label sizes tick in ax1.xaxis.get_major_ticks(): tick.label.set_fontsize(12) tick.label.set_horizontalalignment('center') # lengthen bottom x-ticks , set them dark gray ax1.tick_params(direction='in', axis='x', length=7, color='0.1') # add line legends annotations ax1.annotate(u'private-sector bank deposits, €bn', xy=(0.09, 0.95), xycoords='figure fraction', size=12, color=bank_deposits_color, fontstyle='italic') ax2.annotate(u'ten-year government bond yield, %', xy=(0.6, 0.95), xycoords='figure fraction', size=12, color=bond_yield_color, fontstyle='italic') # add annotation @ date of first bail-out. relpos=(0,0) ensures # label lines on right of vertical line first_bailout_date = datetime.strptime('2010-05-02', '%y-%m-%d') xpos = mdates.date2num(first_bailout_date) ax1.annotate(u'first bail-out', xy=(xpos, 120), xytext=(xpos, 250), color='r', arrowprops=dict(arrowstyle='-', edgecolor='r', ls='dashed', relpos=(0,0)), fontsize=9, fontstyle='italic') fig.savefig('fig.png', facecolor=fig.get_facecolor(), edgecolor='none') 
Comments
Post a Comment