Skip to content
Snippets Groups Projects
Commit fbd8ae16 authored by Michiel Cottaar's avatar Michiel Cottaar Committed by Michiel Cottaar
Browse files

Bug fixes/changes suggested by Mo

parent 8ecc39a4
No related branches found
No related tags found
1 merge request!11Matplotlib talk/practical
%% Cell type:markdown id:4ba14387 tags:
%% Cell type:markdown id:dda775e0 tags:
# Matplotlib tutorial
The main plotting library in python is `matplotlib`.
It provides a simple interface to just explore the data,
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
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/)).
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).
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.
## Contents
- [Basic plotting commands](#basic-plotting-commands)
- [Line plots](#line)
- [Scatter plots](#scatter)
- [Histograms and bar plots](#histograms)
- [Adding error bars](#error)
- [Shading regions](#shade)
- [Displaying images](#image)
- [Adding lines, arrows, text](#annotations)
- [Using the object-oriented interface](#OO)
- [Multiple plots (i.e., subplots)](#subplots)
- [Adjusting plot layouts](#layout)
- [Advanced grid configurations (GridSpec)](#grid-spec)
- [Styling your plot](#styling)
- [Setting title and labels](#labels)
- [Editing the x- and y-axis](#axis)
- [FAQ](#faq)
- [Why am I getting two images?](#double-image)
- [I produced a plot in my python script, but it does not show up](#show)
- [Changing where the image appears: backends](#backends)
<a class="anchor" id="basic-plotting-commands"></a>
## Basic plotting commands
Let's start with the basic imports:
%% Cell type:code id:3f5212f2 tags:
%% Cell type:code id:5ccc9a73 tags:
```
import matplotlib.pyplot as plt
import numpy as np
```
%% Cell type:markdown id:9b66b866 tags:
%% Cell type:markdown id:ed561bcb tags:
<a class="anchor" id="line"></a>
### Line plots
A basic lineplot can be made just by calling `plt.plot`:
%% Cell type:code id:520e577b tags:
%% Cell type:code id:80e15ee1 tags:
```
plt.plot([1, 2, 3], [1.3, 4.2, 3.1])
```
%% Cell type:markdown id:88a5db94 tags:
%% Cell type:markdown id:c2923739 tags:
To adjust how the line is plotted, check the documentation:
%% Cell type:code id:ba7b1bf7 tags:
%% Cell type:code id:0ce4a966 tags:
```
plt.plot?
```
%% Cell type:markdown id:1eb64212 tags:
%% Cell type:markdown id:e7263d90 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:b0571451 tags:
%% Cell type:code id:dfbbb093 tags:
```
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:85597103 tags:
%% Cell type:markdown id:c5cf861d 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:e8c50bcf tags:
%% Cell type:code id:9446bd5b tags:
```
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:84e7d60e tags:
%% Cell type:markdown id:f17ba1d2 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:fff43424 tags:
%% Cell type:code id:31c06d59 tags:
```
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:ed3c393d tags:
%% Cell type:markdown id:777734ae 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:87e83be8 tags:
%% Cell type:code id:1fd95cae tags:
```
r = np.random.rand(1000)
n,bins,_ = plt.hist((r-0.5)**2, bins=30)
```
%% Cell type:markdown id:78abc3b7 tags:
%% Cell type:markdown id:72a015c6 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
> hand side is a shorthand for just throwing away the corresponding part
> of the return structure.
There is also a call for doing bar plots:
%% Cell type:code id:b2945a9f tags:
%% Cell type:code id:0c410bcd tags:
```
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:09f070f4 tags:
%% Cell type:markdown id:75c96456 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:79fb7453 tags:
%% Cell type:code id:00caf192 tags:
```
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:d54c0bbc tags:
%% Cell type:markdown id:dd9fb30f tags:
<a class="anchor" id="shade"></a>
### Shading regions
An area below a plot can be shaded using `plt.fill`
%% Cell type:code id:599096f8 tags:
%% Cell type:code id:bb53b679 tags:
```
x = np.linspace(0, 2, 100)
plt.fill(x, np.sin(x * np.pi))
```
%% Cell type:markdown id:369696fa tags:
%% Cell type:markdown id:e47aefc6 tags:
This can be nicely combined with a polar projection, to create 2D orientation distribution functions:
%% Cell type:code id:b97730b0 tags:
%% Cell type:code id:84538d49 tags:
```
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:049fbd3b tags:
%% Cell type:markdown id:91a936ab tags:
The area between two lines can be shaded using `fill_between`:
%% Cell type:code id:672b1757 tags:
%% Cell type:code id:ebb0f958 tags:
```
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:e866c409 tags:
%% Cell type:markdown id:8ae52787 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:42fc8081 tags:
%% Cell type:code id:0fe3f185 tags:
```
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')
plt.axis('off')
```
%% Cell type:markdown id:83fb2bb3 tags:
%% Cell type:markdown id:dabed6ef 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:8aa53d09 tags:
%% Cell type:code id:65e1381a tags:
```
plt.imshow(imslc.T, cmap=plt.cm.gray)
plt.xlim(reversed(plt.xlim()))
plt.ylim(reversed(plt.ylim()))
plt.colorbar()
plt.grid('off')
plt.axis('off')
```
%% Cell type:markdown id:43fce6ce tags:
%% Cell type:markdown id:0e20f40f 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:37c81436 tags:
%% Cell type:code id:c5101a5b tags:
```
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:40daff02 tags:
%% Cell type:markdown id:6c91efd8 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)
for more detail.
<a class="anchor" id="OO"></a>
## Using the object-oriented interface
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.
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)
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:
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()`.
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.
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.
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:660d1559 tags:
%% Cell type:code id:994a4e47 tags:
```
fig = plt.figure()
ax = fig.add_subplot()
ax.plot([1, 2, 3], [1.3, 4.2, 3.1])
```
%% Cell type:markdown id:2ad9cfbf tags:
%% Cell type:markdown id:0c244a1a 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:7be3b246 tags:
%% Cell type:code id:87b60efe tags:
```
plt.getp(ax)
```
%% Cell type:markdown id:7fbf6acd tags:
%% Cell type:markdown id:9e8785b0 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:80481e71 tags:
%% Cell type:code id:1e9372b7 tags:
```
plt.getp(ax.lines[0])
```
%% Cell type:markdown id:fa8e6fee tags:
%% Cell type:markdown id:afd6a54e 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:83b536d2 tags:
%% Cell type:code id:5bff872f tags:
```
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:3438c4f7 tags:
%% Cell type:markdown id:510185fb 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:4731d436 tags:
%% Cell type:code id:52ffc81d tags:
```
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:bca7abe6 tags:
%% Cell type:markdown id:48464ab0 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:83fab131 tags:
%% Cell type:code id:d047679b tags:
```
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:609cdb3f tags:
%% Cell type:markdown id:c8fe247b 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:972fda35 tags:
%% Cell type:code id:c82a0837 tags:
```
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:18d903e1 tags:
%% Cell type:markdown id:09b2b281 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:e98b00c8 tags:
%% Cell type:code id:b4f4a54b tags:
```
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])
f3_ax2.set_title('gs[1, :-1]')
f3_ax3 = fig.add_subplot(gs[1:, -1])
f3_ax3.set_title('gs[1:, -1]')
f3_ax4 = fig.add_subplot(gs[-1, 0])
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:0d363e0e tags:
%% Cell type:markdown id:37bc60de 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:9553b91a tags:
%% Cell type:code id:c4b8f402 tags:
```
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:79b53065 tags:
%% Cell type:markdown id:7c6e0fc1 tags:
You can also set any of these properties by calling `Axes.set` directly:
%% Cell type:code id:97643220 tags:
%% Cell type:code id:ff0502fc tags:
```
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:e2fbb884 tags:
%% Cell type:markdown id:0db1eb83 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 or after the fact using the object-oriented interface:
%% Cell type:code id:5f16a656 tags:
%% Cell type:code id:369c02d6 tags:
```
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")
axes.get_yaxis().get_label().set_fontsize('larger')
```
%% Cell type:markdown id:f69eb693 tags:
%% Cell type:markdown id:6095ba78 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.
- The range shown on an axis can be set using `ax.set_xlim` (or `plt.xlim`)
- You can switch to a logarithmic (or other) axis using `ax.set_xscale('log')`
- The location of the ticks can be set using `ax.set_xticks` (or `plt.xticks`)
- 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:b2d196a9 tags:
%% Cell type:code id:c8f5a0dd tags:
```
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():
tick.set(
rotation=45,
size='larger'
)
axes.set_xlabel("Progression through practical")
axes.set_yticks((0, 0.5, 1))
axes.set_yticklabels(('0', '50%', '100%'))
fig.tight_layout()
```
%% Cell type:markdown id:c4c35f2a tags:
%% Cell type:markdown id:2dbbad8d 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:ab2633a6 tags:
%% Cell type:code id:20ca99eb tags:
```
plt.getp(axes.get_xlabel())
plt.getp(axes.get_xaxis())
```
%% Cell type:markdown id:440d18ce tags:
%% Cell type:markdown id:35fe5da3 tags:
<a class="anchor" id="faq"></a>
## FAQ
<a class="anchor" id="double-image"></a>
### Why am I getting two images?
Any figure you produce in the notebook will be shown by default once a cell successfully finishes (i.e., without error).
If the code in a notebook cell crashes after creating the figure, this figure will still be in memory.
It will be shown after another cell successfully finishes.
You can remove this additional plot simply by rerunning the cell, after which you should only see the plot produced by the cell in question.
<a class="anchor" id="show"></a>
### I produced a plot in my python script, but it does not show up?
Add `plt.show()` to the end of your script (or save the figure to a file using `plt.savefig` or `fig.savefig`).
`plt.show` will show the image to you and will block the script to allow you to take in and adjust the figure before saving or discarding it.
<a class="anchor" id="backends"></a>
### Changing where the image appears: backends
Matplotlib works across a wide range of environments: Linux, Mac OS, Windows, in the browser, and more.
The exact detail of how to show you your plot will be different across all of these environments.
This procedure used to translate your `Figure`/`Axes` objects into an actual visualisation is called the backend.
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:93dbeb6e tags:
%% Cell type:code id:1620a4da tags:
```
%matplotlib nbagg
```
%% Cell type:markdown id:d53eef2b tags:
%% Cell type:markdown id:b0024423 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:c16b4394 tags:
%% Cell type:code id:b67ee344 tags:
```
import matplotlib
matplotlib.use("osx")
matplotlib.use("MacOSX")
```
%% Cell type:markdown id:96529a3c tags:
%% Cell type:markdown id:36ca2dfc 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.
......
......@@ -159,7 +159,7 @@ imdat = nim.get_data().astype(float)
imslc = imdat[:,:,70]
plt.imshow(imslc, cmap=plt.cm.gray)
plt.colorbar()
plt.grid('off')
plt.axis('off')
```
Note that matplotlib will use the **voxel data orientation**, and that
......@@ -173,7 +173,7 @@ plt.imshow(imslc.T, cmap=plt.cm.gray)
plt.xlim(reversed(plt.xlim()))
plt.ylim(reversed(plt.ylim()))
plt.colorbar()
plt.grid('off')
plt.axis('off')
```
> It is easier to produce informative brain images using nilearn or fsleyes
......@@ -375,7 +375,7 @@ 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())
plt.getp(axes.get_xaxis())
```
<a class="anchor" id="faq"></a>
......@@ -410,7 +410,7 @@ You can change backends in the IPython terminal/notebook using:
In python scripts, this will give you a syntax error and you should instead use:
```
import matplotlib
matplotlib.use("osx")
matplotlib.use("MacOSX")
```
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.
\ No newline at end of file
%% Cell type:markdown id: tags:
%% Cell type:markdown id:ea6b9781 tags:
# Plotting with python
The main plotting module in python is `matplotlib`. There is a lot
that can be done with it - see the [webpage](https://matplotlib.org/gallery/index.html)
## Contents
* [Running inside a notebook](#inside-notebook)
* [2D plots](#2D-plots)
* [Histograms and Bar Plots](#histograms)
* [Scatter plots](#scatter-plots)
* [Subplots](#subplots)
* [Displaying Images](#displaying-images)
* [3D plots](#3D-plots)
* [Running in a standalone script](#plotting-in-scripts)
* [Exercise](#exercise)
---
<a class="anchor" id="inside-notebook"></a>
## Inside a notebook
Inside a jupyter notebook you get access to this in a slightly
different way, compared to other modules:
%% Cell type:code id: tags:
%% Cell type:code id:35ff2bd1 tags:
```
%matplotlib inline
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:c3d1fa16 tags:
This only needs to be done once in a notebook, like for standard imports.
> There are also other alternatives, including interactive versions - see the practical on Jupyter notebooks for more information about this.
The library works very similarly to plotting in matlab. Let's start
with some simple examples.
<a class="anchor" id="2D-plots"></a>
### 2D plots
%% Cell type:code id: tags:
%% Cell type:code id:0963c7e2 tags:
```
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('bmh')
x = np.linspace(-np.pi, np.pi, 256)
cosx, sinx = np.cos(x), np.sin(x)
plt.plot(x, cosx)
plt.plot(x, sinx, color='red', linewidth=4, linestyle='-.')
plt.plot(x, sinx**2)
plt.xlim(-np.pi, np.pi)
plt.title('Our first plots')
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:801fad5d tags:
> Note that the `plt.style.use('bmh')` command is not necessary, but it
> does make nicer looking plots in general. You can use `ggplot`
> instead of `bmh` if you want something resembling plots made by R.
> For a list of options run: `print(plt.style.available)`
You can also save the objects and interrogate/set their properties, as
well as those for the general axes:
%% Cell type:code id: tags:
%% Cell type:code id:da1d3278 tags:
```
hdl = plt.plot(x, cosx)
print(hdl[0].get_color())
hdl[0].set_color('#707010')
hdl[0].set_linewidth(0.5)
plt.grid('off')
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:26173cb4 tags:
Use `dir()` or `help()` or the online docs to get more info on what
you can do with these.
<a class="anchor" id="histograms"></a>
### Histograms and bar charts
For a simple histogram you can do this:
%% Cell type:code id: tags:
%% Cell type:code id:7a7ef2b9 tags:
```
r = np.random.rand(1000)
n,bins,_ = plt.hist((r-0.5)**2, bins=30)
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:3ec49cd2 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
> hand side is a shorthand for just throwing away the corresponding part
> of the return structure.
There is also a call for doing bar plots:
%% Cell type:code id: tags:
%% Cell type:code id:69572b88 tags:
```
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: tags:
%% Cell type:markdown id:94e33c3e tags:
<a class="anchor" id="scatter-plots"></a>
### Scatter plots
It would be possible to use `plot()` to create a scatter plot, but
there is also an alternative: `scatter()`
%% Cell type:code id: tags:
%% Cell type:code id:1ef6ef5a tags:
```
fig, ax = plt.subplots()
# setup some sizes for each point (arbitrarily example here)
ssize = 100*abs(samp1-samp2) + 10
ax.scatter(samp1, samp2, s=ssize, alpha=0.5)
# now add the y=x line
allsamps = np.hstack((samp1,samp2))
ax.plot([min(allsamps),max(allsamps)],[min(allsamps),max(allsamps)], color='red', linestyle='--')
plt.xlim(min(allsamps),max(allsamps))
plt.ylim(min(allsamps),max(allsamps))
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:69710e7b tags:
> Note that in this case we use the first line return to get a handle to
> the axis, `ax`, and the figure ,`fig`. The axis can be used instead of
> `plt` in most cases, although the `xlim()` and `ylim()` calls can only
> be done through `plt`.
> In general, figures and subplots can be created in matplotlib in a
> similar fashion to matlab, but they do not have to be explicitly
> invoked as you can see from the earlier examples.
<a class="anchor" id="subplots"></a>
### Subplots
These are very similar to matlab:
%% Cell type:code id: tags:
%% Cell type:code id:3ffabeb2 tags:
```
plt.subplot(2, 1, 1)
plt.plot(x,cosx, '.-')
plt.xlim(-np.pi, np.pi)
plt.ylabel('Full sampling')
plt.subplot(2, 1, 2)
plt.plot(x[::30], cosx[::30], '.-')
plt.xlim(-np.pi, np.pi)
plt.ylabel('Subsampled')
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:2c285f4e tags:
<a class="anchor" id="displaying-images"></a>
### Displaying images
The main command for displaying images is `imshow()`
%% Cell type:code id: tags:
%% Cell type:code id:9bfff4b7 tags:
```
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')
plt.axis('off')
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:bd7cd76e 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: tags:
%% Cell type:code id:3623ebcc tags:
```
plt.imshow(imslc.T, cmap=plt.cm.gray)
plt.xlim(reversed(plt.xlim()))
plt.ylim(reversed(plt.ylim()))
plt.colorbar()
plt.grid('off')
plt.axis('off')
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:a39985aa tags:
<a class="anchor" id="3D-plots"></a>
### 3D plots
%% Cell type:code id: tags:
%% Cell type:code id:fc6a81c9 tags:
```
# Taken from https://matplotlib.org/gallery/mplot3d/wire3d.html#sphx-glr-gallery-mplot3d-wire3d-py
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Grab some test data.
X, Y, Z = axes3d.get_test_data(0.05)
# Plot a basic wireframe.
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id:ada68f3e tags:
Surface renderings are many other plots are possible - see 3D examples on
the [matplotlib webpage](https://matplotlib.org/gallery/index.html#mplot3d-examples-index)
---
<a class="anchor" id="plotting-in-scripts"></a>
## Plotting from standalone scripts
When running from a standalone script, the same `matplotlib` import is required,
but the line `%matplotlib <backend>` should *not* be used.
In a script it is also necessary to _finish_ with `plt.show()` as
otherwise nothing is actually displayed. For example, the above
examples would setup a plot but the actual graphic would only appear
after the `plt.show()` command was executed. Furthermore, control is
not returned to the script immediately as the plot is interactive by default.
---
<a class="anchor" id="exercise"></a>
## Exercise
Find a different type of plot (e.g., boxplot, violin plot, quiver
plot, pie chart, etc.), look up
the documentation and then write _your own code that calls this_ to create a plot
from some data that you create yourself (i.e., don't just blindly copy
example code from the docs).
%% Cell type:code id: tags:
%% Cell type:code id:5e6f0017 tags:
```
# Make up some data and do the funky plot
```
%% Cell type:markdown id:b517e96f tags:
If you want more plotting goodneess, have a look at the various tutorials available in the `applications/plottings` folder.
......
......@@ -156,7 +156,7 @@ imdat = nim.get_data().astype(float)
imslc = imdat[:,:,70]
plt.imshow(imslc, cmap=plt.cm.gray)
plt.colorbar()
plt.grid('off')
plt.axis('off')
```
Note that matplotlib will use the **voxel data orientation**, and that
......@@ -170,7 +170,7 @@ plt.imshow(imslc.T, cmap=plt.cm.gray)
plt.xlim(reversed(plt.xlim()))
plt.ylim(reversed(plt.ylim()))
plt.colorbar()
plt.grid('off')
plt.axis('off')
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment