MPL - Subplots

PHOTO EMBED

Sat Sep 24 2022 08:34:49 GMT+0000 (Coordinated Universal Time)

Saved by @sfull

# OO Method 1: fig, (ax1, ax2, ...) = plt.subplots(1, 2)

# Note this dataset creates a spiderweb thing
data1, data2, data3, data4 = np.random.randn(4, 100)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(5, 2.7))

ax1.plot(data1, data2, **{'color':'green',
                          'marker':'x',
                          'linestyle':'dashed'})

ax2.plot(data3, data4, **{'color':'blue',
                          'marker':'s',
                          'linestyle':'dotted'})

# Creates 1 x 2
#-------------------------------------------------------------#
# OO Method 2: ax1 = fig.add_subplot(rc#)
fig = plt.figure()

ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(212)

ax1.plot(x1, y1)
ax2.plot(x2, y2)
ax3.plot(x3, y3)

plt.show()
# Creates 2 x [2, 1]
#-------------------------------------------------------------#
# OO method 3: ax = plt.subplot2grid((6,1), (0,0), rowspan=1, colspan=1)
fig = plt.figure()
ax1 = plt.subplot2grid((r,c), (startx_1, starty_1), rowspan=1, colspan=1)
plt.title("Main Title")
ax2 = plt.subplot2grid((r,c), (startx_2, starty_2), rowspan=4, colspan=1)
plt.xlabel('Date')
plt.ylabel('Price')
ax3 = plt.subplot2grid((r,c), (startx_3, starty_3), rowspan=4, colspan=1)
#-------------------------------------------------------------#
#-------------------------------------------------------------#
# pyplot method 1: plt.subplot(rc#)

names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]

plt.figure(figsize=(9, 3))

plt.subplot(131)
plt.bar(names, values)

plt.subplot(132)
plt.scatter(names, values)

plt.subplot(133)
plt.plot(names, values)

plt.suptitle('Categorical Plotting')
plt.show()
content_copyCOPY