Pages

Sunday, 12 June 2016

Plotting from pandas

Today I want to talk a little about plotting from pandas.

I will begin with my markgraph.mpstyle style-sheet. Using a style sheet allows me to maintain a consistent look and feel. In this stylesheet I have opted for a plain white background, minimal axes and very feint grid lines. I buy the Edward Tufte philosophy of reducing the non-data ink as much as possible.
# Mark Graph Style Sheet

font.family: sans-serif
font.style: normal
font.variant: normal
font.weight: medium
font.stretch: normal
font.sans-serif: Helvetica, Arial, Avant Garde, sans-serif
font.size: 14.0

lines.linewidth: 2.0
lines.solid_capstyle: butt

legend.fancybox: true
legend.fontsize: x-small
legend.framealpha: 0.5

axes.prop_cycle: cycler('color', ['ef5962', '5a9dd2', '7dc16d', 'f9a560', '9d69aa', 'cd6f5a', 'd680b3', '737373'])
axes.facecolor: white
axes.titlesize: large  # fontsize of the axes title
axes.labelsize: medium # x and y label size
axes.labelcolor: 111111
axes.axisbelow: true
axes.grid: true
axes.edgecolor: white
axes.linewidth: 0.01

patch.edgecolor: white
patch.linewidth: 0.01

svg.fonttype: path

grid.linestyle: -
grid.linewidth: 0.5
grid.color: e7e7e7

xtick.major.size: 0
xtick.minor.size: 0
xtick.labelsize: small
xtick.color: 333333
ytick.major.size: 0
ytick.minor.size: 0
ytick.labelsize: small
ytick.color: 333333

figure.figsize: 8, 4 # inches
figure.facecolor: white

text.color: black

savefig.edgecolor: white
savefig.facecolor: white

Once a style-sheet is i place (typically saved in the directory I do my work), creating a new plot is easy. I use a recipe like the following to create my plots.
# --- pandas  and numpy initialisation
import pandas as pd
import numpy as np

# --- matplotlib initialisation
import matplotlib.pyplot as plt
plt.style.use('markgraph.mplstyle')
 
# --- create some fake data
df = pd.DataFrame(np.random.randn(100, 6)).cumsum()
df.columns = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta']

# --- plot the data
ax = df.plot(kind='line') # required
ax.set_ylabel('Index') # optional
ax.set_xlabel('Days since inception') # optional
ax.xaxis.set_ticks([0, 20, 40, 60, 80, 100]) # optional

ax.axhline(0, color='#AAAAAA', linestyle='-', linewidth=0.5) # optional

fig = ax.get_figure() # required

fig.suptitle('Fake Data', linespacing=1.2) # optional
fig.tight_layout() # advisable

# - put the legend above - optional - can be fiddly to get right
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width, box.height * 0.88])
ax.legend(bbox_to_anchor=(0.5, 1.13), loc='upper center', ncol=6, numpoints=1)

# - add a footnote - optional
fig.text(0.99, 0.01, 'pandascodesnippets.blogspot.com.au', ha='right', va='bottom',
        fontsize='x-small', fontstyle='italic', color='#999999')

# - and save
fig.savefig('example-chart.png', dpi=125) # required

Which yields the following chart (or something like it).


If you don't have the time to play with your own style-sheet, you can use one of the many built-in style sheets. For example, if I change the matplotlib initialisation part of my code, I can use the built-in style plt.style.use('ggplot') to get ...


There are a host of other built-in styles, including: 'bmh', 'classic', 'dark_background', ' fivethirtyeight', and 'grayscale'. For a complete list, check out this site. My experience has been patchy when it comes to getting the built-in style-sheets to deliver everything I like.

No comments:

Post a Comment