flix
flix

Reputation: 13

Changing y axis on multiple subplots in matplotlib

I am fairly new to python and matplotlib. I have the following problem:

I want to plot six climatic drivers over 2000 years. Since each plot represents a different driver, I want to give each y-axis a different label, but am not able to index each subplot. Please see the following code and subsequent error message:

###Plot MET drivers
fig3 = plt.figure(figsize=(20, 14))

for ii, name_MET in enumerate (["Day_since_start_of_sim", "Min._Temp", "Max._Temp", "Radiation","CO2_(ppm)","DOY"]):
        ax3 = fig3.add_subplot(2,3,ii+1)
        ax3.plot(drivers,'g-',label='2001')
        ax3.set_title(name_MET)
        ax3.set_xlabel('years')
        ax3[1].set_ylabel('Day_since_start_of_simulation')
        ax3[2].set_ylabel('Degrees C')
        ax3[3].set_ylabel('Degrees C')
        ax3[4].set_ylabel('MJ m-2 d-1')
        ax3[5].set_ylabel('ppm')
        ax3[6].set_ylabel('Days')
        ax3.set_xticks(incr_plot_years)
        ax3.set_xticklabels((incr_plot_years/365).astype('S'), rotation = 45)
        ax3.set_xlim(0,ax3.get_xlim()[1])
        ax3.set_ylim(0,ax3.get_ylim()[1])

Error message:

    287     ax3.set_title(name_MET)
    288     ax3.set_xlabel('years')
--> 289     ax3[1].set_ylabel('Day_since_start_of_simulation')
    290     ax3[2].set_ylabel('Degrees C')
    291     ax3[3].set_ylabel('Degrees C')

TypeError: 'AxesSubplot' object does not support indexing

Can anyone help please how I get to name each y-axis individually? It would help me a lot!

Thank you,

Upvotes: 1

Views: 2108

Answers (2)

Arya McCarthy
Arya McCarthy

Reputation: 8829

You're trying to index into ax3, but ax3 represents one subplot, not the entire plot. Instead, you should label each y-axis on its corresponding loop iteration. Try this:

names_MET = ["Day_since_start_of_sim", "Min._Temp", "Max._Temp", "Radiation","CO2_(ppm)","DOY"]
ylabels = ['Day_since_start_of_simulation', 'Degrees C', 'Degrees C', 'MJ m-2 d-1', 'ppm', 'Days']

for ii, (name_MET, ylabel) in enumerate(zip(names_MET, ylabels)):
    ax3 = fig3.add_subplot(2,3,ii+1)
    ax3.plot(drivers,'g-',label='2001')
    ax3.set_title(name_MET)
    ax3.set_xlabel('years')
    ax3.set_ylabel(ylabel)
    ax3.set_xticks(incr_plot_years)
    ax3.set_xticklabels((incr_plot_years/365).astype('S'), rotation = 45)
    ax3.set_xlim(0,ax3.get_xlim()[1])
    ax3.set_ylim(0,ax3.get_ylim()[1])

The important benefit here is that you get to provide custom labels for the y-axis, instead of having to use the values you defined in names_MET. For instance, the units 'Degrees C' instead of simply 'Min._Temp'.

Upvotes: 1

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

It seems you enumerate the list of names you want to have on the axes, so you can use the current name in each iteration like so:

fig3 = plt.figure(figsize=(20, 14))

names =["Day_since_start_of_sim", "Min._Temp", "Max._Temp", 
        "Radiation","CO2_(ppm)","DAY"]

for ii, name_MET in enumerate (names):
    ax3 = fig3.add_subplot(2,3,ii+1)
    ax3.plot(drivers[ii],'g-',label='2001')# - unclear what drivers is?
    ax3.set_title(name_MET)
    ax3.set_xlabel('years')
    ax3.set_ylabel(name_MET)
    ax3.set_xticks(incr_plot_years) # - unclear
    ax3.set_xticklabels((incr_plot_years/365).astype('S'), rotation = 45)
    ax3.set_xlim(0,ax3.get_xlim()[1])
    ax3.set_ylim(0,ax3.get_ylim()[1])

Upvotes: 1

Related Questions