

I didn't managed to sort it out from inside the function, but being careful to the given row_headers and col_headers arguments is enough to make it work easily: mosaic = [Īs a extension of Joe's answer, you can use the Y-label approach, even with existing Y-labels, which I find much easier than using annotate, especially if the axes are not on a regular grid.įor this you can twin the axis and move it to the left: import matplotlib.pyplot as pltįig, axes = plt.subplots(nrows=3, ncols=3, If some axes spans multiple rows / cols, it gets a bit less straightforward to assign rows / cols headers correctly. Subplots_kwargs = dict(sharex=True, sharey=True, figsize=(10, 6))įig, axes = plt.subplot_mosaic(mosaic, **subplots_kwargs)įont_kwargs = dict(fontfamily="monospace", fontweight="bold", fontsize="large")Īdd_headers(fig, col_headers=col_headers, row_headers=row_headers, **font_kwargs) Here is an example of using it using on a standard grid (no axes spans multiple rows / cols): import randomĬol_headers = If (row_headers is not None) and sbs.is_first_col(): If (col_headers is not None) and sbs.is_first_row():

row_headers, col_headers : a sequence of strings to be headers.fig : The figure which contains the axes to work on.
#SETTING SUBPLOT TITLE CODE#
# You could automatically calculate them, but it's a pain.īased on Joe Kington's answer, I put up a function that can be reused across a code base:

# tight_layout doesn't take these labels into account. Xycoords=ax.yaxis.label, textcoords='offset points', Xycoords='axes fraction', textcoords='offset points', import matplotlib.pyplot as pltĬols = ]įig, axes = plt.subplots(nrows=4, ncols=3, figsize=(12, 8)) If you don't have y-labels on your axes, it's easy to exploit the title and y-label of the first row and column of axes. Alternatively, you can place additional text in the right location with annotate and then make room for it semi-manually. The easy way is to exploit the y-labels and titles of the plot and then use fig.tight_layout() to make room for the labels.
