How can I adjust multiple plots with matplotlib?
Adjusting plots can involve:
- removing plot titles,
- removing axis labels and tick marks,
- changing the range of an axis,
- and resizing the plots.
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.]](plot_multi.data.gap.png)
![[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.]](plot_multi.data.gap.png)
Increasing the vertical space between the plots
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)
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).]](plot_multi.all.adjusted.png)
![[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).]](plot_multi.all.adjusted.png)
Adjusted plot
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)
Forcing the same axis range
![[The plot titles have been removed, as have the labels on the X axis for the area between the two plots.]](plot_multi.ids.mpl.png)
![[Print media version: The plot titles have been removed, as have the labels on the X axis for the area between the two plots.]](plot_multi.ids.mpl.png)
Forcing the same axis range
Based on the " Using the ids argumemt" visualization.
sherpa> plot("fit", "resid", ids=[1, 2], alpha=0.5)
sherpa> plt.subplots_adjust(hspace=0.05)
sherpa> fig = plt.gcf()
sherpa> fig.axes[0].set_xlim(0.4, 10)
sherpa> fig.axes[1].set_xlim(0.4, 10)
sherpa> fig.axes[0].set_title("")
sherpa> fig.axes[1].set_title("")
sherpa> fig.axes[0].xaxis.set_tick_params(labelbottom=False)
Tweaking the plot layout
![[The plot titles and axis labels (apart from the bottom plot) have been removed. The spacing between the plots has been greatly reduced.]](plot_multi.combined.mpl.png)
![[Print media version: The plot titles and axis labels (apart from the bottom plot) have been removed. The spacing between the plots has been greatly reduced.]](plot_multi.combined.mpl.png)
Tweaking the plot layout
Based on the " Multiple color settings" visualization.
sherpa> col1 = 'gray'
sherpa> col2 = 'green'
sherpa> colors = [col1, col2, [col1, col2]]
sherpa> plot("fit", "fit", 2, "resid", [1, 2], color=colors, cols=1)
sherpa> fig = plt.gcf()
sherpa> fig.axes[0].set_title("")
sherpa> fig.axes[1].set_title("")
sherpa> fig.axes[2].set_title("")
sherpa> for idx in [0, 1]:
...: fig.axes[idx].xaxis.set_label_text("")
...: fig.axes[idx].xaxis.set_tick_params(labelbottom=False)
...:
sherpa> xr0 = fig.axes[0].get_xlim()
sherpa> xr1 = fig.axes[1].get_xlim()
sherpa> xr2 = fig.axes[2].get_xlim()
sherpa> xmin = min(xr0[0], min(xr1[0], xr2[0]))
sherpa> xmax = max(xr0[1], max(xr1[1], xr2[1]))
sherpa> for ax in fig.axes:
...: ax.set_xlim(xmin, xmax)
...:
sherpa> plt.subplots_adjust(hspace=0.05)