1. Matplotlib
In this lesson, we will talk about the plotting functions in matplotlib. It's important to see how matplotlib works so you can fine tune your plots and figures as per your liking.
2. Matplotlib plots
Let's first recreate some of the plots we've seen so far. As usual, first import matplotlib dot pyplot as plt.
You can then create a histogram by passing the relevant column to the hist() function.
3. Matplotlib scatter
Scatter plots can be created with the scatter() function. You need to pass two required arguments that specify values for the x and y axes.
4. Polishing up the figure
After you have created the plot, you can add labels to your plot to make it more readable. To do this, you first need to assign the result of plt dot scatter to an object. It is a common practice to name this object as ax (short for axes, which will discuss soon).
You can now call the set_title(), set_xlabel(), and set_ylabel() methods on ax to add title, x-label and y-label to your plot.
5. Rotating axis ticks
If the x-axis labels start overlapping, you can choose to rotate them by using the xticks function and passing in an angle of rotation.
6. Parts of a matplotlib figure
Here is the older matplotlib "Parts of a figure" diagram.
I like this diagram because it clearly shows the differences between a figure and an axes.
You can think of figure as the entire image, and an axes is one subplot in that image. This diagram has only one axes, that is only one subplot.
Also note the difference between axes (a-x-e-s) with axis (a-x-i-s).
This distinction between axes and figure is important because different plotting functions return different objects.
Some will return a figure, others will return an axes.
7. Parts of a matplotlib figure 2
And by the way, this is the latest matplotlib "Parts of a figure" diagram.
8. Figures and axes
As mentioned earlier, a figure is composed of one or more axes.
These axes can also be referred to as subplots.
Here we are creating a single figure with one axes using the subplots() function. Then we can take our axes object, ax, and draw a scatter plot in it.
Note the code for creating the scatter plot. Instead of using plt dot scatter, we are calling scatter on the axes object.
9. Multiple axes
The real benefit of plotting in this manner is the ability to have multiple plots in the same figure.
Here we create two axes in the figure with the subplots() function. Our figure will have 1 row and 2 columns as denoted by plt dot subplots of 1, 2.
Now when we plot our figure, we will get a single figure with 2 axes, and a separate plot in each axes.
When plotting multiple subplots, you may want to call the
tight_layout() function so your figure axes do not overlap.
10. Remember
Even though I showed you how to create these plots using matplotlib directly, you can use functions from other plotting libraries to generate plots inside a figure.
For example some functions in seaborn take an optional ax argument. You can pass the axes object to this argument to plot the figure.
11. Clearing the figure
In the exercises, you will be creating multiple plots.
To prevent plots from overlapping on one another, you can use the clf() function to clear the figure, before plotting the next one.
12. Let's practice!
Now let's try some examples of plotting with matplotlib.