Test - Generate - Figure3: Import Matplotlib - Pyplot As PLT Import Numpy As NP Import Matplotlib - Gridspec As Gs
Test - Generate - Figure3: Import Matplotlib - Pyplot As PLT Import Numpy As NP Import Matplotlib - Gridspec As Gs
test_generate_figure3
Define a numpy array 'x' with expression 'np.arange(1, 101)'. Define another numpy array 'y1' with
expression 'y1 = x'.
Define another numpy array 'y2' with expression 'y1 = x∗∗2'.
Define another numpy array 'y3' with expression 'y1 = x∗∗3'.
Create a figure of size 8 inches in width, and 6 inches in height. Name it as fig.
Define a grid 'g' of 2 rows and 2 columns, using 'GridSpec' function. Ensure that 'matplotlib.gridspec' is
imported, before defining the grid.
Create an axes, using plt.subplot function. Name it as axes1. The subplot must span the 1st row and 1st
column of the defined grid 'g'. Set 'title' argument to 'y = x'. Draw a line plot of 'x' and 'y1' using 'plot'
function on 'axes1.
Create an axes, using plt.subplot function. Name it as axes2. The subplot must span 2nd row and 1st
column of defined grid 'g'. Set 'title' argument to 'y = x∗∗2'. Draw a line plot of 'x' and 'y2' using 'plot'
function on 'axes2.
Create an axes, using plt.subplot function. Name it as axes3. The subplot must span all rows of 2nd column
of defined grid 'g'. Set 'title' argument to 'y = x∗∗3'. Draw a line plot of 'x' and 'y3' using 'plot' function on
'axes3.
In [18]:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gs
x = np.arange(1,101)
y1 = x
y2 = x**2
y3 = x**3
fig = plt.figure(figsize=(8,6))
g = gs.GridSpec(2,2)
axes1 = plt.subplot(g[0,0])
axes1.set(title="y = x")
axes1.plot(x,y1)
axes2 = plt.subplot(g[1,0])
axes2.set(title="y = x**2")
axes2.plot(x,y2)
axes3 = plt.subplot(g[:,1])
axes3.set(title="y = x**3")
axes3.plot(x,y3)
plt.tight_layout()