> 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.
<aclass="anchor"id="error"></a>
<aclass="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,
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: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),
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.
<aclass="anchor"id="OO"></a>
<aclass="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 in matplotlib) to a single plot.
(collectively called artists 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:


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:earned-anaheim tags:
%% Cell type:code id:3d3482ef tags:
```python
```
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: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.
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):
%% 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.
<aclass="anchor"id="subplots"></a>
<aclass="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:intensive-bruce tags:
%% Cell type:code id:3cafa27a tags:
```python
```
plt.subplot(221)
plt.subplot(221)
plt.title("Upper left")
plt.title("Upper left")
plt.subplot(222)
plt.subplot(222)
plt.title("Upper right")
plt.title("Upper right")
plt.subplot(223)
plt.subplot(223)
plt.title("Lower left")
plt.title("Lower left")
plt.subplot(224)
plt.subplot(224)
plt.title("Lower right")
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
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.
code to figure out which of the subplots this specific `plt.title` command is affecting.