Last modified: 18 December 2023

How can I adjust multiple plots with matplotlib? [Updated]


Once multiple plots have been created thet can be adjusted using Matplotlib. For example, the vertical spacing between two plots can be adjusted

sherpa> plot("data", 1, "data", 2)
sherpa> plt.subplots_adjust(hspace=0.5)

Increasing the vertical space between the plots

[Two plots arranged vertically where the title of the bottom plot does not overlap the X-axis label of the top plot.]
[Print media version: Two plots arranged vertically where the title of the bottom plot does not overlap the X-axis label of the top plot.]

Increasing the vertical space between the plots

If more than two plots are created then multiple columns will be used:

sherpa> plot('fit', 1, 'fit', 2, 'delchi', 1, 'delchi', 2)
sherpa> fig = plt.gcf()
sherpa> for i in [0, 1]:
   ...:    fig.axes[i].xaxis.label.set_visible(False)
   ...:    fig.axes[i].xaxis.set_ticklabels([])
   ...:    fig.axes[i].set_ylim(1e-4, 2e-1)
   ...:
sherpa> for i in [2, 3]:
   ...:    fig.axes[i].title.set_visible(False)
   ...:    fig.axes[i].set_ylim(-4, 4)
   ...:
sherpa> for i in [1, 3]:
   ...:    fig.axes[i].yaxis.label.set_visible(False)
   ...:    fig.axes[i].yaxis.set_ticklabels([])
   ...:
sherpa> plt.subplots_adjust(hspace=0.05, wspace=0.05)

Adjusted plot

[The horizontal and vertical space between the plots has been removed (except for a small amount to make the tick marks still visible), and the labels that were in this space have gone. The Y axes of the top row of plots is now the same, as is the bottom row (although each row has different ranges).]
[Print media version: The horizontal and vertical space between the plots has been removed (except for a small amount to make the tick marks still visible), and the labels that were in this space have gone. The Y axes of the top row of plots is now the same, as is the bottom row (although each row has different ranges).]

Adjusted plot