%matplotlib inline import matplotlib.pyplot as plt fig, ax = plt.subplots()
%matplotlib inline
is a jupyter Notebook-specific so-called magic statement
which instructs the Notebook to visualize the upcoming figure
material within the Notebook cells. This first statement is thus
not a python statement and should not be included if you
run python outside a jupyter Notebook.
import matplotlib.pyplot as plt
is a
python statement that acts to import the standard python library,
Matplotlib, so that you can use its
functionality in the current jupyter Notebook session. Importing
python libraries into python code is a standard first action taken in almost
any python program. We shall return to that later. For now it suffices
to understand that the matplotlib python library has become available
to your session under the name plt
.
fig, ax = plt.subplots()
now calls a
python function subplots
from the matplotlib
library. The function returns two python objects which are assigned to
the variables fig
and ax
. Wow take a deep
breath. You have now been introduced to a great number of programming
terms (library, function, objects, assignment, variable). It may seem
overwhelming at this point, but soon you will learn the meaning of all
this. For now, type the three lines into a Notebook cell and hit
SHIFT-ENTER
. This causes the Notebook to produce this figure:
fig
and ax
. Python
objects are programming tools that contain data and means to manipulate
the data. The
object fig
contains among other things information on
the size of the canvas (i.e. the paper or the background) for the drawing. We
did not specify that when creating fig
, so it must have
some default value. Likewise, ax
contains among other
things information about a set of -axes and where on the canvas
these axes are to be plotted. Those are parts of the data
within the objects. The objects also contain methods to manipulate
the data. Invoking e.g.:
ax.plot(1, 3, marker='s') fig
SHIFT_ENTER
in the jupyter Notebook (in the
future, this jupyter Notebook command will be assumed issued without
explicit mention herein) you obtain:
ax.plot(1, 3, marker='s')
means
that a function is called. The function is the
plot
-method on the ax
-object. When
given two values, here and , as the first two arguments, it
will plot a marker at the position . Owing to the third
argument, marker='s'
, the marker becomes a square.
The second line,
fig
, is a python statement that outputs a reference
to the fig
object. In other environments for running
python, this causes a bit of gibberish to be output, but in our
context, the jupyter Notebook picks up this output and makes sure that
an updated version of the figure is drawn as illustrated above. If you
use a different framework other than the jupyter Notebook, you might have to
write fig.show()
to have the figure shown.
ax.plot(1, 2, marker='>', color='red') ax.plot(3, 2, marker='o', color='orange') fig
axis
-object has other methods than that used for
plotting single symbols. You may for instance decide the extent of the
- and -axes yourself with these python statements:
ax.set_xlim([-0.5, 3.5]) ax.set_ylim([-0.2, 3.2]) fig
set_xlim
- and
set_ylim
-methods on the axis specified by
ax
. This time, the arguments are so-called
lists, specifying the lower and upper limits wanted. You get
this output:
plot
method is in fact meant for making entire
curves, not just for plotting single points. You specify all
the - and all the -values of the points on the curve
as the two first arguments to
plot
. A vertical line from to
may e.g. be plotted like this:
ax.plot([0, 0], [1, 3], color='black') fig
ax.plot([1, 1, 2], [1, 0, 0], color='blue') ax.plot([2.5, 3, 2.75, 2.5], [0, 0, 0.35,0], color='red') fig
fig.tight_layout() fig.savefig('nice_fig.png')
tight_layout
method makes sure that axes labels are
not cropped in the final figure (don't ask why the user must remember
this himself/herself?). The savefig
does the expected
thing and produces a png-file:
Choose which booklet to go to: