matplotlib
: the ‘big beast’ of visualisation in Python. Similar to MATLAB. Highly customisable. Very complex.seaborn
: a layer that sits over top of matplotlib
and makes it easier to produce good-quality graphics.bokeh
: web-based visualisation tool that can integrate with Jupyter or output to static HTML files.plotly
: another web-based visualisation tool that can integrate with Jupyter.Designed to provide ggplot-like quality output using matplotlib:
Seaborn ‘themes’ act as shortcuts for setting multiple matplotlib parameters:
Seaborn Command | Accomplishes |
---|---|
set_theme(...) |
Set multiple theme parameters in one step. |
axes_style(...) |
Return a parameter dict for the aesthetic style of the plots. |
set_style(...) |
Set the aesthetic style of the plots. |
plotting_context(...) |
Return a parameter dict to scale elements of the figure. |
set_context(...) |
Set the plotting context parameters. |
You can also access:
sns.color_palette(...)
and set using sns.set_palette(...)
.darkgrid
, whitegrid
, dark
, white
, ticks
.There are multiple ways to access/write elements of a plot:
plt.gcf()
(get current figure) or upon creation (e.g. f, ax = plt.subplots(1,1)
or f = plt.figure()
).plt.gca()
(get current axes) or upon creation (e.g. f, ax = plt.subplots(1,1)
or ax = f.add_subplot(1,1,1)
).Annotations, artists, and other features are typically written into the axes using the coordinate space of the figure (e.g. decimal degrees for lat/long, metres for BNG, etc.).
This ‘feature’ is less well-developed but does work:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = plt.axes(projection='3d')
# OR
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# THEN
ax.contour3D(X, Y, Z, ...)
ax.plot_surface(x, y, z, ...)
ax.plot3D(xline, yline, zline, ...)
ax.scatter3D(x, y, z, ...)
# ax.plot_surface and ax.plot_wire also give you 3D renderings
You can then set the elevation and azimuth using: ax.view_init(<elevation>, <azimuth>)
.
Straightforward via save figure function, but lots of options!
plt.savefig(fname, dpi=None, facecolor='w', edgecolor='w',
orientation='portrait', papertype=None, format=None,
transparent=False, bbox_inches=None, pad_inches=0.1,
frameon=None, metadata=None)
The format can be largely determined by the file extension in the fname
(file name) and the supported formats depends on what you’ve installed! You can find out what’s available to you using: plt.gcf().canvas.get_supported_filetypes()
.
By default, Jupyter’s output is static matplotlib, but we can extend this in three ways:
%matplotlib widget
(declare this at the top of your notebook).ipywidgets
(import interact
and related libs as needed).bokeh
, plotly
, altair
/vega
, holoviews
, or even d3
(format may be very, very different from what you are ‘used to’ in Python).Taking an example from Dani’s work:
Plots built on top of matploblib can, to some extent, be automated using functions. For example, to draw circles and place text:
def circle(ax, x, y, radius=0.15):
from matplotlib.patches import Circle
from matplotlib.patheffects import withStroke
circle = Circle((x, y), radius, clip_on=False, zorder=10,
linewidth=1, edgecolor='black',
facecolor=(0, 0, 0, .0125),
path_effects=[withStroke(linewidth=5,
foreground='w')])
ax.add_artist(circle)
def text(ax, x, y, text):
ax.text(x, y, text, backgroundcolor="white",
ha='center', va='top', weight='bold', color='blue')
Click to cycle through the examples:
Visualising Data • Jon Reades