"To adjust how the line is plotted, check the documentation:"
...
...
@@ -87,7 +87,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "0ce4a966",
"id": "9a768ab3",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -96,7 +96,7 @@
},
{
"cell_type": "markdown",
"id": "e7263d90",
"id": "d2e6a4d1",
"metadata": {},
"source": [
"As you can see there are a lot of options.\n",
...
...
@@ -109,7 +109,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "dfbbb093",
"id": "85ed5f73",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -123,7 +123,7 @@
},
{
"cell_type": "markdown",
"id": "c5cf861d",
"id": "a359e01a",
"metadata": {},
"source": [
"Because these keywords are so common, you can actually set one or more of them by passing in a string as the third argument."
...
...
@@ -132,7 +132,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "9446bd5b",
"id": "0e69e842",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -145,7 +145,7 @@
},
{
"cell_type": "markdown",
"id": "f17ba1d2",
"id": "891a9f9e",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"scatter\"></a>\n",
...
...
@@ -156,7 +156,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "31c06d59",
"id": "8cb13b7e",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -168,7 +168,7 @@
},
{
"cell_type": "markdown",
"id": "777734ae",
"id": "df311f5c",
"metadata": {},
"source": [
"The third argument is the variable determining the size, while the fourth argument is the variable setting the color.\n",
...
...
@@ -180,7 +180,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "1fd95cae",
"id": "f9bb4e76",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -190,7 +190,7 @@
},
{
"cell_type": "markdown",
"id": "72a015c6",
"id": "141bf7e8",
"metadata": {},
"source": [
"where it also returns the number of elements in each bin, as `n`, and\n",
...
...
@@ -207,7 +207,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "0c410bcd",
"id": "951bd53e",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -222,7 +222,7 @@
},
{
"cell_type": "markdown",
"id": "75c96456",
"id": "2ae38282",
"metadata": {},
"source": [
"> 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.\n",
...
...
@@ -236,7 +236,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "00caf192",
"id": "3f440fd0",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -249,7 +249,7 @@
},
{
"cell_type": "markdown",
"id": "dd9fb30f",
"id": "1405cf82",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"shade\"></a>\n",
...
...
@@ -260,7 +260,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "bb53b679",
"id": "c0f12a0d",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -270,7 +270,7 @@
},
{
"cell_type": "markdown",
"id": "e47aefc6",
"id": "71d7bc82",
"metadata": {},
"source": [
"This can be nicely combined with a polar projection, to create 2D orientation distribution functions:"
...
...
@@ -279,7 +279,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "84538d49",
"id": "e337ced8",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -290,7 +290,7 @@
},
{
"cell_type": "markdown",
"id": "91a936ab",
"id": "12c4eee6",
"metadata": {},
"source": [
"The area between two lines can be shaded using `fill_between`:"
...
...
@@ -299,7 +299,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "ebb0f958",
"id": "54c6b838",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -313,7 +313,7 @@
},
{
"cell_type": "markdown",
"id": "8ae52787",
"id": "3a1d3815",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"image\"></a>\n",
...
...
@@ -324,7 +324,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "0fe3f185",
"id": "ed051029",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -340,7 +340,7 @@
},
{
"cell_type": "markdown",
"id": "dabed6ef",
"id": "156b0628",
"metadata": {},
"source": [
"Note that matplotlib will use the **voxel data orientation**, and that\n",
...
...
@@ -352,7 +352,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "65e1381a",
"id": "a65cf0d6",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -365,7 +365,7 @@
},
{
"cell_type": "markdown",
"id": "0e20f40f",
"id": "7c8a01a8",
"metadata": {},
"source": [
"> It is easier to produce informative brain images using nilearn or fsleyes\n",
...
...
@@ -377,7 +377,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "c5101a5b",
"id": "3f9f4fad",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -390,7 +390,7 @@
},
{
"cell_type": "markdown",
"id": "6c91efd8",
"id": "d2fb44b4",
"metadata": {},
"source": [
"By default the locations of the arrows and text will be in data coordinates (i.e., whatever is on the axes),\n",
...
...
@@ -425,7 +425,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "994a4e47",
"id": "43229971",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -436,7 +436,7 @@
},
{
"cell_type": "markdown",
"id": "0c244a1a",
"id": "8d4bee33",
"metadata": {},
"source": [
"Note that here we explicitly create the figure and add a single sub-plot to the figure.\n",
...
...
@@ -450,7 +450,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "87b60efe",
"id": "2cc5123a",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -459,7 +459,7 @@
},
{
"cell_type": "markdown",
"id": "9e8785b0",
"id": "37251f4a",
"metadata": {},
"source": [
"When going through this list carefully you might have spotted that the plotted line is stored in the `lines` property.\n",
...
...
@@ -469,7 +469,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "1e9372b7",
"id": "db290a0a",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -478,7 +478,7 @@
},
{
"cell_type": "markdown",
"id": "afd6a54e",
"id": "ae053e0c",
"metadata": {},
"source": [
"This shows us all the properties stored about this line,\n",
...
...
@@ -495,7 +495,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "5bff872f",
"id": "8bd710d5",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -511,7 +511,7 @@
},
{
"cell_type": "markdown",
"id": "510185fb",
"id": "28b82718",
"metadata": {},
"source": [
"For such a simple example, this works fine. But for longer examples you would find yourself constantly looking back through the\n",
...
...
@@ -523,7 +523,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "52ffc81d",
"id": "89a20086",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -536,7 +536,7 @@
},
{
"cell_type": "markdown",
"id": "48464ab0",
"id": "852c2d46",
"metadata": {},
"source": [
"Here we use `plt.subplots`, which creates both a new figure for us and a grid of sub-plots. \n",
...
...
@@ -551,7 +551,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d047679b",
"id": "5c14ec50",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -565,7 +565,7 @@
},
{
"cell_type": "markdown",
"id": "c8fe247b",
"id": "338c7239",
"metadata": {},
"source": [
"Uncomment `fig.tight_layout` and see how it adjusts the spacings between the plots automatically to reduce the whitespace.\n",
...
...
@@ -576,7 +576,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "c82a0837",
"id": "5df7361f",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -591,7 +591,7 @@
},
{
"cell_type": "markdown",
"id": "09b2b281",
"id": "ff58c930",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"grid-spec\"></a>\n",
...
...
@@ -603,7 +603,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "b4f4a54b",
"id": "c1651d0c",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -623,7 +623,7 @@
},
{
"cell_type": "markdown",
"id": "37bc60de",
"id": "5676c42d",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"styling\"></a>\n",
...
...
@@ -637,7 +637,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "c4b8f402",
"id": "b6841514",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -650,7 +650,7 @@
},
{
"cell_type": "markdown",
"id": "7c6e0fc1",
"id": "c27500eb",
"metadata": {},
"source": [
"You can also set any of these properties by calling `Axes.set` directly:"
...
...
@@ -659,7 +659,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "ff0502fc",
"id": "4aa8461b",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -674,7 +674,7 @@
},
{
"cell_type": "markdown",
"id": "0db1eb83",
"id": "e69e0f4b",
"metadata": {},
"source": [
"> 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.\n",
...
...
@@ -685,7 +685,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "369c02d6",
"id": "d9958b2e",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -698,7 +698,7 @@
},
{
"cell_type": "markdown",
"id": "6095ba78",
"id": "111da8e1",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"axis\"></a>\n",
...
...
@@ -717,7 +717,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "c8f5a0dd",
"id": "4e402140",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -738,7 +738,7 @@
},
{
"cell_type": "markdown",
"id": "2dbbad8d",
"id": "9bd34f1c",
"metadata": {},
"source": [
"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:"
...
...
@@ -747,7 +747,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "20ca99eb",
"id": "db2b0e6e",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -756,7 +756,7 @@
},
{
"cell_type": "markdown",
"id": "35fe5da3",
"id": "48b79b04",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"faq\"></a>\n",
...
...
@@ -788,7 +788,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "1620a4da",
"id": "e36ee821",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -797,7 +797,7 @@
},
{
"cell_type": "markdown",
"id": "b0024423",
"id": "68b0aac8",
"metadata": {},
"source": [
"> 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).\n",
...
...
@@ -808,7 +808,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "b67ee344",
"id": "b81eb924",
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -818,7 +818,7 @@
},
{
"cell_type": "markdown",
"id": "36ca2dfc",
"id": "14663014",
"metadata": {},
"source": [
"Usually, the default backend will be fine, so you will not have to set it. \n",
...
...
%% Cell type:markdown id:dda775e0 tags:
%% Cell type:markdown id:fa095385 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.
> 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.
<aclass="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,
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: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),
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.
<aclass="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:

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])`