Unit V Notes
Unit V Notes
Features of Matplotlib
a well-tested, cross-platform graphics engine
Has the ability to work with many operating systems and graphics
backends.
supports dozens of backends and output types, regardless of
operating systems or output formats
cross-platform, everything-to-everyone approach
easy to set new global plotting styles
Seaborn, ggplot, HoloViews, Altair, and even Pandas packages can
be used as wrappers around Matplotlib’s API
o With these wrappers, matplotlib’s syntax can be used to
adjust the final plot output
1. Importing matplotlib
np - NumPy
pd - Pandas
Setting Styles
plt.style.use('classic')
How to Display Your Plots? show() or No show()?
The three applicable contexts of using Matplotlib are
o script
o IPython terminal, or
o IPython notebook
In[5]: fig.savefig('my_figure.png')
from IPython.display import Image
Image('my_figure.png')
To get a list of supported file types for your system - Use the
following method of the figure canvas object:
In[8]: fig.canvas.get_supported_filetypes()
Out[8]: {'eps': 'Encapsulated Postscript',
'jpeg': 'Joint Photographic Experts Group',
'jpg': 'Joint Photographic Experts Group',
'pdf': 'Portable Document Format',
'pgf': 'PGF code for LaTeX',
'png': 'Portable Network Graphics',
'ps': 'Postscript',
'raw': 'Raw RGBA bitmap',
'rgba': 'Raw RGBA bitmap',
'svg': 'Scalable Vector Graphics',
'svgz': 'Scalable Vector Graphics',
'tif': 'Tagged Image File Format',
'tiff': 'Tagged Image File Format'}
Two Interfaces
A potentially confusing feature of Matplotlib is its dual interfaces:
o a convenient MATLAB-style state-based interface, and
o a more powerful object-oriented interface.
i. MATLAB-style interface Matplotlib
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.
This interface is stateful: it keeps track of the “current” figure and
axes, where all plt commands are applied.
o To get current figure – plt.gcf()
o To get current axes - plt.gca()
This interface is fast and convenient for simple plots.
There are many other keyword arguments that can be used to fine-tune
the appearance of the plot.
4. Visualizing Errors
An error is the difference between the calculated value and actual
value.
For any scientific measurement, accurate accounting for errors is
more important than accurate reporting of the number itself.
While representing the data graphically, some of the data have
irregularity.
These irregularities or errors can be effectively visualized using
error bars and the plots give much more complete information.
Basic Errorbars
A basic errorbar can be created with a single Matplotlib function call:
An errorbar example
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np
x = np.linspace(0, 10, 50)
dy = 0.8
y = np.sin(x) + dy * np.random.randn(50)
plt.errorbar(x, y, yerr=dy, fmt='.k');
Customizing errorbars
The errorbar function has many options (parameters) to finetune
the outputs.
o x: specifies horizontal coordinates of the data points.
o y: specifies vertical coordinates of the data points.
o xerr: Define the horizontal error bar sizes. Must have a
float or array-like shape.
o yerr: Define the vertical error bar sizes. Must have a float
or array-like shape.
o fmt: format code controlling the appearance of lines and
points. Contains string value. By default, this plots error
bars with markers. Use ‘none’ to plot error bars without
markers.
o ecolor: specifies the color of the error bars.
o elinewidth: specifies linewidth of the error bars.
o capsize: specifies the length of error bars in points or
float.
Example:
plt.errorbar(x, y, yerr=dy, fmt='o', color='black', ecolor='lightgray',
elinewidth=3, capsize=0);
Continuous Errors
In some situations it is desirable to show errorbars on continuous
quantities.
Matplotlib does not have a built-in convenience routine for this type
of application.
So, primitives like plt.plot and plt.fill_between are combined for a
useful result.
Example: performing a simple Gaussian process regression (GPR),
using the Scikit-Learn API. This is a method of fitting a very flexible
nonparametric function to data with a continuous measure of the
uncertainty.
xfit, yfit, and dyfit - sample the continuous fit to the data.
To avoid plotting 1,000 points with 1,000 errorbars, the
plt.fill_between function can be used with a light color to visualize
this continuous error.
A simple histogram
In[2]: plt.hist(data);
A customized histogram
In[3]: plt.hist(data, bins=30, normed=True, alpha=0.5, histtype='stepfilled',
color='steelblue', edgecolor='none');
Customizing Colorbars
The colormap can be specified using the cmap argument to the
plotting function that is creating the visualization.
A grayscale colormap
In[4]: plt.imshow(I, cmap='gray');
Choosing the colormap
Three different categories of colormaps:
1. Sequential colormaps
These consist of one continuous sequence of colors (e.g., binary or
viridis).
2. Divergent colormaps
These usually contain two distinct colors, which show positive and
negative deviations from a mean (e.g., RdBu or PuOr).
3. Qualitative colormaps
These mix colors with no particular sequence (e.g., rainbow or jet).
In[6]: view_colormap('jet')
In[7]: view_colormap('viridis')
A discretized colormap
In[11]: plt.imshow(I, cmap=plt.cm.get_cmap('Blues', 6))
plt.colorbar()
plt.clim(-1, 1);
9. Multiple Subplots
The subplots in Matplotlib are groups of smaller axes that can exist
together within a single figure.
These subplots might be insets, grids of plots, or other more
complicated layouts.
There are four routines for creating subplots in Matplotlib.
They include:
o plt.axes
o plt.subplot
o plt.subplots
o plt.GridSpec
i. plt.axes: Subplots by Hand
o The most basic method of creating axes is to use the plt.axes
function.
o By default this function creates a standard axes object that fills the
entire figure.
o plt.axes also takes an optional argument that is a list of four
numbers in the figure coordinate system.
o These numbers represent [bottom, left, width, height] in the figure
coordinate system, which ranges from 0 at the bottom left of the
figure to 1 at the top right of the figure.
There are two axes (the top with no tick labels) that are just
touching: the bottom of the upper panel (at position 0.5) matches
the top of the lower panel (at position 0.1 + 0.4).
A plt.subplot() example
In[4]: for i in range(1, 7):
plt.subplot(2, 3, i)
plt.text(0.5, 0.5, str((2, 3, i)), fontsize=18, ha='center')
Multiple Subplots
The subplots in Matplotlib are groups of smaller axes that can exist
together within a single figure.
These subplots might be insets, grids of plots, or other more
complicated layouts.
There are four routines for creating subplots in Matplotlib.
They include:
o plt.axes
o plt.subplot
o plt.subplots
o plt.GridSpec
v. plt.axes: Subplots by Hand
o The most basic method of creating axes is to use the plt.axes
function.
o By default this function creates a standard axes object that fills the
entire figure.
o plt.axes also takes an optional argument that is a list of four
numbers in the figure coordinate system.
o These numbers represent [bottom, left, width, height] in the figure
coordinate system, which ranges from 0 at the bottom left of the
figure to 1 at the top right of the figure.
There are two axes (the top with no tick labels) that are just
touching: the bottom of the upper panel (at position 0.5) matches
the top of the lower panel (at position 0.1 + 0.4).
A plt.subplot() example
In[4]: for i in range(1, 7):
plt.subplot(2, 3, i)
plt.text(0.5, 0.5, str((2, 3, i)), fontsize=18, ha='center')
x. Factor plots
An example of a factor plot, comparing distributions given various
discrete factors
This is useful in viewing the distribution of a parameter within bins
defined by any other parameter.
In[16]: with sns.axes_style(style='ticks'):
g = sns.factorplot("day", "total_bill", "sex", data=tips, kind="box")
g.set_axis_labels("Day", "Total Bill");