Commit 96f3b243 authored by Michiel Cottaar's avatar Michiel Cottaar Committed by Michiel Cottaar
Browse files

Explain plt.getp

parent e8296319
%% Cell type:markdown id:christian-smart tags:
%% Cell type:markdown id:5567ba9e tags:
# Matplotlib tutorial
The main plotting library in python is `matplotlib`.
It provides a simple interface to just explore the data,
......@@ -37,102 +37,102 @@
<a class="anchor" id="basic-plotting-commands"></a>
## Basic plotting commands
Let's start with the basic imports:
%% Cell type:code id:quick-postage tags:
%% Cell type:code id:3917392c tags:
``` python
```
import matplotlib.pyplot as plt
import numpy as np
```
%% Cell type:markdown id:prescribed-writing tags:
%% Cell type:markdown id:76688c00 tags:
<a class="anchor" id="line"></a>
### Line plots
A basic lineplot can be made just by calling `plt.plot`:
%% Cell type:code id:turkish-marsh tags:
%% Cell type:code id:00d5ff18 tags:
``` python
```
plt.plot([1, 2, 3], [1.3, 4.2, 3.1])
```
%% Cell type:markdown id:compact-modeling tags:
%% Cell type:markdown id:3d0472b7 tags:
To adjust how the line is plotted, check the documentation:
%% Cell type:code id:distinct-coordinate tags:
%% Cell type:code id:38d3f3ab tags:
``` python
```
plt.plot?
```
%% Cell type:markdown id:green-dutch tags:
%% Cell type:markdown id:867ea1f5 tags:
As you can see there are a lot of options.
The ones you will probably use most often are:
- `linestyle`: how the line is plotted (set to '' to omit the line)
- `marker`: how the points are plotted (these are not plotted by default)
- `color`: what color to use (defaults to cycling through a set of 7 colors)
%% Cell type:code id:adjacent-satellite tags:
%% Cell type:code id:c705366a tags:
``` python
```
theta = np.linspace(0, 2 * np.pi, 101)
plt.plot(np.sin(theta), np.cos(theta))
plt.plot([-0.3, 0.3], [0.3, 0.3], marker='o', linestyle='', markersize=20)
plt.plot(0, -0.1, marker='s', color='black')
x = np.linspace(-0.5, 0.5, 5)
plt.plot(x, x ** 2 - 0.5, linestyle='--', marker='+', color='red')
```
%% Cell type:markdown id:external-meaning tags:
%% Cell type:markdown id:f7e493a7 tags:
Because these keywords are so common, you can actually set one or more of them by passing in a string as the third argument.
%% Cell type:code id:simple-korean tags:
%% Cell type:code id:c2b6c5c5 tags:
``` python
```
x = np.linspace(0, 1, 11)
plt.plot(x, x)
plt.plot(x, x ** 2, '--') # sets the linestyle to dashed
plt.plot(x, x ** 3, 's') # sets the marker to square (and turns off the line)
plt.plot(x, x ** 4, '^y:') # sets the marker to triangles (i.e., '^'), linestyle to dotted (i.e., ':'), and the color to yellow (i.e., 'y')
```
%% Cell type:markdown id:pediatric-sullivan tags:
%% Cell type:markdown id:c7dd3aa9 tags:
<a class="anchor" id="scatter"></a>
### Scatter plots
The main extra feature of `plt.scatter` over `plt.plot` is that you can vary the color and size of the points based on some other variable array:
%% Cell type:code id:bright-preparation tags:
%% Cell type:code id:33b81ef4 tags:
``` python
```
x = np.random.rand(30)
y = np.random.rand(30)
plt.scatter(x, y, x * 30, y)
plt.colorbar() # adds a colorbar
```
%% Cell type:markdown id:asian-mailing tags:
%% Cell type:markdown id:22de1aac tags:
The third argument is the variable determining the size, while the fourth argument is the variable setting the color.
<a class="anchor" id="histograms"></a>
### Histograms and bar plots
For a simple histogram you can do this:
%% Cell type:code id:massive-relative tags:
%% Cell type:code id:0c445269 tags:
``` python
```
r = np.random.rand(1000)
n,bins,_ = plt.hist((r-0.5)**2, bins=30)
```
%% Cell type:markdown id:positive-insight tags:
%% Cell type:markdown id:41a54dd8 tags:
where it also returns the number of elements in each bin, as `n`, and
the bin centres, as `bins`.
> The `_` in the third part on the left
......@@ -140,135 +140,135 @@
> of the return structure.
There is also a call for doing bar plots:
%% Cell type:code id:intensive-taste tags:
%% Cell type:code id:9d7c817d tags:
``` python
```
samp1 = r[0:10]
samp2 = r[10:20]
bwidth = 0.3
xcoord = np.arange(10)
plt.bar(xcoord-bwidth, samp1, width=bwidth, color='red', label='Sample 1')
plt.bar(xcoord, samp2, width=bwidth, color='blue', label='Sample 2')
plt.legend(loc='upper left')
```
%% Cell type:markdown id:boolean-metropolitan tags:
%% Cell type:markdown id:eca1cea7 tags:
> If you want more advanced distribution plots beyond a simple histogram, have a look at the seaborn [gallery](https://seaborn.pydata.org/examples/index.html) for (too?) many options.
<a class="anchor" id="error"></a>
### Adding error bars
If your data is not completely perfect and has for some obscure reason some uncertainty associated with it,
you can plot these using `plt.error`:
%% Cell type:code id:basic-cambridge tags:
%% Cell type:code id:939fcf82 tags:
``` python
```
x = np.arange(5)
y1 = [0.3, 0.5, 0.7, 0.1, 0.3]
yerr = [0.12, 0.28, 0.1, 0.25, 0.6]
xerr = 0.3
plt.errorbar(x, y1, yerr, xerr, marker='s', linestyle='')
```
%% Cell type:markdown id:twelve-assist tags:
%% Cell type:markdown id:cb1a8d17 tags:
<a class="anchor" id="shade"></a>
### Shading regions
An area below a plot can be shaded using `plt.fill`
%% Cell type:code id:stuck-teaching tags:
%% Cell type:code id:f7221bc3 tags:
``` python
```
x = np.linspace(0, 2, 100)
plt.fill(x, np.sin(x * np.pi))
```
%% Cell type:markdown id:metric-chemical tags:
%% Cell type:markdown id:86d77cf6 tags:
This can be nicely combined with a polar projection, to create 2D orientation distribution functions:
%% Cell type:code id:democratic-israel tags:
%% Cell type:code id:b96cbc10 tags:
``` python
```
plt.subplot(projection='polar')
theta = np.linspace(0, 2 * np.pi, 100)
plt.fill(theta, np.exp(-2 * np.cos(theta) ** 2))
```
%% Cell type:markdown id:connected-consideration tags:
%% Cell type:markdown id:5a0defe8 tags:
The area between two lines can be shaded using `fill_between`:
%% Cell type:code id:engaged-lottery tags:
%% Cell type:code id:3f11d97b tags:
``` python
```
x = np.linspace(0, 10, 1000)
y = 5 * np.sin(5 * x) + x - 0.1 * x ** 2
yl = x - 0.1 * x ** 2 - 5
yu = yl + 10
plt.plot(x, y, 'r')
plt.fill_between(x, yl, yu)
```
%% Cell type:markdown id:paperback-stylus tags:
%% Cell type:markdown id:aa3fb87b tags:
<a class="anchor" id="image"></a>
### Displaying images
The main command for displaying images is `plt.imshow` (use `plt.pcolor` for cases where you do not have a regular grid)
%% Cell type:code id:acoustic-kitchen tags:
%% Cell type:code id:63f18c75 tags:
``` python
```
import nibabel as nib
import os.path as op
nim = nib.load(op.expandvars('${FSLDIR}/data/standard/MNI152_T1_1mm.nii.gz'), mmap=False)
imdat = nim.get_data().astype(float)
imslc = imdat[:,:,70]
plt.imshow(imslc, cmap=plt.cm.gray)
plt.colorbar()
plt.grid('off')
```
%% Cell type:markdown id:frank-master tags:
%% Cell type:markdown id:be2facc0 tags:
Note that matplotlib will use the **voxel data orientation**, and that
configuring the plot orientation is **your responsibility**. To rotate a
slice, simply transpose the data (`.T`). To invert the data along along an
axis, you don't need to modify the data - simply swap the axis limits around:
%% Cell type:code id:initial-passing tags:
%% Cell type:code id:1a960ddf tags:
``` python
```
plt.imshow(imslc.T, cmap=plt.cm.gray)
plt.xlim(reversed(plt.xlim()))
plt.ylim(reversed(plt.ylim()))
plt.colorbar()
plt.grid('off')
```
%% Cell type:markdown id:specialized-maintenance tags:
%% Cell type:markdown id:070f5772 tags:
> It is easier to produce informative brain images using nilearn or fsleyes
<a class="anchor" id="annotations"></a>
### Adding lines, arrows, and text
Adding horizontal/vertical lines, arrows, and text:
%% Cell type:code id:weighted-publicity tags:
%% Cell type:code id:c7f6c0f6 tags:
``` python
```
plt.axhline(-1) # horizontal line
plt.axvline(1) # vertical line
plt.arrow(0.2, -0.2, 0.2, -0.8, length_includes_head=True, width=0.01)
plt.text(0.5, 0.5, 'middle of the plot', transform=plt.gca().transAxes, ha='center', va='center')
plt.annotate("line crossing", (1, -1), (0.8, -0.8), arrowprops={}) # adds both text and arrow; need to set the arrowprops keyword for the arrow to be plotted
```
%% Cell type:markdown id:manual-bacon tags:
%% Cell type:markdown id:de291180 tags:
By default the locations of the arrows and text will be in data coordinates (i.e., whatever is on the axes),
however you can change that. For example to find the middle of the plot in the last example we use
axes coordinates, which are always (0, 0) in the lower left and (1, 1) in the upper right.
See the matplotlib [transformations tutorial](https://matplotlib.org/stable/tutorials/advanced/transforms_tutorial.html)
......@@ -294,109 +294,135 @@
In that case we want to be more explicit about what sub-plot we want to add the artist to.
We can do this by switching from the "procedural" interface used above to the "object-oriented" interface.
The commands are very similar, we just have to do a little more setup.
For example, the equivalent of `plt.plot([1, 2, 3], [1.3, 4.2, 3.1])` is:
%% Cell type:code id:earned-anaheim tags:
%% Cell type:code id:3d3482ef tags:
``` python
```
fig = plt.figure()
ax = fig.add_subplot()
ax.plot([1, 2, 3], [1.3, 4.2, 3.1])
```
%% Cell type:markdown id:valued-hungary tags:
%% Cell type:markdown id:4923481d tags:
Note that here we explicitly create the figure and add a single sub-plot to the figure.
We then call the `plot` function explicitly on this figure.
The "Axes" object has all of the same plotting command as we used above,
although the commands to adjust the properties of things like the title, x-axis, and y-axis are slighly different.
`plt.getp` gives a helpful summary of the properties of a matplotlib object (and what you might change):
%% Cell type:code id:c0c64d42 tags:
```
plt.getp(ax)
```
%% Cell type:markdown id:76b789ab tags:
When going through this list carefully you might have spotted that the plotted line is stored in the `lines` property.
Let's have a look at this line in more detail
%% Cell type:code id:02f9cb81 tags:
```
plt.getp(ax.lines[0])
```
%% Cell type:markdown id:9a192752 tags:
This shows us all the properties stored about this line,
including its coordinates in many different formats
(`data`, `path`, `xdata`, `ydata`, or `xydata`),
the line style and width (`linestyle`, `linewidth`), `color`, etc.
<a class="anchor" id="subplots"></a>
## Multiple plots (i.e., subplots)
As stated one of the strengths of the object-oriented interface is that it is easier to work with multiple plots.
While we could do this in the procedural interface:
%% Cell type:code id:intensive-bruce tags:
%% Cell type:code id:3cafa27a tags:
``` python
```
plt.subplot(221)
plt.title("Upper left")
plt.subplot(222)
plt.title("Upper right")
plt.subplot(223)
plt.title("Lower left")
plt.subplot(224)
plt.title("Lower right")
```
%% Cell type:markdown id:upset-stanley tags:
%% Cell type:markdown id:51141fee tags:
For such a simple example, this works fine. But for longer examples you would find yourself constantly looking back through the
code to figure out which of the subplots this specific `plt.title` command is affecting.
The recommended way to this instead is:
%% Cell type:code id:frank-treatment tags:
%% Cell type:code id:a7a6b2c7 tags:
``` python
```
fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0, 0].set_title("Upper left")
axes[0, 1].set_title("Upper right")
axes[1, 0].set_title("Lower left")
axes[1, 1].set_title("Lower right")
```
%% Cell type:markdown id:occupational-astronomy tags:
%% Cell type:markdown id:a1ec90e2 tags:
Here we use `plt.subplots`, which creates both a new figure for us and a grid of sub-plots.
The returned `axes` object is in this case a 2x2 array of `Axes` objects, to which we set the title using the normal numpy indexing.
> Seaborn is great for creating grids of closely related plots. Before you spent a lot of time implementing your own have a look if seaborn already has what you want on their [gallery](https://seaborn.pydata.org/examples/index.html)
<a class="anchor" id="layout"></a>
### Adjusting plot layout
The default layout of sub-plots often leads to overlap between the labels/titles of the various subplots (as above) or to excessive amounts of whitespace in between. We can often fix this by just adding `fig.tight_layout` (or `plt.tight_layout`) after making the plot:
%% Cell type:code id:joint-chick tags:
%% Cell type:code id:0a5f85cc tags:
``` python
```
fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0, 0].set_title("Upper left")
axes[0, 1].set_title("Upper right")
axes[1, 0].set_title("Lower left")
axes[1, 1].set_title("Lower right")
fig.tight_layout()
```
%% Cell type:markdown id:illegal-spanish tags:
%% Cell type:markdown id:ab07e5ab tags:
Uncomment `fig.tight_layout` and see how it adjusts the spacings between the plots automatically to reduce the whitespace.
If you want more explicit control, you can use `fig.subplots_adjust` (or `plt.subplots_adjust` to do this for the active figure).
For example, we can remove any whitespace between the plots using:
%% Cell type:code id:accomplished-watershed tags:
%% Cell type:code id:8e420304 tags:
``` python
```
np.random.seed(1)
fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)
for ax in axes.flat:
offset = np.random.rand(2) * 5
ax.scatter(np.random.randn(10) + offset[0], np.random.randn(10) + offset[1])
fig.suptitle("group of plots, sharing x- and y-axes")
fig.subplots_adjust(wspace=0, hspace=0, top=0.9)
```
%% Cell type:markdown id:standing-course tags:
%% Cell type:markdown id:260dc7eb tags:
<a class="anchor" id="grid-spec"></a>
### Advanced grid configurations (GridSpec)
You can create more advanced grid layouts using [GridSpec](https://matplotlib.org/stable/tutorials/intermediate/gridspec.html).
An example taken from that website is:
%% Cell type:code id:silent-voluntary tags:
%% Cell type:code id:f58613d6 tags:
``` python
```
fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(3, 3)
f3_ax1 = fig.add_subplot(gs[0, :])
f3_ax1.set_title('gs[0, :]')
f3_ax2 = fig.add_subplot(gs[1, :-1])
......@@ -407,61 +433,62 @@
f3_ax4.set_title('gs[-1, 0]')
f3_ax5 = fig.add_subplot(gs[-1, -2])
f3_ax5.set_title('gs[-1, -2]')
```
%% Cell type:markdown id:roman-discharge tags:
%% Cell type:markdown id:cf0ef5c8 tags:
<a class="anchor" id="styling"></a>
## Styling your plot
<a class="anchor" id="labels"></a>
### Setting title and labels
You can edit a large number of plot properties by using the `Axes.set_*` interface.
We have already seen several examples of this above, but here is one more:
%% Cell type:code id:english-yahoo tags:
%% Cell type:code id:6f49e003 tags:
``` python
```
fig, axes = plt.subplots()
axes.plot([1, 2, 3], [2.3, 4.1, 0.8])
axes.set_xlabel('xlabel')
axes.set_ylabel('ylabel')
axes.set_title('title')
```
%% Cell type:markdown id:constant-evolution tags:
%% Cell type:markdown id:d1c2e77c tags:
You can also set any of these properties by calling `Axes.set` directly:
%% Cell type:code id:suspended-biography tags:
%% Cell type:code id:5d744cf1 tags:
``` python
```
fig, axes = plt.subplots()
axes.plot([1, 2, 3], [2.3, 4.1, 0.8])
axes.set(
xlabel='xlabel',
ylabel='ylabel',
title='title',
)
```
%% Cell type:markdown id:annual-hundred tags:
%% Cell type:markdown id:9e621844 tags:
> To match the matlab API and save some typing the equivalent commands in the procedural interface do not have the `set_` preset. So, they are `plt.xlabel`, `plt.ylabel`, `plt.title`. This is also true for many of the `set_` commands we will see below.
You can edit the font of the text when setting the label:
You can edit the font of the text when setting the label or after the fact using the object-oriented interface:
%% Cell type:code id:trained-mouse tags:
%% Cell type:code id:389a85ff tags:
``` python
```
fig, axes = plt.subplots()
axes.plot([1, 2, 3], [2.3, 4.1, 0.8])
axes.set_xlabel("xlabel", color='red')
axes.set_ylabel("ylabel", fontsize='larger')
axes.set_ylabel("ylabel")
axes.get_yaxis().get_label().set_fontsize('larger')
```
%% Cell type:markdown id:elementary-mentor tags:
%% Cell type:markdown id:c7612d2c tags:
<a class="anchor" id="axis"></a>
### Editing the x- and y-axis
We can change many of the properties of the x- and y-axis by using `set_` commands.
......@@ -471,13 +498,13 @@
- The text shown for the ticks can be set using `ax.set_xticklabels` (or as a second argument to `plt.xticks`)
- The style of the ticks can be adjusted by looping through the ticks (obtained through `ax.get_xticks` or calling `plt.xticks` without arguments).
For example:
%% Cell type:code id:capital-surgeon tags:
%% Cell type:code id:0b91948e tags:
``` python
```
fig, axes = plt.subplots()
axes.errorbar([0, 1, 2], [0.8, 0.4, -0.2], 0.1, linestyle='-', marker='s')
axes.set_xticks((0, 1, 2))
axes.set_xticklabels(('start', 'middle', 'end'))
for tick in axes.get_xticklabels():
......@@ -489,11 +516,21 @@
axes.set_yticks((0, 0.5, 1))
axes.set_yticklabels(('0', '50%', '100%'))
fig.tight_layout()
```
%% Cell type:markdown id:discrete-hartford tags:
%% Cell type:markdown id:3d5db817 tags:
As illustrated earlier, we can get a more complete list of the things we could change about the x-axis by looking at its properties:
%% Cell type:code id:ca4c208f tags:
```
plt.getp(axes.get_xlabel())
```
%% Cell type:markdown id:61b4f566 tags:
<a class="anchor" id="faq"></a>
## FAQ
<a class="anchor" id="double-image"></a>
### Why am I getting two images?
......@@ -516,28 +553,28 @@
In this notebook we were using the `inline` backend, which is the default when running in a notebook.
While very robust, this backend has the disadvantage that it only produces static plots.
We could have had interactive plots if only we had changed backends to `nbagg`.
You can change backends in the IPython terminal/notebook using:
%% Cell type:code id:fifty-relaxation tags:
%% Cell type:code id:33900afe tags:
``` python
```
%matplotlib nbagg
```
%% Cell type:markdown id:fifty-sunglasses tags:
%% Cell type:markdown id:7273f530 tags:
> If you are using Jupyterlab (new version of the jupyter notebook) the `nbagg` backend will not work. Instead you will have to install `ipympl` and then use the `widgets` backend to get an interactive backend (this also works in the old notebooks).
In python scripts, this will give you a syntax error and you should instead use:
%% Cell type:code id:underlying-dealer tags:
%% Cell type:code id:b50352a0 tags:
``` python
```
import matplotlib
matplotlib.use("osx")
```
%% Cell type:markdown id:general-subscriber tags:
%% Cell type:markdown id:2a436f07 tags:
Usually, the default backend will be fine, so you will not have to set it.
Note that setting it explicitly will make your script less portable.
......
......@@ -224,6 +224,20 @@ We then call the `plot` function explicitly on this figure.
The "Axes" object has all of the same plotting command as we used above,
although the commands to adjust the properties of things like the title, x-axis, and y-axis are slighly different.
`plt.getp` gives a helpful summary of the properties of a matplotlib object (and what you might change):
```
plt.getp(ax)
```
When going through this list carefully you might have spotted that the plotted line is stored in the `lines` property.
Let's have a look at this line in more detail
```
plt.getp(ax.lines[0])
```
This shows us all the properties stored about this line,
including its coordinates in many different formats
(`data`, `path`, `xdata`, `ydata`, or `xydata`),
the line style and width (`linestyle`, `linewidth`), `color`, etc.
<a class="anchor" id="subplots"></a>
## Multiple plots (i.e., subplots)
As stated one of the strengths of the object-oriented interface is that it is easier to work with multiple plots.
......@@ -322,12 +336,13 @@ axes.set(
> To match the matlab API and save some typing the equivalent commands in the procedural interface do not have the `set_` preset. So, they are `plt.xlabel`, `plt.ylabel`, `plt.title`. This is also true for many of the `set_` commands we will see below.
You can edit the font of the text when setting the label:
You can edit the font of the text when setting the label or after the fact using the object-oriented interface:
```
fig, axes = plt.subplots()
axes.plot([1, 2, 3], [2.3, 4.1, 0.8])
axes.set_xlabel("xlabel", color='red')
axes.set_ylabel("ylabel", fontsize='larger')
axes.set_ylabel("ylabel")
axes.get_yaxis().get_label().set_fontsize('larger')
```
<a class="anchor" id="axis"></a>
......@@ -357,6 +372,12 @@ axes.set_yticks((0, 0.5, 1))
axes.set_yticklabels(('0', '50%', '100%'))
fig.tight_layout()
```
As illustrated earlier, we can get a more complete list of the things we could change about the x-axis by looking at its properties:
```
plt.getp(axes.get_xlabel())
```
<a class="anchor" id="faq"></a>
## FAQ
<a class="anchor" id="double-image"></a>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!