10/3/22, 12:12 PM AD3301 - Unit II - Part 3 .
ipynb - Colaboratory
Multiple Subplots
Different views of data side by side
Matplotlib has the concept of subplots: groups of smaller axes that can exist together within a
single figure
plt.axes: Subplots by Hand
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
import numpy as np
ax1 = plt.axes() # standard axes
ax2 = plt.axes([0.65, 0.65, 0.2, 0.2])
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))
plt.subplot: Simple Grids of Subplots
for i in range(1, 5):
plt.subplot(2, 2, i)
plt.text(0.5, 0.5, str((2, 2, i)),fontsize=18, ha='center')
The command plt.subplots_adjust can be used to adjust the spacing between these plots
fig = plt.figure()
fig.subplots_adjust(hspace=0.8, wspace=0.8)
for i in range(1, 7):
ax = fig.add_subplot(2, 3, i)
ax.text(0.5, 0.5, str((2, 3, i)),
fontsize=18, ha='center')
plt.subplots: The Whole Grid in One Go
https://colab.research.google.com/drive/1tbyr9idF_04x9KhiC9c7AYgAUZcjFnnV#printMode=true 1/5
10/3/22, 12:12 PM AD3301 - Unit II - Part 3 .ipynb - Colaboratory
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.
fig, ax = plt.subplots(2, 2,sharex='col', sharey='row')
for i in range(2):
for j in range(2):
ax[i, j].text(0.5, 0.5, str((i, j)),fontsize=18, ha='center')
plt.GridSpec: More Complicated Arrangements
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.
grid = plt.GridSpec(2, 3, wspace=0.4, hspace=0.3)
plt.subplot(grid[0, 0])
plt.subplot(grid[0, 1:])
plt.subplot(grid[1, :2])
plt.subplot(grid[1, 2])
Text and Annotation
A good visualization involves guiding the reader so that the figure tells a story
import matplotlib.pyplot as plt
import matplotlib as mpl
plt.style.use('seaborn-whitegrid')
import numpy as np
import pandas as pd
fig, ppool = plt.subplots()
t = np.arange(0.0, 1.0, 0.001)
s = np.sin(2 * np.pi * t)
line = ppool.plot(t, s)
ppool.annotate('Peak Value', xy=(.50, 1),
xytext=(1, 1),arrowprops=dict(facecolor='yellow',
shrink=0.05),xycoords="data",)
ppool.set_ylim(-1.5, 1.5)
https://colab.research.google.com/drive/1tbyr9idF_04x9KhiC9c7AYgAUZcjFnnV#printMode=true 2/5
10/3/22, 12:12 PM AD3301 - Unit II - Part 3 .ipynb - Colaboratory
# Import library
import matplotlib.pyplot as plt
# Define Data
x = [7, 14, 21, 28, 35]
y = [4, 8, 12, 16, 20]
# Plot a Graph
plt.plot(x, y, color = 'r', linestyle= '--')
# Add multiple texts
plt.text(13, 18, "I am Adding Text To The Plot \nI am Trying To Add a New Line of Text \nW
x and y: specifies the coordinates to place text.
# Import library
import matplotlib.pyplot as plt
# Define Data
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
# Plot a Graph
plt.plot(x)
# Define bbox style
box_style=dict(boxstyle='round', facecolor='wheat', alpha=0.5)
# Text inside a box
plt.text(1.2,4.2, "$y=mx + c$",{'color':'red','weight':'heavy','size':20},bbox=box_style)
# Display Graph
plt.show()
In the above example, we use plt.text() method to add a text inside the chart and we pass the
keyword argument bbox to place a text inside the box. The style of the bbox is defined in the
https://colab.research.google.com/drive/1tbyr9idF_04x9KhiC9c7AYgAUZcjFnnV#printMode=true 3/5
10/3/22, 12:12 PM AD3301 - Unit II - Part 3 .ipynb - Colaboratory
box_style variable.
Customizing Ticks
A tick is a short line on an axis.
Ticks are always the same color and line style as the axis.
Ticks come in two types: major and minor.
Major ticks separate the axis into major units.
Minor ticks subdivide the major tick units.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import (MultipleLocator, AutoMinorLocator)
from scipy.stats import norm
x_axis = np.arange(-100, 100, 0.01)
mean = np.mean(x_axis)
sd = np.std(x_axis)
y_axis = norm.pdf(x_axis, mean, sd)
fig, ax = plt.subplots()
ax.plot(x_axis,y_axis)
fig, ax = plt.subplots()
ax.plot(x_axis,y_axis)
ax.xaxis.set_major_locator(MultipleLocator(50))
fig, ax = plt.subplots()
ax.plot(x_axis,y_axis)
https://colab.research.google.com/drive/1tbyr9idF_04x9KhiC9c7AYgAUZcjFnnV#printMode=true 4/5
10/3/22, 12:12 PM AD3301 - Unit II - Part 3 .ipynb - Colaboratory
#ax.xaxis.set_major_locator(MultipleLocator(50))
ax.xaxis.set_minor_locator(MultipleLocator(10))
fig, ax = plt.subplots()
ax.plot(x_axis,y_axis)
ax.xaxis.set_major_locator(MultipleLocator(50))
ax.xaxis.set_minor_locator(MultipleLocator(10))
ax.tick_params(which='minor', length=10, color='r')
ax.tick_params(which='major', length=20,color='g')
Colab paid products - Cancel contracts here
https://colab.research.google.com/drive/1tbyr9idF_04x9KhiC9c7AYgAUZcjFnnV#printMode=true 5/5