Matplotlib Cheat Sheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8
At a glance
Powered by AI
The key takeaways are that there are two ways to use Matplotlib - a non-pythonic way using global functions and a pythonic object-oriented way, and the main components of a Matplotlib plot are the Figure, Axes and various plot elements.

The two ways of using Matplotlib are the non-pythonic way which relies on global functions and the pythonic object-oriented way which uses explicit Figure and Axes objects.

The main components of a Matplotlib plot are the Figure (top-level container), Axes (container for a specific plot) and various plot elements like lines, text etc. that are added to the Axes.

Introductory Notes: Matplotlib

Preliminaries

Start by importing these Python modules


import numpy as np
import pandas as pd
from pandas import DataFrame, Series
import matplotlib.pyplot as plt
import matplotlib

Which Application Programming Interface?

Matplotlib: intro to the object oriented way


The two worlds of Matplotlib
There are 2 broad ways of using pyplot:
1. The first (and most common) way is not pythonic. It The Figure
relies on global functions to build and display a global Figure is the top-level container for everything on a
figure using matplotlib as a global state machine. canvas. It was obtained from the global Figure factory.
(This is an easy approach for interactive use). fig = plt.figure(num=None, figsize=None,
2. The second way is pythonic and object oriented. You dpi=None, facecolor=None,
obtain an empty Figure from a global factory, and edgecolor=None)
then build the plot explicitly using the methods of the num integer or string identifier of figure
Figure and the classes it contains. (This is the best if num exists, it is selected
approach for programmatic use). if num is None, a new one is allocated
figsize tuple of (width, height) in inches
While these notes focus on second approach, let's begin dpi dots per inch
with a quick look at the first. facecolor background; edgecolor border

Iterating over the open figures


for i in plt.get_fignums():
Using matplotlib in a non-pythonic way fig = plt.figure(i) # get the figure
print (fig.number) # do something

1. Get some (fake) data - monthly time series


Close a figure
x = pd.period_range('1980-01-01',
plt.close(fig.number) # close figure
periods=410, freq='M')
x = x.to_timestamp().to_pydatetime() plt.close() # close the current figure
y = np.random.randn(len(x)).cumsum() plt.close(i) # close figure numbered i
plt.close(name) # close figure by str name
plt.close('all')# close all figures
2. Plot the data
plt.plot(x, y, label='FDI') An Axes or Subplot (a subclass of Axes)
An Axes is a container class for a specific plot. A figure
3. Add your labels and pretty-up the plot may contain many Axes and/or Subplots. Subplots are
plt.title('Fake Data Index') laid out in a grid within the Figure. Axes can be placed
plt.xlabel('Date') anywhere on the Figure. There are a number of
plt.ylabel('Index') methods that yield an Axes, including:
plt.grid(True)
ax = fig.add_subplot(2,2,1) # row-col-num
plt.figtext(0.995, 0.01, 'Footnote',
ha='right', va='bottom') ax = fig.add_axes([0.1,0.1,0.8,0.8])
plt.legend(loc='best', framealpha=0.5,
prop={'size':'small'}) All at once
plt.tight_layout(pad=1) We can use the subplots factory to get the Figure and all
plt.gcf().set_size_inches(8, 4) the desired Axes at once.
fig, ax = plt.subplots()
4. SAVE the figure fig,(ax1,ax2,ax3) = plt.subplots(nrows=3,
plt.savefig('filename.png') ncols=1, sharex=True, figsize=(8,4))

5. Finally, close the figure Iterating the Axes within a Figure


plt.close() for ax in fig.get_axes():
pass # do something
Alternatively, SHOW the figure
With IPython, follow steps 1 to 3 above then Remove an Axes from a Figure
plt.show() # Note: also closes the figure fig.delaxes(ax)

Version 3 May 2015 - [Draft Mark Graph mark dot the dot graph at gmail dot com @Mark_Graph on twitter]
1!
!
Line plots using ax.plot() Scatter plots using ax.scatter()

Single plot constructed with Figure and Axes A simple scatter plot
# --- get the data x = np.random.randn(100)
x = np.linspace(0, 16, 800) y = x + np.random.randn(100) + 10
y = np.sin(x) fig, ax = plt.subplots(figsize=(8, 3))
# --- get an empty figure and add an Axes ax.scatter(x, y, alpha=0.5, color='orchid')
fig = plt.figure(figsize=(8,4)) fig.suptitle('Example Scatter Plot')
ax = fig.add_subplot(1,1,1) # row-col-num fig.tight_layout(pad=2);
# --- line plot data on the Axes ax.grid(True)
ax.plot(x, y, 'b-', linewidth=2, fig.savefig('filename1.png', dpi=125)
label=r'$y=\sin(x)$')
# --- add title, labels and legend, etc.
ax.set_ylabel(r'$y$', fontsize=16);
ax.set_xlabel(r'$x$', fontsize=16)
ax.legend(loc='best')
ax.grid(True)
fig.suptitle('The Sine Wave')
fig.tight_layout(pad=1)
fig.savefig('filename.png', dpi=125)

Add a regression line (using statsmodels)


import statsmodels.api as sm
x = sm.add_constant(x) # intercept
# Model: y ~ x + c
model = sm.OLS(y, x)
fitted = model.fit()
x_pred = np.linspace(x.min(), x.max(), 50)
x_pred2 = sm.add_constant(x_pred)
y_pred = fitted.predict(x_pred2)
Multiple lines with markers on a line plot ax.plot(x_pred, y_pred, '-',
# --- get the Figure and Axes all at once color='darkorchid', linewidth=2)
fig, ax = plt.subplots(figsize=(8,4)) fig.savefig('filename2.png', dpi=125)
# --- plot some lines
N = 8 # the number of lines we will plot
styles = ['-', '--', '-.', ':']
markers = list('+ox^psDv')
x = np.linspace(0, 100, 20)
for i in range(N): # add line-by-line
y = x + x/5*i + i
s = styles[i % len(styles)]
m = markers[i % len(markers)]
ax.plot(x, y,
label='Line '+str(i+1)+' '+s+m,
marker=m, linewidth=2, linestyle=s)
# --- add grid, legend, title and save
ax.grid(True)
ax.legend(loc='best', prop={'size':'large'})
fig.suptitle('A Simple Line Plot')
fig.savefig('filename.png', dpi=125)

Version 3 May 2015 - [Draft Mark Graph mark dot the dot graph at gmail dot com @Mark_Graph on twitter]
2!
!
Add confidence bands for the regression line Changing the marker size and colour
y_hat = fitted.predict(x) N = 100
y_err = y - y_hat x = np.random.rand(N)
mean_x = x.T[1].mean() y = np.random.rand(N)
n = len(x) size = ((np.random.rand(N) + 1) * 8) ** 2
dof = n - fitted.df_model - 1 colours = np.random.rand(N)
from scipy import stats fig, ax = plt.subplots(figsize=(8,4))
t = stats.t.ppf(1-0.025, df=dof) # 2-tail l = ax.scatter(x, y, s=size, c=colours)
s_err = np.sum(np.power(y_err, 2)) fig.colorbar(l)
conf = t * np.sqrt((s_err/(n-2))*(1.0/n + ax.set_xlim((-0.05, 1.05))
(np.power((x_pred-mean_x),2) / ax.set_ylim((-0.05, 1.05))
((np.sum(np.power(x_pred,2))) - fig.suptitle('Dramatic Scatter Plot')
n*(np.power(mean_x,2)))))) fig.tight_layout(pad=2);
upper = y_pred + abs(conf) ax.grid(True)
lower = y_pred - abs(conf) fig.savefig('filename.png', dpi=125)
ax.fill_between(x_pred, lower, upper, Note: matplotlib has a huge range of colour maps in
color='#888888', alpha=0.3) addition to the default used here.
fig.savefig('filename3.png', dpi=125)

Changing the marker symbol


fig, ax = plt.subplots(figsize=(8,5))
markers = list('ov^<>12348sphHdD+x*|_')
Add a prediction interval for the regression line N = 10
from statsmodels.sandbox.regression.predstd\ for i, m in enumerate(markers):
import wls_prediction_std x = np.arange(N)
sdev, lower, upper = y = np.repeat(i+1, N)
wls_prediction_std(fitted, ax.scatter(x, y, marker=m, label=m,
exog=x_pred2, alpha=0.05) s=50, c='cornflowerblue')
ax.fill_between(x_pred, lower, upper,
color='#888888', alpha=0.1) ax.set_xlim((-1,N))
fig.savefig('filename4.png', dpi=125) ax.set_ylim((0,len(markers)+1))
ax.legend(loc='upper left', ncol=3,
prop={'size':'xx-large'},
shadow=True, title='Marker Legend')
ax.get_legend().get_title().set_color("red")
fig.suptitle('Markers ' +
'(with an oversized legend)')
fig.tight_layout(pad=2);
fig.savefig('filename.png', dpi=125)

Note: The confidence interval relates to the location of


the regression line. The predication interval relates to
the location of data points around the regression line.

Version 3 May 2015 - [Draft Mark Graph mark dot the dot graph at gmail dot com @Mark_Graph on twitter]
3!
!
Bar plots using ax.bar() and ax.barh()

A simple bar chart


The bars in a bar-plot are placed to the right of the bar x-
axis location by default. Centred labels require a little
jiggling with the bar and label positions.
# --- get the data
N = 5
labels = list('ABCDEFGHIJKLM'[0:N])
Stacked bar
data = np.array(range(N)) +
np.random.rand(N) # --- get some data
# --- plot the data alphas = np.array( [23, 44, 52, 32] )
fig, ax = plt.subplots(figsize=(8, 3.5)) betas = np.array( [38, 49, 32, 61] )
width = 0.8; labels = ['Sydney', 'Melb', 'Canb', 'Bris']
tickLocations = np.arange(N) # --- the plot
rectLocations = tickLocations-(width/2.0) fig, ax = plt.subplots(figsize=(8, 3.5))
ax.bar(rectLocations, data, width, width = 0.8;
color='wheat', xlocations=np.array(range(len(alphas)+2))
edgecolor='#8B7E66', linewidth=4.0) adjlocs = xlocations[1:-1] - width/2.0
# --- pretty-up the plot ax.bar(adjlocs, alphas, width,
ax.set_xticks(ticks= tickLocations) label='alpha', color='tan')
ax.set_xticklabels(labels) ax.bar(adjlocs, betas, width,
ax.set_xlim(min(tickLocations)-0.6, label='beta', color='wheat',
max(tickLocations)+0.6) bottom=alphas)
ax.set_yticks(range(N)[1:]) # --- pretty-up and save
ax.set_ylim((0,N)) ax.set_xticks(ticks=xlocations[1:-1])
ax.yaxis.grid(True) ax.set_xticklabels(labels)
# --- title and save ax.yaxis.grid(True)
fig.suptitle("Bar Plot with " + ax.legend(loc='best', prop={'size':'small'})
"Oversized Edges") fig.suptitle("Stacked Nonsense")
fig.tight_layout(pad=2) fig.tight_layout(pad=2)
fig.savefig('filename.png', dpi=125) fig.savefig('filename.png', dpi=125)

Side by side bar chart Horizontal bar charts


# --- get the data Just as tick placement needs to be managed with
before = np.array([10, 11, 9, 12]) vertical bars; so with horizontal bars (which are above
after = np.array([11, 12, 8, 17]) the y-tick mark)
labels=['Group '+x for x in list('ABCD')] labels = ['Males', 'Females', 'Persons']
# --- the plot left then right data = [6.3, 7.2, 6.8]
fig, ax = plt.subplots(figsize=(8, 3.5)) width = 0.8
width = 0.4 # bar width yTickPos = np.arange(len(data))
xlocs = np.arange(len(before)) yBarPos = yTickPos - (width/2.0)
ax.bar(xlocs-width, before, width, fig, ax = plt.subplots(figsize=(8, 3.5))
color='wheat', label='Males') ax.barh(yBarPos,data,width,color='wheat')
ax.bar(xlocs, after, width, ax.set_yticks(ticks= yTickPos)
color='#8B7E66', label='Females') ax.set_yticklabels(labels)
# --- labels, grids and title, then save ax.set_ylim((min(yTickPos)-0.6,
ax.set_xticks(ticks=range(len(before))) max(yTickPos)+0.6))
ax.set_xticklabels(labels) ax.xaxis.grid(True)
ax.yaxis.grid(True) ax.set_ylabel('Gender');
ax.legend(loc='best') ax.set_xlabel('Rate (Percent)')
ax.set_ylabel('Mean Group Result') fig.suptitle("Horizontal Nonsense")
fig.suptitle('Group Results by Gender') fig.tight_layout(pad=2)
fig.tight_layout(pad=1) fig.savefig('filename.png', dpi=125)
fig.savefig('filename.png', dpi=125)

Version 3 May 2015 - [Draft Mark Graph mark dot the dot graph at gmail dot com @Mark_Graph on twitter]
4!
!
Pie Chart using ax.pie()
Plot spines
As nice as pie
# --- get some data Hiding the top and right spines
data = np.array([5,3,4,6])
x = np.linspace(-np.pi, np.pi, 800)
labels = ['bats', 'cats', 'gnats', 'rats']
y = np.sin(x)
explode = (0, 0.1, 0, 0) # explode cats
fig, ax = plt.subplots(figsize=(8, 4))
colrs=['khaki', 'goldenrod', 'tan', 'wheat']
ax.plot(x, y, label='Sine', color='red')
# --- the plot
ax.set_axis_bgcolor('#e5e5e5')
fig, ax = plt.subplots(figsize=(8, 3.5))
ax.spines['right'].set_color('none')
ax.pie(data, explode=explode,
ax.spines['top'].set_color('none')
labels=labels, autopct='%1.1f%%',
ax.spines['left'].set_position(
startangle=270, colors=colrs)
('outward',10))
ax.axis('equal') # keep it a circle
ax.spines['bottom'].set_position(
# --- tidy-up and save
('outward',10))
fig.suptitle("Delicious Pie Ingredients")
ax.xaxis.set_ticks_position('bottom')
fig.savefig('filename.png', dpi=125)
ax.yaxis.set_ticks_position('left')
# do the ax.grid() after setting ticks
ax.grid(b=True, which='both',
color='white', linestyle='-',
linewidth=1.5)
ax.set_axisbelow(True)
ax.legend(loc='best', frameon=False)
fig.savefig('filename.png', dpi=125)

Polar using ax.plot()

Polar coordinates
# --- theta
theta = np.linspace(-np.pi, np.pi, 800)
# --- get us a Figure
fig = plt.figure(figsize=(8,4)) Spines in the middle
# --- left hand plot x = np.linspace(-np.pi, np.pi, 800)
ax = fig.add_subplot(1,2,1, polar=True) y = np.sin(x)
r = 3 + np.cos(5*theta) fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(theta, r) ax.plot(x, y, label='Sine')
ax.set_yticks([1,2,3,4]) ax.spines['right'].set_color('none')
# --- right hand plot ax.spines['top'].set_color('none')
ax = fig.add_subplot(1,2,2, polar=True) ax.xaxis.set_ticks_position('bottom')
r = (np.sin(theta)) - (np.cos(10*theta)) ax.spines['bottom'].set_position((
ax.plot(theta, r, color='green') 'data',0))
ax.set_yticks([1,2]) ax.yaxis.set_ticks_position('left')
# --- title, explanatory text and save ax.spines['left'].set_position((
fig.suptitle('Polar Coordinates') 'data',0))
fig.text(x=0.24, y=0.05, ax.grid(b=True, which='both',
s=r'$r = 3 + \cos(5 \theta)$') color='#888888', linestyle='-',
fig.text(x=0.64, y=0.05, linewidth=0.5)
s=r'$r = \sin(\theta) - \cos(10' + fig.suptitle('Sine')
r'\theta)$') fig.savefig('filename.png', dpi=125)
fig.savefig('filename.png', dpi=125)

Version 3 May 2015 - [Draft Mark Graph mark dot the dot graph at gmail dot com @Mark_Graph on twitter]
5!
!
Legend to the right of the plot
N = 5
x = np.arange(N)
fig, ax = plt.subplots(figsize=(8, 3))
for j in range(5):
ax.plot(x, x*(j+1),
label='Line '+str(j))

box = ax.get_position() # Shrink plot


ax.set_position([box.x0, box.y0,
box.width * 0.8, box.height])
ax.legend(bbox_to_anchor=(1, 0.5),
loc='center left') # Put legend
fig.savefig('filename.png', dpi=125)
Legends

Legend within the plot


Use the 'loc' argument to place the legend
N = 5
x = np.arange(N)
fig, ax = plt.subplots(figsize=(8, 3))
for j in range(5):
ax.plot(x, x*(j+1),label='Line'+str(j))

ax.legend(loc='upper left')
fig.savefig('filename.png', dpi=125) Legend below the plot
N = 5
x = np.arange(N)
fig, ax = plt.subplots(figsize=(8, 3))
for j in range(5):
ax.plot(x, x*(j+1),
label='Line '+str(j))

box = ax.get_position()
ax.set_position([box.x0,
box.y0 + box.height * 0.15,
box.width, box.height * 0.85])
ax.legend(bbox_to_anchor=(0.5, -0.075),
Legend slightly outside of the plot loc='upper center', ncol=N)
N = 5 fig.savefig('filename.png', dpi=125)
x = np.arange(N)
fig, ax = plt.subplots(figsize=(8, 3))
for j in range(5):
ax.plot(x, x*(j+1),
label='Line '+str(j))

ax.legend(bbox_to_anchor=(1.1, 1.05))
fig.savefig('filename.png', dpi=125)

Version 3 May 2015 - [Draft Mark Graph mark dot the dot graph at gmail dot com @Mark_Graph on twitter]
6!
!
Using GridSpec layouts (like list slicing)
Multiple plots on a canvas import matplotlib.gridspec as gs
gs = gs.GridSpec(3, 3) # nrows, ncols
Using Axes to place a plot within a plot fig = plt.figure(figsize=(8,4))
fig.text(x=0.01, y=0.01, s='Figure',
fig = plt.figure(figsize=(8,4)) color='#888888', ha='left',
fig.text(x=0.01, y=0.01, s='Figure', va='bottom', fontsize=20)
color='#888888', ha='left', ax1 = fig.add_subplot(gs[0, :]) # row,col
va='bottom', fontsize=20) ax1.text(x=0.2,y=0.2,s='0, :', color='b')
# --- Main Axes ax2 = fig.add_subplot(gs[1,:-1])
ax = fig.add_axes([0.1,0.1,0.8,0.8]) ax2.text(x=0.2,y=0.2,s='1, :-1', color='b')
ax.text(x=0.01, y=0.01, s='Main Axes', ax3 = fig.add_subplot(gs[1:, -1])
color='red', ha='left', va='bottom', ax3.text(x=0.2,y=0.2, s='1:, -1', color='b')
fontsize=20) ax4 = fig.add_subplot(gs[-1,0])
ax.set_xticks([]); ax.set_yticks([]) ax4.text(x=0.2,y=0.2, s='-1, :0', color='b')
# --- Insert Axes ax5 = fig.add_subplot(gs[-1,-2])
ax= fig.add_axes([0.15,0.65,0.2,0.2]) ax5.text(x=0.2,y=0.2, s='-1,:-2', color='b')
ax.text(x=0.01, y=0.01, s='Insert Axes', for a in fig.get_axes():
color='blue', ha='left', va='bottom', a.set_xticks([])
fontsize=20) a.set_yticks([])
ax.set_xticks([]); ax.set_yticks([])
fig.suptitle('An Axes within an Axes') fig.suptitle('GridSpec Layout')
fig.savefig('filename.png', dpi=125) fig.savefig('filename.png', dpi=125)

Simple subplot grid layouts


fig = plt.figure(figsize=(8,4))
fig.text(x=0.01, y=0.01, s='Figure', Plotting defaults
color='#888888', ha='left',
va='bottom', fontsize=20)
Configuration files
for i in range(4): Matplotlib uses configuration files to set the defaults. So
# fig.add_subplot(nrows, ncols, num) that you can edit it, the location of the configuration file
ax = fig.add_subplot(2, 2, i+1) can be found as follows:
ax.text(x=0.01, y=0.01, print (matplotlib.matplotlib_fname())
s='Subplot 2 2 '+str(i+1),
color='red', ha='left',
va='bottom', fontsize=20) Configuration settings
ax.set_xticks([]); ax.set_yticks([]) The current configuration settings
print (matplotlib.rcParams)
ax.set_xticks([]); ax.set_yticks([])
fig.suptitle('Subplots') Change the default settings
fig.savefig('filename.png', dpi=125) plt.rc('figure', figsize=(8,4), dpi=125,
facecolor='white', edgecolor='white')
plt.rc('axes', facecolor='#e5e5e5',
grid=True, linewidth=1.0,
axisbelow=True)
plt.rc('grid', color='white', linestyle='-',
linewidth=2.0, alpha=1.0)
plt.rc('xtick', direction='out')
plt.rc('ytick', direction='out')
plt.rc('legend', loc='best')

Version 3 May 2015 - [Draft Mark Graph mark dot the dot graph at gmail dot com @Mark_Graph on twitter]
7!
!
Cautionary notes

This cheat sheet was cobbled together by bots roaming


the dark recesses of the Internet seeking ursine and
pythonic myths. There is no guarantee the narratives
were captured and transcribed accurately. You use
these notes at your own risk. You have been warned.

Version 3 May 2015 - [Draft Mark Graph mark dot the dot graph at gmail dot com @Mark_Graph on twitter]
8!
!

You might also like