1# First create some toy data:
2x = np.linspace(0, 2*np.pi, 400)
3y = np.sin(x**2)
4
5# Create just a figure and only one subplot
6fig, ax = plt.subplots()
7ax.plot(x, y)
8ax.set_title('Simple plot')
9
10# Create two subplots and unpack the output array immediately
11f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
12ax1.plot(x, y)
13ax1.set_title('Sharing Y axis')
14ax2.scatter(x, y)
15
16# Create four polar axes and access them through the returned array
17fig, axs = plt.subplots(2, 2, subplot_kw=dict(polar=True))
18axs[0, 0].plot(x, y)
19axs[1, 1].scatter(x, y)
20
21# Share a X axis with each column of subplots
22plt.subplots(2, 2, sharex='col')
23
24# Share a Y axis with each row of subplots
25plt.subplots(2, 2, sharey='row')
26
27# Share both X and Y axes with all subplots
28plt.subplots(2, 2, sharex='all', sharey='all')
29
30# Note that this is the same as
31plt.subplots(2, 2, sharex=True, sharey=True)
32
33# Create figure number 10 with a single subplot
34# and clears it if it already exists.
35fig, ax = plt.subplots(num=10, clear=True)
36
1# using the variable ax for single a Axes
2fig, ax = plt.subplots()
3
4# using the variable axs for multiple Axes
5fig, axs = plt.subplots(2, 2)
6
7# using tuple unpacking for multiple Axes
8fig, (ax1, ax2) = plt.subplot(1, 2)
9fig, ((ax1, ax2), (ax3, ax4)) = plt.subplot(2, 2)
10
1# First create some toy data:
2x = np.linspace(0, 2*np.pi, 400)
3y = np.sin(x**2)
4
5# Creates just a figure and only one subplot
6fig, ax = plt.subplots()
7ax.plot(x, y)
8ax.set_title('Simple plot')
9
10# Creates two subplots and unpacks the output array immediately
11f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
12ax1.plot(x, y)
13ax1.set_title('Sharing Y axis')
14ax2.scatter(x, y)
15
16# Creates four polar axes, and accesses them through the returned array
17fig, axes = plt.subplots(2, 2, subplot_kw=dict(polar=True))
18axes[0, 0].plot(x, y)
19axes[1, 1].scatter(x, y)
20
21# Share a X axis with each column of subplots
22plt.subplots(2, 2, sharex='col')
23
24# Share a Y axis with each row of subplots
25plt.subplots(2, 2, sharey='row')
26
27# Share both X and Y axes with all subplots
28plt.subplots(2, 2, sharex='all', sharey='all')
29
30# Note that this is the same as
31plt.subplots(2, 2, sharex=True, sharey=True)
32
33# Creates figure number 10 with a single subplot
34# and clears it if it already exists.
35fig, ax=plt.subplots(num=10, clear=True)
1fig, (ax1, ax2) = plt.subplots(2)
2fig.suptitle('Vertically stacked subplots')
3ax1.plot(x, y)
4ax2.plot(x, -y)
5#or
6fig, (ax1, ax2) = plt.subplots(1,2) #in lines