Search code examples
pythonpandassubplot

python subplot text location when ylim is different


I want to add text in multiple subplot figures when those figures have different ylim.

I want to add text exact same location in each subplot.

However, the problem is each subplot has a different range of y-axis.

enter image description here

My Original Code is this,

    fig = plt.figure(figsize=(20,20))


ax1 = fig.add_subplot(2,3,1)
ax2 = fig.add_subplot(2,3,2)
ax3 = fig.add_subplot(2,3,3)
ax4 = fig.add_subplot(2,3,4)
ax5 = fig.add_subplot(2,3,5)
ax6 = fig.add_subplot(2,3,6)

sns.lineplot( x = 'date',
            y = 'wage',
            data = epi_raw_occ_2018_private[epi_raw_occ_2018_private['docc03']==1],
            label = 'Management occupations',
            ax=ax1)

ax1.axvline(dt.datetime(2020, 3, 1),linewidth=0.5, color='k',linestyle='--')

sns.lineplot( x = 'date',
            y = 'wage',
            data = epi_raw_occ_2018_private[epi_raw_occ_2018_private['docc03']==2],
            label = 'Business and financial operations occupations',
            ax=ax2)

ax2.axvline(dt.datetime(2020, 3, 1),linewidth=0.5, color='k',linestyle='--')


sns.lineplot( x = 'date',
            y = 'wage',
            data = epi_raw_occ_2018_private[epi_raw_occ_2018_private['docc03']==3],
            label = 'Computer and mathematical science occupations',
            ax=ax3)

ax3.axvline(dt.datetime(2020, 3, 1),linewidth=0.5, color='k',linestyle='--')

Here is example of my dataframe

epi_raw_occ_2018_private[['date','wage','docc03']]

epi_raw_occ_2018_private:
date          wage        docc03
200206         40           1
200207         50           1
200208         60           1
.
.
.
200206         30           2
200207         30           2
200208         40           2
.
.
.
200206         10           3
200207         10           3
200208         20           3
.

I am drawing a wage graph according to each Mocc category. However, since the maximum and minimum wage points for each category are different, it is difficult to put text in the place I want.

is there any way to handle this problems?

Thanks in advance


Solution

  • Use annotations to adjust the display position of text annotations. If you specify a value in the range from 0 to 1, based on the lower left corner of the graph, it will be displayed at the same position regardless of the y-axis value. See here for more details.

    fig = plt.figure(figsize=(20,20))
    
    ax1 = fig.add_subplot(2,3,1)
    ax2 = fig.add_subplot(2,3,2)
    ax3 = fig.add_subplot(2,3,3)
    
    sns.lineplot( x = 'date',
                y = 'wage',
                data = df[df['docc03']==1],
                label = 'Management occupations',
                ax=ax1)
    ax1.axvline(dt.datetime(2020, 3, 1),linewidth=0.5, color='k',linestyle='--')
    ax1.annotate('text', (0.8, 0.1), xycoords='axes fraction', fontsize=18, fontweight='bold')
    sns.lineplot( x = 'date',
                y = 'wage',
                data = df[df['docc03']==2],
                label = 'Business and financial operations occupations',
                ax=ax2)
    ax2.axvline(dt.datetime(2020, 3, 1),linewidth=0.5, color='k',linestyle='--')
    ax2.annotate('text', (0.8, 0.1), xycoords='axes fraction', fontsize=18, fontweight='bold')
    
    sns.lineplot( x = 'date',
                y = 'wage',
                data = df[df['docc03']==3],
                label = 'Computer and mathematical science occupations',
                ax=ax3)
    ax3.axvline(dt.datetime(2020, 3, 1),linewidth=0.5, color='k',linestyle='--')
    ax3.annotate('text', (0.8, 0.1), xycoords='axes fraction', fontsize=18, fontweight='bold')
    plt.show()
    

    enter image description here