> 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: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),
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](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:


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:660d1559 tags:
%% Cell type:code id:994a4e47 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: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.
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:7be3b246 tags:
%% Cell type:code id:87b60efe tags:
```
```
plt.getp(ax)
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.
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:80481e71 tags:
%% Cell type:code id:1e9372b7 tags:
```
```
plt.getp(ax.lines[0])
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,
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.
<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: