Search code examples
matplotlibaxes

How to set common labels with matplotlib


I have a plot obtained in this way:

f, ((ax1, ax2, ax3, ax4), (ax5, ax6, ax7, ax8), (ax9, ax10, ax11, ax12)) = plt.subplots(3, 4, sharex = 'col', sharey = 'row')
ax1.set_title('column1')
ax1.plot([x], [y])
ax5.plot([x1],[y1])
ax9.plot([x2],[y2])
.....

so, I essentially have 3 rows and 4 columns. I would like to know how is it possible to put commond labels to the x and y axis. I tried to write

 plt_xlabel('x')
 plt.ylabel('y')

or

 set.xlabel('x')
 set.ylabel('y')

but it doesn't work. Can you help me? Is it also possible to put text on the right end side of the plot?


Solution

  • You can do this by iterating over your list of axes:

    f, ax_lst  =  plt.subplots(3, 4, sharex = 'col', sharey = 'row')
    for ax_l in ax_lst:
        for ax in ax_l:
            ax.set_xlabel('x')
            ax.set_ylabel('y')