How to set title and ylims for subplots in seaborn

13,957

You probably want to set the title and the limits on the axes objects themselves using the object oriented API. This means you can control the title etc on an individual subplot which is easier than plt.title when using multiple subplots:

You already have the axes objects when you create the figure fig, ax = plt.subplots(nrows = 2,ncols = 1). Therefore modify the setting of the title and ylim using set_title and set_ylim.

Your code becomes:

TREATMENTINSTIDs = atg_cg.TREATMENTINSTID.unique()
sn.set_style('ticks')
fig, ax = plt.subplots(nrows=2, ncols=1)
fig.set_size_inches(10, 12)
i = 0
# plt.title(TREATMENTINSTID)

for TREATMENTINSTID in TREATMENTINSTIDs:
    ax[i].set_title(TREATMENTINSTID)
    ax[i].set_ylim(0, 1000)
    sn.violinplot(x="group_type", y="arpu", hue='isSMS', ax=ax[i], cut=0,
                  data=atg_cg[atg_cg.TREATMENTINSTID == TREATMENTINSTID], inner="quartile", split=True,
                  title=TREATMENTINSTID)
    sn.despine(left=True)
    i = i + 1
Share:
13,957

Related videos on Youtube

Rocketq
Author by

Rocketq

Updated on June 04, 2022

Comments

  • Rocketq
    Rocketq almost 2 years

    I have such code which draws 2 subplots. I want set ylim and title for both subplots , but it applits only to the last subplot.

       TREATMENTINSTIDs = atg_cg.TREATMENTINSTID.unique()
        sn.set_style('ticks')
        fig, ax = plt.subplots(nrows = 2,ncols = 1)
        fig.set_size_inches(10, 12)
        i = 0
        #plt.title(TREATMENTINSTID)
    
        for TREATMENTINSTID in TREATMENTINSTIDs:
            plt.title(TREATMENTINSTID)
            plt.ylim(0, 1000)
            sn.violinplot(x="group_type", y="arpu" , hue = 'isSMS',ax=ax[i],cut=0, 
                          data=atg_cg[atg_cg.TREATMENTINSTID == TREATMENTINSTID],inner="quartile", split=True, title = TREATMENTINSTID)
            sn.despine(left=True)
            i = i + 1
    

    enter image description here

    What is wrong here? And why first subplot is floating or soaring above x axis?