Commit 427d64cd authored by Michiel Cottaar's avatar Michiel Cottaar Committed by Michiel Cottaar
Browse files

move to data_visualisation directory

parent fbd8ae16
%% Cell type:markdown id:dda775e0 tags: %% Cell type:markdown id:fa095385 tags:
# Matplotlib tutorial # Matplotlib tutorial
The main plotting library in python is `matplotlib`. The main plotting library in python is `matplotlib`.
It provides a simple interface to just explore the data, It provides a simple interface to just explore the data,
while also having a lot of flexibility to create publication-worthy plots. while also having a lot of flexibility to create publication-worthy plots.
In fact, the vast majority of python-produced plots in papers will be either produced In fact, the vast majority of python-produced plots in papers will be either produced
directly using matplotlib or by one of the many plotting libraries built on top of directly using matplotlib or by one of the many plotting libraries built on top of
matplotlib (such as [seaborn](https://seaborn.pydata.org/) or [nilearn](https://nilearn.github.io/)). matplotlib (such as [seaborn](https://seaborn.pydata.org/) or [nilearn](https://nilearn.github.io/)).
Like everything in python, there is a lot of help available online (just google it or ask your local pythonista). Like everything in python, there is a lot of help available online (just google it or ask your local pythonista).
A particularly useful resource for matplotlib is the [gallery](https://matplotlib.org/gallery/index.html). A particularly useful resource for matplotlib is the [gallery](https://matplotlib.org/gallery/index.html).
Here you can find a wide range of plots. Here you can find a wide range of plots.
Just find one that looks like what you want to do and click on it to see (and copy) the code used to generate the plot. Just find one that looks like what you want to do and click on it to see (and copy) the code used to generate the plot.
## Contents ## Contents
- [Basic plotting commands](#basic-plotting-commands) - [Basic plotting commands](#basic-plotting-commands)
- [Line plots](#line) - [Line plots](#line)
- [Scatter plots](#scatter) - [Scatter plots](#scatter)
- [Histograms and bar plots](#histograms) - [Histograms and bar plots](#histograms)
- [Adding error bars](#error) - [Adding error bars](#error)
- [Shading regions](#shade) - [Shading regions](#shade)
- [Displaying images](#image) - [Displaying images](#image)
- [Adding lines, arrows, text](#annotations) - [Adding lines, arrows, text](#annotations)
- [Using the object-oriented interface](#OO) - [Using the object-oriented interface](#OO)
- [Multiple plots (i.e., subplots)](#subplots) - [Multiple plots (i.e., subplots)](#subplots)
- [Adjusting plot layouts](#layout) - [Adjusting plot layouts](#layout)
- [Advanced grid configurations (GridSpec)](#grid-spec) - [Advanced grid configurations (GridSpec)](#grid-spec)
- [Styling your plot](#styling) - [Styling your plot](#styling)
- [Setting title and labels](#labels) - [Setting title and labels](#labels)
- [Editing the x- and y-axis](#axis) - [Editing the x- and y-axis](#axis)
- [FAQ](#faq) - [FAQ](#faq)
- [Why am I getting two images?](#double-image) - [Why am I getting two images?](#double-image)
- [I produced a plot in my python script, but it does not show up](#show) - [I produced a plot in my python script, but it does not show up](#show)
- [Changing where the image appears: backends](#backends) - [Changing where the image appears: backends](#backends)
<a class="anchor" id="basic-plotting-commands"></a> <a class="anchor" id="basic-plotting-commands"></a>
## Basic plotting commands ## Basic plotting commands
Let's start with the basic imports: Let's start with the basic imports:
%% Cell type:code id:5ccc9a73 tags: %% Cell type:code id:41578cdc tags:
``` ```
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
``` ```
%% Cell type:markdown id:ed561bcb tags: %% Cell type:markdown id:1a9a5f55 tags:
<a class="anchor" id="line"></a> <a class="anchor" id="line"></a>
### Line plots ### Line plots
A basic lineplot can be made just by calling `plt.plot`: A basic lineplot can be made just by calling `plt.plot`:
%% Cell type:code id:80e15ee1 tags: %% Cell type:code id:2531bb20 tags:
``` ```
plt.plot([1, 2, 3], [1.3, 4.2, 3.1]) plt.plot([1, 2, 3], [1.3, 4.2, 3.1])
``` ```
%% Cell type:markdown id:c2923739 tags: %% Cell type:markdown id:9ef51d5c tags:
To adjust how the line is plotted, check the documentation: To adjust how the line is plotted, check the documentation:
%% Cell type:code id:0ce4a966 tags: %% Cell type:code id:9a768ab3 tags:
``` ```
plt.plot? plt.plot?
``` ```
%% Cell type:markdown id:e7263d90 tags: %% Cell type:markdown id:d2e6a4d1 tags:
As you can see there are a lot of options. As you can see there are a lot of options.
The ones you will probably use most often are: The ones you will probably use most often are:
- `linestyle`: how the line is plotted (set to '' to omit the line) - `linestyle`: how the line is plotted (set to '' to omit the line)
- `marker`: how the points are plotted (these are not plotted by default) - `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) - `color`: what color to use (defaults to cycling through a set of 7 colors)
%% Cell type:code id:dfbbb093 tags: %% Cell type:code id:85ed5f73 tags:
``` ```
theta = np.linspace(0, 2 * np.pi, 101) theta = np.linspace(0, 2 * np.pi, 101)
plt.plot(np.sin(theta), np.cos(theta)) 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.3, 0.3], [0.3, 0.3], marker='o', linestyle='', markersize=20)
plt.plot(0, -0.1, marker='s', color='black') plt.plot(0, -0.1, marker='s', color='black')
x = np.linspace(-0.5, 0.5, 5) x = np.linspace(-0.5, 0.5, 5)
plt.plot(x, x ** 2 - 0.5, linestyle='--', marker='+', color='red') plt.plot(x, x ** 2 - 0.5, linestyle='--', marker='+', color='red')
``` ```
%% Cell type:markdown id:c5cf861d tags: %% Cell type:markdown id:a359e01a 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. 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:9446bd5b tags: %% Cell type:code id:0e69e842 tags:
``` ```
x = np.linspace(0, 1, 11) x = np.linspace(0, 1, 11)
plt.plot(x, x) plt.plot(x, x)
plt.plot(x, x ** 2, '--') # sets the linestyle to dashed 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 ** 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') 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:f17ba1d2 tags: %% Cell type:markdown id:891a9f9e tags:
<a class="anchor" id="scatter"></a> <a class="anchor" id="scatter"></a>
### Scatter plots ### 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: 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:31c06d59 tags: %% Cell type:code id:8cb13b7e tags:
``` ```
x = np.random.rand(30) x = np.random.rand(30)
y = np.random.rand(30) y = np.random.rand(30)
plt.scatter(x, y, x * 30, y) plt.scatter(x, y, x * 30, y)
plt.colorbar() # adds a colorbar plt.colorbar() # adds a colorbar
``` ```
%% Cell type:markdown id:777734ae tags: %% Cell type:markdown id:df311f5c tags:
The third argument is the variable determining the size, while the fourth argument is the variable setting the color. 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> <a class="anchor" id="histograms"></a>
### Histograms and bar plots ### Histograms and bar plots
For a simple histogram you can do this: For a simple histogram you can do this:
%% Cell type:code id:1fd95cae tags: %% Cell type:code id:f9bb4e76 tags:
``` ```
r = np.random.rand(1000) r = np.random.rand(1000)
n,bins,_ = plt.hist((r-0.5)**2, bins=30) n,bins,_ = plt.hist((r-0.5)**2, bins=30)
``` ```
%% Cell type:markdown id:72a015c6 tags: %% Cell type:markdown id:141bf7e8 tags:
where it also returns the number of elements in each bin, as `n`, and where it also returns the number of elements in each bin, as `n`, and
the bin centres, as `bins`. the bin centres, as `bins`.
> The `_` in the third part on the left > The `_` in the third part on the left
> hand side is a shorthand for just throwing away the corresponding part > hand side is a shorthand for just throwing away the corresponding part
> of the return structure. > of the return structure.
There is also a call for doing bar plots: There is also a call for doing bar plots:
%% Cell type:code id:0c410bcd tags: %% Cell type:code id:951bd53e tags:
``` ```
samp1 = r[0:10] samp1 = r[0:10]
samp2 = r[10:20] samp2 = r[10:20]
bwidth = 0.3 bwidth = 0.3
xcoord = np.arange(10) xcoord = np.arange(10)
plt.bar(xcoord-bwidth, samp1, width=bwidth, color='red', label='Sample 1') plt.bar(xcoord-bwidth, samp1, width=bwidth, color='red', label='Sample 1')
plt.bar(xcoord, samp2, width=bwidth, color='blue', label='Sample 2') plt.bar(xcoord, samp2, width=bwidth, color='blue', label='Sample 2')
plt.legend(loc='upper left') plt.legend(loc='upper left')
``` ```
%% Cell type:markdown id:75c96456 tags: %% Cell type:markdown id:2ae38282 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. > 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> <a class="anchor" id="error"></a>
### Adding error bars ### Adding error bars
If your data is not completely perfect and has for some obscure reason some uncertainty associated with it, 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`: you can plot these using `plt.error`:
%% Cell type:code id:00caf192 tags: %% Cell type:code id:3f440fd0 tags:
``` ```
x = np.arange(5) x = np.arange(5)
y1 = [0.3, 0.5, 0.7, 0.1, 0.3] y1 = [0.3, 0.5, 0.7, 0.1, 0.3]
yerr = [0.12, 0.28, 0.1, 0.25, 0.6] yerr = [0.12, 0.28, 0.1, 0.25, 0.6]
xerr = 0.3 xerr = 0.3
plt.errorbar(x, y1, yerr, xerr, marker='s', linestyle='') plt.errorbar(x, y1, yerr, xerr, marker='s', linestyle='')
``` ```
%% Cell type:markdown id:dd9fb30f tags: %% Cell type:markdown id:1405cf82 tags:
<a class="anchor" id="shade"></a> <a class="anchor" id="shade"></a>
### Shading regions ### Shading regions
An area below a plot can be shaded using `plt.fill` An area below a plot can be shaded using `plt.fill`
%% Cell type:code id:bb53b679 tags: %% Cell type:code id:c0f12a0d tags:
``` ```
x = np.linspace(0, 2, 100) x = np.linspace(0, 2, 100)
plt.fill(x, np.sin(x * np.pi)) plt.fill(x, np.sin(x * np.pi))
``` ```
%% Cell type:markdown id:e47aefc6 tags: %% Cell type:markdown id:71d7bc82 tags:
This can be nicely combined with a polar projection, to create 2D orientation distribution functions: This can be nicely combined with a polar projection, to create 2D orientation distribution functions:
%% Cell type:code id:84538d49 tags: %% Cell type:code id:e337ced8 tags:
``` ```
plt.subplot(projection='polar') plt.subplot(projection='polar')
theta = np.linspace(0, 2 * np.pi, 100) theta = np.linspace(0, 2 * np.pi, 100)
plt.fill(theta, np.exp(-2 * np.cos(theta) ** 2)) plt.fill(theta, np.exp(-2 * np.cos(theta) ** 2))
``` ```
%% Cell type:markdown id:91a936ab tags: %% Cell type:markdown id:12c4eee6 tags:
The area between two lines can be shaded using `fill_between`: The area between two lines can be shaded using `fill_between`:
%% Cell type:code id:ebb0f958 tags: %% Cell type:code id:54c6b838 tags:
``` ```
x = np.linspace(0, 10, 1000) x = np.linspace(0, 10, 1000)
y = 5 * np.sin(5 * x) + x - 0.1 * x ** 2 y = 5 * np.sin(5 * x) + x - 0.1 * x ** 2
yl = x - 0.1 * x ** 2 - 5 yl = x - 0.1 * x ** 2 - 5
yu = yl + 10 yu = yl + 10
plt.plot(x, y, 'r') plt.plot(x, y, 'r')
plt.fill_between(x, yl, yu) plt.fill_between(x, yl, yu)
``` ```
%% Cell type:markdown id:8ae52787 tags: %% Cell type:markdown id:3a1d3815 tags:
<a class="anchor" id="image"></a> <a class="anchor" id="image"></a>
### Displaying images ### Displaying images
The main command for displaying images is `plt.imshow` (use `plt.pcolor` for cases where you do not have a regular grid) 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:0fe3f185 tags: %% Cell type:code id:ed051029 tags:
``` ```
import nibabel as nib import nibabel as nib
import os.path as op import os.path as op
nim = nib.load(op.expandvars('${FSLDIR}/data/standard/MNI152_T1_1mm.nii.gz'), mmap=False) nim = nib.load(op.expandvars('${FSLDIR}/data/standard/MNI152_T1_1mm.nii.gz'), mmap=False)
imdat = nim.get_data().astype(float) imdat = nim.get_data().astype(float)
imslc = imdat[:,:,70] imslc = imdat[:,:,70]
plt.imshow(imslc, cmap=plt.cm.gray) plt.imshow(imslc, cmap=plt.cm.gray)
plt.colorbar() plt.colorbar()
plt.axis('off') plt.axis('off')
``` ```
%% Cell type:markdown id:dabed6ef tags: %% Cell type:markdown id:156b0628 tags:
Note that matplotlib will use the **voxel data orientation**, and that Note that matplotlib will use the **voxel data orientation**, and that
configuring the plot orientation is **your responsibility**. To rotate a configuring the plot orientation is **your responsibility**. To rotate a
slice, simply transpose the data (`.T`). To invert the data along along an 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: axis, you don't need to modify the data - simply swap the axis limits around:
%% Cell type:code id:65e1381a tags: %% Cell type:code id:a65cf0d6 tags:
``` ```
plt.imshow(imslc.T, cmap=plt.cm.gray) plt.imshow(imslc.T, cmap=plt.cm.gray)
plt.xlim(reversed(plt.xlim())) plt.xlim(reversed(plt.xlim()))
plt.ylim(reversed(plt.ylim())) plt.ylim(reversed(plt.ylim()))
plt.colorbar() plt.colorbar()
plt.axis('off') plt.axis('off')
``` ```
%% Cell type:markdown id:0e20f40f tags: %% Cell type:markdown id:7c8a01a8 tags:
> It is easier to produce informative brain images using nilearn or fsleyes > It is easier to produce informative brain images using nilearn or fsleyes
<a class="anchor" id="annotations"></a> <a class="anchor" id="annotations"></a>
### Adding lines, arrows, and text ### Adding lines, arrows, and text
Adding horizontal/vertical lines, arrows, and text: Adding horizontal/vertical lines, arrows, and text:
%% Cell type:code id:c5101a5b tags: %% Cell type:code id:3f9f4fad tags:
``` ```
plt.axhline(-1) # horizontal line plt.axhline(-1) # horizontal line
plt.axvline(1) # vertical line plt.axvline(1) # vertical line
plt.arrow(0.2, -0.2, 0.2, -0.8, length_includes_head=True, width=0.01) 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.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 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:6c91efd8 tags: %% Cell type:markdown id:d2fb44b4 tags:
By default the locations of the arrows and text will be in data coordinates (i.e., whatever is on the axes), 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 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. 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) See the matplotlib [transformations tutorial](https://matplotlib.org/stable/tutorials/advanced/transforms_tutorial.html)
for more detail. for more detail.
<a class="anchor" id="OO"></a> <a class="anchor" id="OO"></a>
## Using the object-oriented interface ## Using the object-oriented interface
In the examples above we simply added multiple lines/points/bars/images In the examples above we simply added multiple lines/points/bars/images
(collectively called [artists](https://matplotlib.org/stable/tutorials/intermediate/artists.html) in matplotlib) to a single plot. (collectively called [artists](https://matplotlib.org/stable/tutorials/intermediate/artists.html) in matplotlib) to a single plot.
To prettify this plots, we first need to know what all the features are called: To prettify this plots, we first need to know what all the features are called:
![anatomy of a plot](https://matplotlib.org/stable/_images/anatomy.png) ![anatomy of a plot](https://matplotlib.org/stable/_images/anatomy.png)
Using the terms in this plot let's see what our first command of `plt.plot([1, 2, 3], [1.3, 4.2, 3.1])` Using the terms in this plot let's see what our first command of `plt.plot([1, 2, 3], [1.3, 4.2, 3.1])`
actually does: actually does:
1. First it creates a figure and makes this the active figure. Being the active figure means that any subsequent commands will affect figure. You can find the active figure at any point by calling `plt.gcf()`. 1. First it creates a figure and makes this the active figure. Being the active figure means that any subsequent commands will affect figure. You can find the active figure at any point by calling `plt.gcf()`.
2. Then it creates an Axes or Subplot in the figure and makes this the active axes. Any subsequent commands will reuse this active axes. You can find the active axes at any point by calling `plt.gca()`. 2. Then it creates an Axes or Subplot in the figure and makes this the active axes. Any subsequent commands will reuse this active axes. You can find the active axes at any point by calling `plt.gca()`.
3. Finally it creates a Line2D artist containing the x-coordinates `[1, 2, 3]` and `[1.3, 4.2, 3.1]` ands adds this to the active axes. 3. Finally it creates a Line2D artist containing the x-coordinates `[1, 2, 3]` and `[1.3, 4.2, 3.1]` ands adds this to the active axes.
4. At some later time, when actually creating the plot, matplotlib will also automatically determine for you a default range for the x-axis and y-axis and where the ticks should be. 4. At some later time, when actually creating the plot, matplotlib will also automatically determine for you a default range for the x-axis and y-axis and where the ticks should be.
This concept of an "active" figure and "active" axes can be very helpful with a single plot, it can quickly get very confusing when you have multiple sub-plots within a figure or even multiple figures. This concept of an "active" figure and "active" axes can be very helpful with a single plot, it can quickly get very confusing when you have multiple sub-plots within a figure or even multiple figures.
In that case we want to be more explicit about what sub-plot we want to add the artist to. 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. 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. 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: For example, the equivalent of `plt.plot([1, 2, 3], [1.3, 4.2, 3.1])` is:
%% Cell type:code id:994a4e47 tags: %% Cell type:code id:43229971 tags:
``` ```
fig = plt.figure() fig = plt.figure()
ax = fig.add_subplot() ax = fig.add_subplot()
ax.plot([1, 2, 3], [1.3, 4.2, 3.1]) ax.plot([1, 2, 3], [1.3, 4.2, 3.1])
``` ```
%% Cell type:markdown id:0c244a1a tags: %% Cell type:markdown id:8d4bee33 tags:
Note that here we explicitly create the figure and add a single sub-plot to the figure. 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. We then call the `plot` function explicitly on this figure.
The "Axes" object has all of the same plotting command as we used above, 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. 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` gives a helpful summary of the properties of a matplotlib object (and what you might change):
%% Cell type:code id:87b60efe tags: %% Cell type:code id:2cc5123a tags:
``` ```
plt.getp(ax) plt.getp(ax)
``` ```
%% Cell type:markdown id:9e8785b0 tags: %% Cell type:markdown id:37251f4a tags:
When going through this list carefully you might have spotted that the plotted line is stored in the `lines` property. 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 Let's have a look at this line in more detail
%% Cell type:code id:1e9372b7 tags: %% Cell type:code id:db290a0a tags:
``` ```
plt.getp(ax.lines[0]) plt.getp(ax.lines[0])
``` ```
%% Cell type:markdown id:afd6a54e tags: %% Cell type:markdown id:ae053e0c tags:
This shows us all the properties stored about this line, This shows us all the properties stored about this line,
including its coordinates in many different formats including its coordinates in many different formats
(`data`, `path`, `xdata`, `ydata`, or `xydata`), (`data`, `path`, `xdata`, `ydata`, or `xydata`),
the line style and width (`linestyle`, `linewidth`), `color`, etc. the line style and width (`linestyle`, `linewidth`), `color`, etc.
<a class="anchor" id="subplots"></a> <a class="anchor" id="subplots"></a>
## Multiple plots (i.e., subplots) ## 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. 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: While we could do this in the procedural interface:
%% Cell type:code id:5bff872f tags: %% Cell type:code id:8bd710d5 tags:
``` ```
plt.subplot(221) plt.subplot(221)
plt.title("Upper left")