Finding Axes handles with range()

Thanks in advance.

I created 5 subplots using the code below.

fig = plt.figure(figsize=(12,12))
for i in range(5):
    ax = fig.add_subplot(5,1,i+1)

How do I find out the handles for the 5 Axes objects created in this loop? Say that at a later stage, I want to modify one of them.

I tried fig.axes which provided following o/p. It didn’t give me usable handles.

[<matplotlib.axes._subplots.AxesSubplot object at 0x7fd4abc2b160>, <matplotlib.axes._subplots.AxesSubplot object at 0x7fd4abc7e2e8>, <matplotlib.axes._subplots.AxesSubplot object at 0x7fd4abba4b70>, <matplotlib.axes._subplots.AxesSubplot object at 0x7fd4abb63b70>, <matplotlib.axes._subplots.AxesSubplot object at 0x7fd4a9af0668>]

Hey @aparna.bharadwaj

Welcome back to DQ Community.

I tried this, but it requires us to remember which axes we are trying to work with.

fig.axes[0].set_title("Dummy Titles") This sets the title only for the first plot.

Also, out of curiosity, how exactly would you work on the axes at a later stage and why would you do so?

Thanks much Rucha. This helps. I used following two ways to set titles to one or all plots. Also, your way helps me to refer to a particular axes.

For All Titles

    for i in range(5):
        ax = fig.add_subplot(5,1,i+1, title='plot'+str(i))

Rucha’s trick: This answers my question.
fig.axes[3].set_title("4th year")

There would always be some tweaking to be done. While I am new to analytics, I assume that prior to presenting an analysis there may be a need to say adjust ticks or font or title. Hence we would need to access the axes.

What does “ax” in the loop stand for?

Thanks again for your support.