0% found this document useful (0 votes)
11 views15 pages

Unit 4 DSF

This document covers data visualization techniques using Matplotlib, including creating line plots, scatter plots, and visualizing errors. It explains how to customize plots with colors, styles, and labels, as well as how to create density and contour plots for three-dimensional data. Additionally, it discusses the importance of accurately representing errors in scientific measurements through error bars and continuous error visualization.

Uploaded by

nl3454343
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views15 pages

Unit 4 DSF

This document covers data visualization techniques using Matplotlib, including creating line plots, scatter plots, and visualizing errors. It explains how to customize plots with colors, styles, and labels, as well as how to create density and contour plots for three-dimensional data. Additionally, it discusses the importance of accurately representing errors in scientific measurements through error bars and continuous error visualization.

Uploaded by

nl3454343
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

UNIT IV
DATA VISUALIZATION

Importing Matplotlib – Line plots – Scatter plots – visualizing errors – density and contour plots – Histograms –
legends – colors – subplots – text and annotation – customization – three dimensional plotting - Geographic Data
with Basemap - Visualization with Seaborn.

1. SIMPLE LINE PLOTS


The simplest of all plots is the visualization of a single function y = f x. Here we will take a first look at creating a
simple plot of this type.
The figure (an instance of the class plt.Figure) can be thought of as a single container that contains all the objects
representing axes, graphics, text, and labels.
The axes (an instance of the class plt.Axes) is what we see above: a bounding box with ticks and labels, which will
eventually contain the plot elements that make up our visualization.

Line Colors and Styles


 The first adjustment you might wish to make to a plot is to control the line colors and styles.
 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
 If no color is specified, Matplotlib will automatically cycle through a set of default colors for multiple lines

Different forms of color representation.


specify color by name - color='blue'
short color code (rgbcmyk) - color='g'
Grayscale between 0 and 1 - color='0.75'
Hex code (RRGGBB from 00 to FF) - color='#FFDD44' RGB tuple, values 0 and 1 -
color=(1.0,0.2,0.3) all HTML color
names supported - color='chartreuse'

 We can adjust the line style using the linestyle keyword.


Different line styles
linestyle='solid'
linestyle='dashed'
linestyle='dashdot'
linestyle='dotted'
Short assignment
linestyle='-' #
solid
linestyle='--' #
dashed
linestyle='-.' #
dashdot
linestyle=':' #
dotted
 linestyle and color codes can be combined into a single nonkeyword argument to the plt.plot() function
plt.plot(x, x + 0, '-g')
# solid green
plt.plot(x, x + 1, '--c')
# dashed cyan plt.plot(x, x + 2, '-.k')
# dashdot black
plt.plot(x, x + 3, ':r');
# dotted red
Axes Limits
 The most basic way to adjust axis limits is to use the plt.xlim() and plt.ylim() methods
Example

60 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

plt.xlim(10, 0)
plt.ylim(1.2, -1.2);
 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]
plt.axis([-1, 11, -1.5, 1.5]);
 Aspect ratio equal is used to represent one unit in x is equal to one unit in y.
plt.axis('equal')

Labeling Plots
The labeling of plots includes titles, axis labels, and simple legends.
Title - plt.title()
Label - plt.xlabel()
plt.ylabel()
Legend - plt.legend()

Example programs Line color:


import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x));
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');# all HTML color names supported

OUTPUT:

Line style:
import matplotlib.pyplot as plt
import numpy as
np fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
plt.plot(x, x + 0, linestyle='solid')
plt.plot(x, x + 1, linestyle='dashed')
plt.plot(x, x + 2, linestyle='dashdot')

61 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

plt.plot(x, x + 3, linestyle='dotted');
# For short, you can use the following codes:
plt.plot(x, x + 4, linestyle='-') # solid
plt.plot(x, x + 5, linestyle='--') # dashed
plt.plot(x, x + 6, linestyle='-.')# dashdot
plt.plot(x, x + 7, linestyle=':'); # dotted

OUTPUT:

Axis limit with label and legend:


import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
plt.xlim(-1, 11)
plt.ylim(-1.5, 1.5);
plt.plot(x, np.sin(x), '-g', label='sin(x)')
plt.plot(x, np.cos(x), ':b', label='cos(x)')
plt.title("A Sine Curve")
plt.xlabel("x")
plt.ylabel("sin(x)");
plt.legend();

OUTPUT:

62 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

2. SIMPLE SCATTER PLOTS


Another commonly used plot type is the simple scatter plot, a close cousin of the line plot. Instead of points
being joined by line segments, here the points are represented individually with a dot, circle, or other shape.
Syntax
plt.plot(x, y, 'type of symbol ', color);
Example
plt.plot(x, y, 'o', color='black');
The third argument in the function call is a character that represents the type of symbol used for the plotting.
Just as you can specify options such as '-' and '--' to control the line style, the marker style has its own set of
short string codes.

Example
 Various symbols used to specify ['o', '.', ',', 'x', '+', 'v', '^', '<', '>', 's', 'd']
 Short hand assignment of line, symbol and color also allowed.
plt.plot(x, y, '-ok');
 Additional arguments in plt.plot()
We can specify some other parameters related with scatter plot which makes it more attractive. They are
color, marker size, linewidth, marker face color, marker edge color, marker edge width, etc

Example
plt.plot(x, y, '-p', color='gray',
markersize=15, linewidth=4,
markerfacecolor='white',
markeredgecolor='gray',
markeredgewidth=2) plt.ylim(-1.2, 1.2);

Scatter Plots with plt.scatter


 A second, more powerful method of creating scatter plots is the plt.scatter function, which can be
used very similarly to the plt.plot function
plt.scatter(x, y, marker='o');
 The primary difference of plt.scatter from plt.plot is that it can be used to create scatter plots where
the properties of each individual point (size, face color, edge color, etc.) can be individually controlled
or mapped to data.
 Notice that the color argument is automatically mapped to a color scale (shown here by the colorbar()
command), and the size argument is given in pixels.
 Cmap – color map used in scatter plot gives different color combinations.

Perceptually Uniform Sequential

['viridis', 'plasma', 'inferno', 'magma']


Sequential
['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds', 'YlOrBr', 'YlOrRd',
'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']
Sequential (2)
['binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink', 'spring', 'summer',
'autumn', 'winter', 'cool', 'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper']
Diverging
['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']
Qualitative
['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']
Miscellaneous
['flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', 'gnuplot',
'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv', 'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar']

63 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

Example programs. Simple scatter plot.


import numpy as np
import matplotlib.pyplot as
plt x = np.linspace(0, 10, 30)
y = np.sin(x)
plt.plot(x, y, 'o', color='black');

Scatter plot with edge color, face color, size,


and width of marker. (Scatter plot with line)
import numpy as np
import matplotlib.pyplot as
plt x = np.linspace(0, 10, 20)
y = np.sin(x)
plt.plot(x, y, '-o', color='gray', markersize=15, linewidth=4,
markerfacecolor='yellow', markeredgecolor='red', markeredgewidth=4)
plt.ylim(-1.5, 1.5);

Scatter plot with random colors, size and transparency


import numpy as np
import matplotlib.pyplot as plt
rng = np.random.RandomState(0)
x = rng.randn(100)
y = rng.randn(100)
colors = rng.rand(100)
sizes = 1000 * rng.rand(100)
plt.scatter(x, y, c=colors, s=sizes, alpha=0.3, map='viridis')
plt.colorbar()

3. VISUALIZING ERRORS
For any scientific measurement, accurate accounting for errors is nearly as important, if not more important,
than accurate reporting of the number itself. For example, imagine that I am using some astrophysical
observations to estimate the Hubble Constant, the local measurement of the expansion rate of the Universe.
In visualization of data and results, showing these errors effectively can make a plot convey much more
complete information.

Types of errors
 Basic Errorbars
 Continuous Errors

Basic Errorbars
A basic errorbar can be created with a single Matplotlib function call.
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');

64 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

 Here the fmt is a format code controlling the appearance of lines and points, and has the same syntax
as the shorthand used in plt.plot()
 In addition to these basic options, the errorbar function has many options to fine tune the outputs.
Using these additional options you can easily customize the aesthetics of your errorbar plot.

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. Though 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 API. This is a
method of fitting a very flexible nonparametric function to data with a continuous measure of the
uncertainty.
4. DENSITY AND CONTOUR PLOTS

To display three-dimensional data in two dimensions using contours or color-coded regions. There are three
Matplotlib functions that can be helpful for this task:
 plt.contour for contour plots,
 plt.contourf for filled contour plots, and
 plt.imshow for showing images.

65 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

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 way to prepare such data is to use the np.meshgrid function,
which builds two-dimensional grids from one- dimensional arrays:

Example
def f(x, y):
return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
plt.contour(X, Y, Z, colors='black');

 Notice that by default when a single color is used, negative values are represented by dashed lines, and
positive values by solid lines.
 Alternatively, you can color-code the lines by specifying a colormap with the cmap argument.
 We’ll also specify that we want more lines to be drawn—20 equally spaced intervals within the data range.
plt.contour(X, Y, Z, 20, cmap='RdGy');
 One potential issue with this plot is that it is a bit “splotchy.” That is, the color steps are discrete rather
than continuous, which is not always what is desired.
 You could remedy this by setting the number of contours to a very high number, but this results in a
rather inefficient plot: Matplotlib must render a new polygon for each step in the level.
 A better way to handle this is to use the plt.imshow() function, which interprets a two-dimensional grid
of data as an image.

There are a few potential gotchas with imshow().


 plt.imshow() doesn’t accept an x and y grid, so you must manually specify the extent [xmin, xmax,
ymin, ymax] of the image on the plot.
 plt.imshow() by default follows the standard image array definition where the origin is in the upper left,
not in the lower left as in most contour plots. This must be changed when showing gridded data.
 plt.imshow() will automatically adjust the axis aspect ratio to match the input data; you can change this
by setting, for example, plt.axis(aspect='image') to make x and y units match.

Finally, it can sometimes be useful to combine


contour plots and image plots. we’ll use a partially
transparent background image (with
transparency set via the alpha parameter) and
over-plot contours with labels on the contours
themselves (using the plt.clabel() function):

contours = plt.contour(X, Y, Z, 3, colors='black')


plt.clabel(contours, inline=True, fontsize=8)
plt.imshow(Z, extent=[0, 5, 0, 5], origin='lower',
cmap='RdGy', alpha=0.5)
plt.colorbar();

Example Program

66 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

import numpy as np
import matplotlib.pyplot as plt
def f(x, y):
return np.sin(x) ** 10 + np.cos(10 + y * x) *
np.cos(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
plt.imshow(Z, extent=[0, 10, 0, 10], origin='lower', cmap='RdGy')
plt.colorbar()

5. HISTOGRAMS
Histogram is the simple plot to represent the large data set. A histogram is a graph showing
frequency distributions. It is a graph showing the number of observations within each given interval.

5. 1Parameters
 plt.hist( ) is used to plot histogram. The hist() function will use an array of numbers to create a
histogram, the array is sent into the function as an argument.
 bins - A histogram displays numerical data by grouping data into "bins" of equal width. Each bin is
plotted as a bar whose height corresponds to how many data points are in that bin. Bins are also
sometimes called "intervals", "classes", or "buckets".
 normed - Histogram normalization is a technique to distribute the frequencies of the histogram over
a wider range than the current range.
 x - (n,) array or sequence of (n,) arrays Input values, this takes either a single array or a sequence of
arrays which are not required to be of the same length.
 histtype - {'bar', 'barstacked', 'step', 'stepfilled'}, optional The type of histogram to draw.

 'bar' is a traditional bar-type histogram. If multiple data are given the bars are arranged side
by side.
 'barstacked' is a bar-type histogram where multiple data are stacked on top of each other.
 'step' generates a lineplot that is by default unfilled.
 'stepfilled' generates a lineplot that is by default filled. Default is 'bar'

 align - {'left', 'mid', 'right'}, optional Controls how the histogram is plotted.

 'left': bars are centered on the left bin edges.


 'mid': bars are centered between the bin edges.
 'right': bars are centered on the right bin edges. Default is 'mid'
 orientation - {'horizontal', 'vertical'}, optional
If 'horizontal', barh will be used for bar-type histograms and the bottom kwarg will be the left
edges.
 color - color or array_like of colors or None, optional
Color spec or sequence of color specs, one per dataset. Default (None) uses the standard line color
sequence.

Default is None
 label - str or None, optional. Default is None

5.2 Other parameter


**kwargs - Patch properties, it allows us to pass
a variable number of keyword arguments to a
python function. ** denotes this type of function.

Example
import numpy as np

67 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

import matplotlib.pyplot as plt


plt.style.use('seaborn-white')
data = np.random.randn(1000)
plt.hist(data);

The hist() function has many options to tune both the calculation and the display; here’s an example of
a more customized histogram.

plt.hist(data, bins=30, alpha=0.5,histtype='stepfilled', color='steelblue',edgecolor='none');

The plt.hist docstring has more information on other customization options available. I find this combination
of histtype='stepfilled' along with some transparency alpha to be very useful when comparing histograms of
several distributions

x1 = np.random.normal(0, 0.8, 1000)


x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)
kwargs = dict(histtype='stepfilled', alpha=0.3, bins=40)
plt.hist(x1, **kwargs)
plt.hist(x2, **kwargs)
plt.hist(x3, **kwargs);

OUTPUT:

5.3 Two-Dimensional Histograms and Binnings


 We can create histograms in two dimensions by dividing points among two dimensional bins.
 We would define x and y values. Here for example We’ll start by defining some data—an x and y
array drawn from a multivariate Gaussian distribution:
 Simple way to plot a two-dimensional histogram is to use Matplotlib’s plt.hist2d() function

Example
mean = [0, 0]
cov = [[1, 1], [1, 2]]
x, y = np.random.multivariate_normal(mean, cov, 1000).T
plt.hist2d(x, y, bins=30, cmap='Blues')
cb = plt.colorbar()
cb.set_label('counts in bin')

OUTPUT:

68 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

6. LEGENDS

Plot legends give meaning to a visualization, assigning labels to the various plot elements. We previously saw
how to create a simple legend; here we’ll take a look at customizing the placement and aesthetics of the legend
in Matplotlib.
Plot legends give meaning to a visualization, assigning labels to the various plot elements. We previously saw
how to create a simple legend; here we’ll take a look at customizing the placement and aesthetics of the legend
in Matplotlib

plt.plot(x, np.sin(x), '-b', label='Sine')


plt.plot(x, np.cos(x), '--r', label='Cosine')
plt.legend();

6. 1 Customizing Plot Legends

Location and turn off the frame


We can specify the location and turn off the frame. By the parameter loc and framon.
ax.legend(loc='upper left', frameon=False)
fig
Number of columns
We can use the ncol command to specify the number of columns in the legend.
ax.legend(frameon=False, loc='lower center', ncol=2)
fig

Rounded box, shadow and frame transparency


We can use a rounded box (fancybox) or add a shadow, change the transparency (alpha value) of the frame,
or change the padding around the text.
ax.legend(fancybox=True, framealpha=1, shadow=True, borderpad=1)
fig

6.2 Choosing Elements for the Legend


 The legend includes all labeled elements by default. We can change 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.
 Passing any of these to plt.legend() will tell it which to identify, along with the labels we’d like to
specify

y = np.sin(x[:, np.newaxis] + np.pi * np.arange(0, 2, 0.5)) OUTPUT:

69 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

lines = plt.plot(x, y)
plt.legend(lines[:2],['first','second']);
# Applying label individually.
plt.plot(x, y[:, 0], label='first')
plt.plot(x, y[:, 1], label='second')
plt.plot(x, y[:, 2:])
plt.legend(framealpha=1, frameon=True);

6.3 Multiple legends


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

Example
import matplotlib.pyplot as plt
plt.style.use('classic')
import numpy as np
x = np.linspace(0, 10, 1000)
ax.legend(loc='lower center', frameon=True, shadow=True,borderpad=1,fancybox=True)
fig

7. COLOR BARS

In Matplotlib, a color bar is a separate axes that can provide a key for the meaning of colors in a plot.
For continuous labels based on the color of points, lines, or regions, a labeled color bar can be a great tool.

The simplest colorbar can be created with the plt.colorbar() function.

Customizing Colorbars Choosing color map.

We can specify the colormap using the cmap argument to the plotting function that is creating the
visualization. Broadly, we can know three different categories of colormaps:
 Sequential colormaps - These consist of one continuous sequence of colors (e.g., binary or viridis).
 Divergent colormaps - These usually contain two distinct colors, which show positive and
negative deviations from a mean (e.g., RdBu or PuOr).
 Qualitative colormaps - These mix colors with no particular sequence (e.g., rainbow or jet).
Color limits and extensions
 Matplotlib allows for a large range of colorbar customization. The colorbar itself is simply an instance
of plt.Axes, so all of the axes and tick formatting tricks we’ve learned are applicable.
 We can narrow the color limits and indicate the out-of-bounds values with a triangular arrow at the top
and bottom by setting the extend property.
plt.subplot(1, 2, 2)
plt.imshow(I, cmap='RdBu')
plt.colorbar(extend='both')
plt.clim(-1, 1);

OUTPUT:

70 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

Discrete colorbars
Colormaps are by default continuous, but sometimes you’d like to represent discrete values. The easiest way to
do this is to use the plt.cm.get_cmap() function, and pass the name of a suitable colormap along with the number
of desired bins.
plt.imshow(I, cmap=plt.cm.get_cmap('Blues', 6))
plt.colorbar()
plt.clim(-1, 1);

8. SUBPLOTS

 Matplotlib has the concept of subplots: 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.
 We’ll explore four routines for creating subplots in Matplotlib.
 plt.axes: Subplots by Hand
 plt.subplot: Simple Grids of Subplots
 plt.subplots: The Whole Grid in One Go
 plt.GridSpec: More Complicated Arrangements

8.1 plt.axes: Subplots by Hand

 The most basic method of creating an axes is to use the plt.axes function. As we’ve seen previously,
by default this creates a standard axes object that fills the entire figure.
 plt.axes also takes an optional argument that is a list of four numbers in the figure coordinate system.
 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.

71 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

For example,
we might create an inset axes at the top-right corner of another axes by setting the x and y position to 0.65
(that is, starting at 65% of the width and 65% of the height of the figure) and the x and y extents to 0.2 (that is,
the size of the axes is 20% of the width and 20% of the height of the figure).

import matplotlib.pyplot
as plt import numpy as np
ax1 = plt.axes() # standard axes
ax2 = plt.axes([0.65, 0.65, 0.2, 0.2])

OUTPUT:

8.2 Vertical sub plot


The equivalent of plt.axes() command within the object-oriented interface is ig.add_axes(). Let’s use this to
create two vertically stacked axes.
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.5, 0.8, 0.4], xticklabels=[], ylim=(-1.2, 1.2))
ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.4], ylim=(-1.2, 1.2))
x = np.linspace(0, 10)
ax1.plot(np.sin(x))
ax2.plot(np.cos(x));

OUTPUT:

 We now have 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).
 If the axis value is changed in second plot both the plots are separated with each other,
example
ax2 = fig.add_axes([0.1, 0.01, 0.8, 0.4])

72 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

8.3 plt.subplot: Simple Grids of Subplots


 Matplotlib has several convenience routines to align columns or rows of subplots.
 The lowest level of these is plt.subplot(), which creates a single subplot within a grid.
 This command takes three integer arguments—the number of rows, the number of columns, and the
index of the plot to be created in this scheme, which runs from the upper left to the bottom right

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')

OUTPUT:

8.4 plt.subplots: The Whole Grid in One Go

 The approach just described can become quite tedious when you’re creating a large grid of subplots,
especially if you’d like to hide the x- and y-axis labels on the inner plots.
 For this purpose, plt.subplots() is the easier tool to use (note the s at the end of subplots).
 Rather than creating a single subplot, this function creates a full grid of subplots in a single line,
returning them in a NumPy array
 Rather than creating a single subplot, this function creates a full grid of subplots in a single line,
returning them in a NumPy array.
 The arguments are the number of rows and number of columns, along with optional keywords sharex
and sharey, which allow you to specify the relationships between different axes.
 Here we’ll create a 2×3 grid of subplots, where all axes in the same row share their y- axis scale, and
all axes in the same column share their x-axis scale

fig, ax = plt.subplots(2, 3, sharex='col', sharey='row')

Note that by specifying sharex and sharey, we’ve automatically removed inner labels on the grid to
make the plot cleaner.

73 | P a g e
Downloaded by Kalai ilaiya ([email protected])
lOMoARcPSD|28284242

OCS353 - DATA SCIENCE FUNDAMENTALS

8.5 plt.GridSpec: More Complicated Arrangements

To go beyond a regular grid to subplots that span multiple rows and columns, plt.GridSpec() is the best tool.
The plt.GridSpec() object does not create a plot by itself; it is simply a convenient interface that is recognized
by the plt.subplot() command.

For example, a gridspec for a grid of two rows and three columns with some specified width and height
space looks like this:

grid = plt.GridSpec(2, 3, wspace=0.4, hspace=0.3)


From this we can specify subplot locations and extents
plt.subplot(grid[0, 0])
plt.subplot(grid[0, 1:])
plt.subplot(grid[1, :2])
plt.subplot(grid[1, 2]);

OUTPUT:

9. TEXT AND ANNOTATION


The most basic types of annotations we will use are axes labels and titles, here we will see some more
visualization and annotation information’s.
 Text annotation can be done manually with the plt.text/ax.text command, which will place text at a
particular x/y value.
 The ax.text method takes an x position, a y position, a string, and then optional keywords specifying
the color, size, style, alignment, and other properties of the text. Here we used ha='right' and
ha='center', where ha is short for horizontal alignment.

Transforms and Text Position


 We anchored our text annotations to data locations. Sometimes it’s preferable to anchor the text to a
position on the axes or figure, independent of the data. In Matplotlib, we do this by modifying the
transform.
 Any graphics display framework needs some scheme for translating between coordinate systems.
 Mathematically, such coordinate transformations are relatively straightforward, and Matplotlib has
a well- developed set of tools that it uses internally to perform them (the tools can be explored in the
matplotlib.transforms submodule).
 There are three predefined transforms that can be useful in this situation.

o ax.transData - Transform associated with data coordinates


o ax.transAxes - Transform associated with the axes (in units of axes dimensions)
o fig.transFigure - Transform associated with the figure (in units of figure dimensions)

74 | P a g e
Downloaded by Kalai ilaiya ([email protected])

You might also like