Matplotlib
- Get link
- X
- Other Apps
Matplotlib is the "grandfather" library of data visualization with Python. It was created by John Hunter.
He created it to try to replicate MatLab's (another programming language) plotting capabilities in Python.
So if you happen to be familiar with matlab, matplotlib will feel natural to you.
It is an excellent 2D and 3D graphics library for generating scientific figures.
Some of the major Pros of Matplotlib are:
- Generally easy to get started for simple plots
- Support for custom labels and texts
- Great control of every element in a figure
- High-quality output in many formats
- Very customizable in general
Matplotlib allows you to create reproducible figures programmatically. Let's learn how to use
it!Before continuing this lecture, I encourage you just to explore the official Matplotlib web page:
Let's walk through a very simple example using two numpy arrays:
Example
Let's walk through a very simple example using two numpy arrays. You can also use lists, but most
likely you'll be passing numpy arrays or pandas columns (which essentially also behave like arrays).
** The data we want to plot:**
Basic Matplotlib Commands
We can create a very simple line plot using the following ( I encourage you to pause and use Shift+Tab
along the way to check out the document strings for the functions we are using).
Creating Multiplots on Same Canvas
Matplotlib Object Oriented Method
Now that we've seen the basics, let's break it all down with a more formal introduction of Matplotlib's
Object Oriented API. This means we will instantiate figure objects and then call methods or attributes
from that object.
Introduction to the Object Oriented Method
The main idea in using the more formal Object Oriented method is to create figure objects and then
just call methods or attributes off of that object. This approach is nicer when dealing with a canvas
that has multiple plots on it.
To begin we create a figure instance. Then we can add axes to that figure:
subplots()
The plt.subplots() object will act as a more automatic axis manager.
Basic use cases:
Then you can specify the number of rows and columns when creating the subplots() object:
A common issue with matplolib is overlapping subplots or figures. We ca use fig.tight_layout() or
plt.tight_layout() method, which automatically adjusts the positions of the axes on the figure
canvas so that there is no overlapping content:
A common issue with matplolib is overlapping subplots or figures. We ca use fig.tight_layout()
or plt.tight_layout() method, which automatically adjusts the positions of the axes on the figure
canvas so that there is no overlapping content:
or plt.tight_layout() method, which automatically adjusts the positions of the axes on the figure
canvas so that there is no overlapping content:
Figure size, aspect ratio and DPI
Matplotlib allows the aspect ratio, DPI and figure size to be specified when the Figure object is created. You can use the
figsize and dpi keyword arguments.figsizeis a tuple of the width and height of the figure in inchesdpiis the dots-per-inch (pixel per inch).
For example:
Saving figures
Matplotlib can generate high-quality output in a number formats, including PNG, JPG, EPS,
SVG, PGF and PDF.
To save a figure to a file we can use the
savefig method in the Figure class:Legends, labels and titles
Now that we have covered the basics of how to create a figure canvas and add axes instances to
the canvas, let's look at how decorate a figure with titles, axis labels, and legends.
Figure titles
A title can be added to each axis instance in a figure. To set the title, use the
set_title
method in the axes instance
Legends
You can use the label="label text" keyword argument when plots or other objects are added to the
figure, and then using the legend method without arguments to add the legend to the figure:
Notice how are legend overlaps some of the actual plot!
The legend function takes an optional keyword argument loc that can be used to specify where
in the figure the legend is to be drawn. The allowed values of loc are numerical codes for the various
places the legend can be drawn. See the documentation page for details. Some of the most common
loc values are:
- Get link
- X
- Other Apps

























Comments
Post a Comment