Lec 16-19 Ch4 Matplotlib Part1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 65

Visualization with Matplotlib

Disclaimer Statement
In preparation of these slides, materials have been taken from different online sources in the
shape of books, websites, research papers and presentations etc. However, the author does not
have any intention to take any benefit of these in her/his own name. This lecture (audio, video,
slides etc) is prepared and delivered only for educational purposes and is not intended to
infringe upon the copyrighted material. Sources have been acknowledged where applicable. The
views expressed are presenter’s alone and do not necessarily represent actual author(s) or the
institution.
Introduction
Matplotlib is a multiplatform data visualization library built on NumPy arrays, and designed to
work with the broader SciPy stack
IOne of Matplotlib most important features is its ability to play well with many operating
systems and graphics backbends. Matplotlib supports dozens of backbends and output types,
which means you can count on it to work regardless of which operating system you are using or
which output format you wish. This cross-platform, everything-to-everyone approach has been
one of the great strengths of Matplotlib.
General Matplotlib Tips
we will use some standard shorthand's for Matplotlib imports
Plotting from a script
If you are using Matplotlib from within a script, the function plt.show() starts an event loop,
looks for all currently active figure objects, and opens one or more interactive windows that
display your figure or figures. So, for example, you may have a file called myplot.py containing
the following:

import matplotlib.pyplot as plt


import numpy as np
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.show()
Plotting from an IPython shell
IPython is built to work well with Matplotlib if you specify Matplotlib mode. To enable this mode, you
can use the %matplotlib magic command after start‐ ing ipython:

At this point, any plt plot command will cause a figure window to open, and further commands can
be run to update the plot. Some changes (such as modifying proper‐ ties of lines that are already
drawn) will not draw automatically; to force an update, use plt.draw(). Using plt.show() in Matplotlib
mode is not required.
(Cont..)
For this book, we will generally opt for
%matplotlib inline:

%matplotlib inline

import numpy as np
x = np.linspace(0, 10, 100)
fig = plt.figure()
plt.plot(x, np.sin(x), '-')
plt.plot(x, np.cos(x), '--');
%matplot without using inline

Code output configure subplot options


Saving Figures to File
One nice feature of Matplotlib is the ability to save figures in a wide variety of formats. You can
save a figure using the savefig() command. For example, to save the previous figure as a PNG
file, you can run this:

fig.savefig(‘filename.png’)

Or
plt.savefig(‘filename.png’)
(Cont..)
To confirm that it contains what we think it contains, let’s use the IPython Image object to
display the contents of this file

You can find the list of supported file types for your system by using the following method of the
figure canvas object:
Matplotlib & its dual interfaces
A potentially confusing feature of Matplotlib is its dual interfaces: a convenient MATLAB-style
state-based interface, and a more powerful object-oriented interface. We’ll quickly highlight the
differences between the two here.
1. MATLAB-style interface
2. Object-oriented interface
(Cont..)
MATLAB-style interface Matplotlib was originally written as a Python alternative for MATLAB
users, and much of its syntax reflects that fact. The MATLAB-style tools are contained in the
pyplot (plt) interface. For example, the following code will probably look quite familiar to
MATLAB users
Object-oriented interface
In the object-oriented interface the plotting functions are methods of explicit Figure and
Axes objects. To re-create the previous plot
using this style of plotting, you might do the following

# First create a grid of plots


# ax will be an array of two Axes
objects
fig, ax = plt.subplots(2)
# Call plot() method on the
appropriate object
ax[0].plot(x, np.sin(x))
ax[1].plot(x, np.cos(x));
Simple Line Plots
The simplest of all plots is the visualization of a single function y = f x .
we’ll start by setting up the notebook for plotting and importing the functions we will use.

%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np
An empty gridded axes
For all Matplotlib plots, we start by creating a figure and an axes. In their simplest
form, a figure and axes can be created as follows
Example:A simple sinusoid
we have created an axes, we can use the ax.plot function to plot some data. Let’s start with a
simple sinusoid .

fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x));
OR
plt.plot(x, np.sin(x));
(Cont..)
we want to create a single figure with multiple lines, we can simply call the plot function
multiple times
Adjusting the Plot: Line Colors and
Styles
The first adjustment you might wish to make to a plot is to control the line colors and styles.
The plt.plot() function takes additional arguments that can be used to spec‐ ify these.
To adjust the color, you can use the color keyword, which accepts a string argument
representing virtually any imaginable color. The color can be specified in a variety of ways
Controlling the color of plot elements
plt.plot(x, np.sin(x - 0), color='blue') # specify color by name
plt.plot(x, np.sin(x - 1), color='g') # short color code (rgbcmyk)
plt.plot(x, np.sin(x - 2), color='0.75') # Grayscale between 0 and 1
plt.plot(x, np.sin(x - 3), color='#FFDD44') # Hex code (RRGGBB from 00 to
FF)
plt.plot(x, np.sin(x - 4), color=(1.0,0.2,0.3)) # RGB tuple, values 0 and 1
plt.plot(x, np.sin(x - 5), color='chartreuse');
Example of various line styles
you can adjust the line style using the linestyle keyword
(Cont..)
color codes can be combined into a single non keyword argument to the plt.plot() function
Adjusting the Plot: Axes Limits
Adjusting the Plot: Axes Limits
Matplotlib does a decent job of choosing default axes limits for your plot, but some‐
times it’s nice to have finer control. The most basic way to adjust axis limits is to use
the plt.xlim() and plt.ylim() methods
Example of reversing the y-axis
If for some reason you’d like either axis to be displayed in reverse, you can simply reverse the
order of the arguments
Setting the axis limits with plt.axis
A useful related method is plt.axis() (note here the potential confusion between axes with an e,
and axis with an i).
The plt.axis() method allows you to set the x and y limits with a single call, by passing a list that
specifies
[xmin, xmax, ymin, ymax]
Example of a “tight” layout
The plt.axis() method goes even beyond this, allowing you to do things like automatically tighten
the bounds around the current plot
Example of an “equal” layout, with units
matched to the output resolution
It allows even higher-level specifications, such as ensuring an equal aspect ratio so that on your
screen, one unit in x is equal to one unit in y
Labeling Plots
. Examples of axis labels and title
We’ll briefly look at the labeling of plots: titles, axis labels, and simple legends.
Titles and axis labels are the simplest such labels—there are methods that can be used to
quickly set them.
(Cont..)
When multiple lines are being shown within a single axes, it can be useful to create a plot legend
that labels each line type. Again, Matplotlib has a built-in way of quickly creating such a legend.
It is done via plt.legend() method.
.
Matplotlib Gotchas
Transitioning between MATLAB and
object-oriented methods
While most plt functions translate directly to ax methods
plt.plot() → ax.plot()
plt.legend() → ax.legend()
In particular, functions to set limits, labels, and titles are slightly modified. plt.xlabel() →
ax.set_xlabel()
plt.ylabel() → ax.set_ylabel()
plt.xlim() → ax.set_xlim()
plt.ylim() → ax.set_ylim()
plt.title() → ax.set_title()
Example of using ax.set to set multiple
properties at once
In the object-oriented interface to plotting, rather than calling these functions individually, it is often more
convenient to use the ax.set() method to set all these properties at once (
Simple Scatter Plots
Simple Scatter Plots
. Instead of points being joined by line segments, here the points are represented individually
with a dot, circle, or other shape.

Simple plot Scatter Plot


Marker style
The marker style has its own set of short string codes.
The full list of available symbols can be seen in the documentation of plt.plot, or in Matplotlib’s
online documentation.
we’ll show a number of the more common ones here
Combining line and point markers
For even more possibilities, these character codes can be used together with line and color
codes to plot points along with a line connecting them .

line (-),
circle marker (o),
black (k)
Customizing line and point numbers
Additional keyword arguments to plt.plot specify a wide range of properties of the lines and
markers
Scatter Plots with plt.scatter
Previous method was Second method is
using plt.plot () function Using plot.Scatter() function

In scatter plots function, the properties of each individual point (size, face color, edge color, etc.) can
be individually controlled or mapped to data.
Random scatter plot using alpha
keyword
(Cont..)
For example, we might use the Iris data from Scikit-Learn, where each sample is one of three
types of flowers that has had the size of its petals and sepals carefully measured.
The Iris Dataset
This data sets consists of 3 different types of irises’ (Setosa, Versicolour, and Virginica) petal and
sepal length, stored in a 150x4 numpy.ndarray
Using point properties to encode features
of the Iris data
Visualizing Errors
Visualizing Errors
Some standard errors, confidence intervals, and likelihoods often lose their visual space in data
graphics, which leads to judgements based on simplified summaries expressed as means,
medians, etc.
In visualization of data and results, showing these errors effectively can make a plot convey
much more complete information.
For any scientific measurement, accurate accounting for errors is nearly as important
Example, the expansion rate of the universe in current literature suggests a value of around 71
― 2.5 (km/s)/Mpc, and user method has measured a value of 74 ―74 ― 5 (km/s)/Mpc
Basic Errorbars
A basic errorbar can be created with a single Matplotlib function call.

Using plt.plot using plt.errorbar


Continuous Errors
To show errorbars on continuous quantities, Matplotlib does not have a built-in convenience
routine for this type of application,
it’s relatively easy to combine primitives like plt.plot and plt.fill_between for a useful result.
Here we’ll perform a simple Gaussian process regression (GPR), using the Scikit-Learn
This is a method of fitting a very flexible nonparametric function to data with a continuous
measure of the uncertainty.
Representing continuous uncertainty with filled regions
(Cont..)
Note what we’ve done here with the fill_between function: we pass an x value, then the lower
y-bound, then the upper y-bound, and the result is that the area between these regions is filled.
The resulting figure gives a very intuitive view into what the Gaussian process regression
algorithm is doing: in regions near a measured data point, the model is strongly constrained and
this is reflected in the small model errors. In regions far from a measured data point, the model
is not strongly constrained, and the model errors increase.
Density and Contour Plots
Sometimes it is useful to display three-dimensional data in two dimensions using contours or
color-coded regions.
A contour plot is a graphical technique for representing a 3-dimensional surface by plotting
constant z slices, called contours, on a 2-dimensional format.
There are three Matplotlib functions that can be helpful for this task:
1.plt.contour for contour plots
2. plt.contourf for filled contour plots
3.plt.imshow for showing images.
Visualizing a Three-Dimensional
Function
A contour plot can be created with the plt.contour function. It takes three arguments:
a grid of x values, a grid of y values, and a grid of z values.
The x and y values represent positions on the plot, and the z values will be represented by the
contour levels.
The most straightforward way to prepare such data is to use the
np.meshgrid function
which builds two-dimensional grids from one-dimensional arrays:
(Cont..)
def f(x, y):
return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
In[3]: x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
In[4]: plt.contour(X, Y, Z, colors='black');
(Cont..)
plt.contour(X, Y, Z, 20, cmap='RdGy');
Visualizing three-dimensional data with
filled contours
Our plot is looking nicer, but the spaces between the lines may be a bit distracting.
We can change this by switching to a filled contour plot using the plt.contourf() function
plt.contourf(X, Y, Z, 20, cmap='RdGy')
plt.colorbar();
The colorbar makes it clear that the black regions are “peaks,” while the red regions are
“valleys.”
Histograms, Binnings,
and Density
Histograms, Binnings, and Density
A simple histogram can be a great first step in understanding a dataset
Using function
plt.hist(data);
The hist() function has many options to
tune both the calculation and the display
Example of a more customized
histogram
Histograms of several distributions
using alpha transparency
The combination of histtype='stepfilled' along with some transparency
alpha to be very useful when comparing histograms of several distributions

bins=5 bins=40
compute the histogram
If you would like to simply compute the histogram (that is, count the number of points in a given
bin) and not display it, the np.histogram() function is available:
Two-Dimensional Histograms and
Binnings
Just as we create histograms in one dimension by dividing the number line into bins.
we can also create histograms in two dimensions by dividing points among two dimensional
bins.
We’ll take a brief look at several ways to do this here. We’ll start by defining some data—an x
and y array drawn from a multivariate Gaussian distribution:
Two-dimensional histogram(plt.hist2d)
One straightforward way to plot a two-dimensional histogram is to use Matplotlib’s plt.hist2d
function
A two-dimensional histogram with
plt.hexbin
Another natural shape for such a tessellation is the regular hexagon. For this purpose, Matplotlib
provides the plt.hexbin routine, which represents a two-dimensional dataset binned within a
grid of hexagons
Customizing Plot Legends 2

Default plot legend A two-column plot legend A fancy box plot legend

A two-column plot legend A fancybox plot legend


ax.legend(fancybox=True, framealphA
ax.legend(frameon=False, loc='lower
fancybox plot legend
center', ncol=2)
a=1, shadow=True, borderpad=1)
fig
fig
Choosing Elements for the Legend
The legend includes all labelled elements by default.
If this is not what is desired, we can fine tune which elements and labels appear in the legend by
using the objects returned by plot commands.
The plt.plot() command is able to create multiple lines at once, and returns a list of created line
instances
Multiple Legends
Sometimes when designing a plot you’d like to add multiple legends to the same axes.
Unfortunately, Matplotlib does not make this easy: via the standard legend interface, it is only
possible to create a single legend for the entire plot.
If you try to create a second legend using plt.legend() or ax.legend(), it will simply override the
first one
. We can work around this by creating a new legend artist from scratch, and then using the
lower-level ax.add_artist() method to manually add the second artist to the plot
Reference
VanderPlas, J. (2016). Python data science handbook: Essential tools for working with data. "
O'Reilly Media, Inc.".

You might also like