There are lots of different backends for plotting matplotlib figures in notebooks:
%matplotlib --list
We use notebook
for interactive notebooks, but inline
is better suited for static output.
%matplotlib notebook
#%matplotlib inline
After choosing a backend there are a range of different choices for image format if you use inline
:
# The InlineBackend only appears *after* matplotlib magic
%config InlineBackend
Normally a vector format like svg
or pdf
is preferred over a raster format.
Otherwise lossless compression using png
is preferred over the lossy jpeg
format.
%config InlineBackend.figure_format = 'svg'
We import numpy
and matplotlib
to plot a simple figure:
import numpy as np
import matplotlib.pyplot as plt
After doing so we can change the canvas size, which inconveniently is only expressible in absolute units (inches!?). Ideally we would set this to be the width of the text, for this notebook we guessed.
If a raster format was chosen, the dpi
(dots/pixels per inch, yes still inches...) parameter can be changed to producing higher resolution figures.
This also has the effect inside a notbook of increasing the figure size too.
plt.rcParams['figure.figsize'] = (8,6)
plt.rcParams['figure.dpi'] = 120
We can now produce a plot in our notebook. Notice that we are using matplotlib's "object oriented" interface as opposed to the "state machine" interface. This is very powerful and well worth learning how to use, if you are not already familiar.
# Do a "Regular" plot
fig, ax = plt.subplots(1, 1)
α = 0
ω = 1
t = np.linspace(0, 2*np.pi, 200)
ax.plot(t, (t**α)*np.sin(ω*t))
ax.set_xlabel('$t$')
ax.set_ylabel('$f$')
ax.set_title(r"$\alpha$ = {}, $\omega$ = {}".format(α, ω))
plt.show()