matplotlibseabornbar-chartsubplotpointplot

x axis value ranges not sequential in seaborn barplot & pointplot as subplots


My data frames are:

df['graph_df_uni_valid']

             group  MO_SNCE_REC_APP     Label  predictions
0  (-0.001, 25.0]            24324  0.042551     0.042118
1    (25.0, 45.0]            24261  0.035077     0.033748
2    (45.0, 64.0]            23000  0.033391     0.033354
3    (64.0, 83.0]            22960  0.028876     0.028351
4   (83.0, 118.0]            23725  0.028872     0.029056
5  (118.0, 174.0]            23354  0.021024     0.022121
6            miss                0  0.009165     0.008978

df['graph_df_uni_oot']

             group  MO_SNCE_REC_APP     Label  predictions
0  (-0.001, 25.0]            28942  0.033308     0.041806
1    (25.0, 44.0]            28545  0.027921     0.034701
2    (44.0, 64.0]            27934  0.026634     0.033682
3    (64.0, 83.0]            27446  0.021132     0.028101
4   (83.0, 119.0]            28108  0.022236     0.028721
5  (119.0, 171.0]            27812  0.015892     0.020897
6            miss                0  0.007614     0.009352

Issue is x-axis of Test (& OOT) plot is not in sequential order i.e. bin (11.0 – 102.0] should be the last, NOT 2nd in sequence. enter image description here

My data is in correct sequence so I used sort=False for pointplot (or lineplot) and order=df['graph_df_uni_valid'].sort_values(by='group').group for barplot. But I get same unordered x-axis with/without these parameters.

Here is my code:

    fig, ax = plt.subplots(nrows = 1, ncols = 2, figsize = (12,5), sharex = False, sharey = True, tight_layout = True)
    fig.supxlabel(desc, ha = 'center', wrap = True)
    fig.suptitle(f"{col} (Rank #{rank}, TotGain: {totgain}, Cum TotGain: {cumtotgain})", fontsize = 16)
  
    ax1_line = ax[0].twinx()
    ax2_line = ax[1].twinx()

   
    
    ax2_line.get_shared_y_axes().join(ax1_line,ax2_line)

    ax[0] = sns.barplot(data = df['graph_df_uni_valid'], ax = ax[0], x = 'group', y = col, color = 'blue', order=df['graph_df_uni_valid'].sort_values(by='group').group)
    ax[0].set(xlabel = '', ylabel = 'Count')
    ax[0].tick_params(axis = 'x', rotation = 60)

    ax1_line = sns.pointplot(data = df['graph_df_uni_valid'], ax = ax1_line, x = 'group', y = target, sort= False, color = 'red', marker = '.')    
    ax1_line = sns.pointplot(data = df['graph_df_uni_valid'], ax = ax1_line, x = 'group', y = sc, sort= False, color = 'green', marker = '.')
    ax1_line.set(xlabel = '', ylabel = 'Book Rate/Score')
    ax[0].set_title('Test (202205 - 202208)')

    ax[1] = sns.barplot(data = df['graph_df_uni_oot'], ax = ax[1], x = 'group', y = col, color = 'blue', order=df['graph_df_uni_oot'].sort_values(by='group').group)
    ax[1].set(xlabel = '', ylabel = 'Count')
    ax[1].tick_params(axis = 'x', rotation = 60)

    ax2_line = sns.pointplot(data = df['graph_df_uni_oot'], x = 'group', y = target, sort= False, color = 'red', marker = '.')
    ax2_line = sns.pointplot(data = df['graph_df_uni_oot'], ax = ax2_line, x = 'group', y = sc, sort=False, color = 'green', marker = '.')
    ax2_line.set(xlabel = '', ylabel = 'Book Rate/Score')    
    ax[1].set_title('OOT (202204)')

If I change barplot parameter order=df['graph_df_uni_valid'].index, I get desired x-axis sequence but bars disappears. enter image description here

versions

2nd Question How to add legend that red line is 'Book rate', green line is 'Score' & blue bars are volume


Solution

  • As my data was in correct sequence so I just have to use sort=False for pointplot (or lineplot) and no order parameter for barplot. I get x-axis in correct order.

    enter image description here