Adding Two Smaller Subplots To The Side Of My Main Plot In Matplotlib Subplots
Currently my chart is showing only the main big chart on the left. However, I now want to add the two smaller plots to the right-hand side of my main plot; with each individual set
Solution 1:
@max 's answer is fine, but something you can also do matplotlib>=3.3 is
import matplotlib.pyplot as plt
fig = plt.figure(constrained_layout=True)
axs = fig.subplot_mosaic([['Left', 'TopRight'],['Left', 'BottomRight']],
gridspec_kw={'width_ratios':[2, 1]})
axs['Left'].set_title('Plot on Left')
axs['TopRight'].set_title('Plot Top Right')
axs['BottomRight'].set_title('Plot Bottom Right')
Note hw the repeated name 'Left'
is used twice to indicate that this subplot takes up two slots in the layout. Also note the use of width_ratios
.
Solution 2:
This is a tricky question. Essentially, you can place a grid on a figure (add_gridspec()
) and than open subplots (add_subplot()
) in and over different grid elements.
import matplotlib.pyplot as plt
# open figure
fig = plt.figure()
# add grid specifications
gs = fig.add_gridspec(2, 3)
# open axes/subplots
axs = []
axs.append( fig.add_subplot(gs[:,0:2]) ) # large subplot (2 rows, 2 columns)
axs.append( fig.add_subplot(gs[0,2]) ) # small subplot (1st row, 3rd column)
axs.append( fig.add_subplot(gs[1,2]) ) # small subplot (2nd row, 3rd column)
Post a Comment for "Adding Two Smaller Subplots To The Side Of My Main Plot In Matplotlib Subplots"