Introductory Notes: Matplotlib: Preliminaries
Introductory Notes: Matplotlib: Preliminaries
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
close figure
the current figure
figure numbered i
figure by str name
all figures
Version 3 May 2015 - [Draft Mark Graph mark dot the dot graph at gmail dot com @Mark_Graph on twitter]
1!
Version 3 May 2015 - [Draft Mark Graph mark dot the dot graph at gmail dot com @Mark_Graph on twitter]
2!
Version 3 May 2015 - [Draft Mark Graph mark dot the dot graph at gmail dot com @Mark_Graph on twitter]
3!
Stacked bar
# --- get some data
alphas = np.array( [23, 44, 52, 32] )
betas = np.array( [38, 49, 32, 61] )
labels = ['Sydney', 'Melb', 'Canb', 'Bris']
# --- the plot
fig, ax = plt.subplots(figsize=(8, 3.5))
width = 0.8;
xlocations=np.array(range(len(alphas)+2))
adjlocs = xlocations[1:-1] - width/2.0
ax.bar(adjlocs, alphas, width,
label='alpha', color='tan')
ax.bar(adjlocs, betas, width,
label='beta', color='wheat',
bottom=alphas)
# --- pretty-up and save
ax.set_xticks(ticks=xlocations[1:-1])
ax.set_xticklabels(labels)
ax.yaxis.grid(True)
ax.legend(loc='best', prop={'size':'small'})
fig.suptitle("Stacked Nonsense")
fig.tight_layout(pad=2)
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!
Version 3 May 2015 - [Draft Mark Graph mark dot the dot graph at gmail dot com @Mark_Graph on twitter]
5!
Legends
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!
Plotting defaults
Configuration files
Matplotlib uses configuration files to set the defaults. So
that you can edit it, the location of the configuration file
can be found as follows:
print (matplotlib.matplotlib_fname())
Configuration settings
The current configuration settings
print (matplotlib.rcParams)
Change the default settings
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!