Home > OS >  how to move the legends in seaborn plots to the axis ticks
how to move the legends in seaborn plots to the axis ticks

Time:08-11

I have a pandas df as below and I have the following codes to plot in seaborn:

Group   IP  IB  FP  CP  CS  PB
0   1   100 20  50  120 40  90
1   1   100 20  50  120 40  80
2   1   100 20  50  120 40  78
3   1   100 20  50  120 40  70
4   1   100 20  50  120 40  62
... ... ... ... ... ... ... ...
95  18  150 40  50  150 60  50
96  19  200 20  70  150 40  72
97  19  200 20  70  150 40  64
98  19  200 20  70  150 40  74
99  19  200 20  70  150 40  76

df_m = pd.melt(df, id_vars='Group', value_vars=['PB'])
fig, ax = plt.subplots(figsize=(15,10))
sns.stripplot(x='variable', y='value', data=df_m, hue='Group', dodge=True, ax=ax, linewidth=1)

sns.boxplot(x='variable', y='value', data=df_m, hue='Group')

handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[:19], labels[:19], title='Group')

I can get the graph like this: enter image description here

what I want to is to have the legends (groups 1-19) as the x axis ticks. I tried this but the graph becomes squeezed:

ax.set_xticks(range(19))
ax.set_xticklabels(['G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'G10', 
                    'G11', 'G12', 'G13', 'G14', 'G15', 'G16', 'G17', 'G18', 'G19']);

enter image description here

CodePudding user response:

as you are using df_m.variable as x-axis and the value is PB for all, the x-axis has just one entry. So, adding 19 ticks and labels is leading all your boxplots to be compressed around the first entry. So, check the below code, which uses x=Group and that leads to 19 points/ticks for the 19 plots instead of relying on hue. As you have 19 ticks, you can then easily change the tick labels. Although not mentioned, I have gone ahead and updated the legend from 1,2,3... to G1,G2,G3... If not required, you can remove that piece of code. Note that most of my data is based on what you provided and some random numbers, so your plot might look slightly different due to different data points. Hope this is what you are looking for...

df_m = pd.melt(df, id_vars='Group', value_vars=['PB'])
fig, ax = plt.subplots(figsize=(15,10))
sns.boxplot(x='Group', y='value', data=df_m, hue='Group', dodge=False, ax=ax)
sns.stripplot(x='Group', y='value', data=df_m, linewidth=1)

myLabels = ['G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'G10', 
                    'G11', 'G12', 'G13', 'G14', 'G15', 'G16', 'G17', 'G18', 'G19']
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[:19], myLabels, title='Group')
ax.set_xticks(range(19))
ax.set_xticklabels(myLabels)

enter image description here

  • Related