1

I created a boxplot with matplotlib. Because of one outlier the range of the x-axis is very wide and the box in relation to the axis small. Is there a possibility to interrupt the x-axis f.e. with a double slash "//" or anything else? So that I can see the outlier but get a bigger box.

example dataframe

data = { 'T1': ['ja', 'ja', 'ja', 'ja', 'ja', 'nein', 'nein', 'nein', 'nein', 'nein', 'ja', 'ja', 'ja', 'ja', 'ja', 'nein', 'nein', 'nein', 'nein', 'nein', 'ja', 'ja', 'ja', 'ja', 'ja', 'nein', 'nein', 'nein', 'nein'], 'days': [2000,200,200,400,400,100,500,50,400,600,30,200,200,400,400,100,100,50,400,600,30,200,200,400,400,100,100,50,400]}
df = pd.DataFrame(data)

plot box with seaborn

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

f, ax = plt.subplots(figsize=(15,5))
sns.boxplot(data=df,
            x = df.loc[:,'days'],
            y= df.loc[:,'T1'],
           hue='T1',
            ax=ax)


ax.set_xlabel('days')
ax.legend().remove()
plt.show()

Now the outlier is at 2000 days, for my purpose it is necessary to show this extreme value. But I want to condense the x-axis. So my idea is, to use f.e. // to disrupt the x-axis between 700 and 1750. Has anyone an idea?

After trying to transfer it to my data, following problem popped up: #renamed 2nd column to 'days_after' and create 3rd column

df.loc[:, 'days_before']=0-df.loc[:, 'days_after']

If I tried now to boxplot the same graph with splitted x-axis, it doesn't work.

[![f, (ax1, ax2) = plt.subplots(ncols=2, sharey=True, gridspec_kw={'width_ratios':\[5,1\]}, figsize=(5,2))
sns.boxplot(data=df,
            x = df.loc\[:,'days_before'\],
            y= df.loc\[:,'T1'\],
            hue='T1',
            ax=ax1, showfliers = False)
sns.boxplot(data=df,
            x = df.loc\[:,'days_before'\],
            y= df.loc\[:,'T1'\],
            hue='T1',
            ax=ax2)

ax1.set_xlabel('days')
ax1.legend().remove()
ax2.legend().remove()

ax1.spines\['right'\].set_visible(False)
ax2.spines\['left'\].set_visible(False)
ax2.yaxis.set_visible(False)

ax1.set_xlim(right=-1750)
ax2.set_xlim(left=-750)

plt.show()][2]][2]

2nd try

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Kajette
  • 47
  • 4

1 Answers1

1

One possibility:

f, (ax1, ax2) = plt.subplots(ncols=2, sharey=True, gridspec_kw={'width_ratios':[5,1]}, figsize=(5,2))
sns.boxplot(data=df,
            x = df.loc[:,'days'],
            y= df.loc[:,'T1'],
            hue='T1',
            ax=ax1)
sns.boxplot(data=df,
            x = df.loc[:,'days'],
            y= df.loc[:,'T1'],
            hue='T1',
            ax=ax2)

ax1.set_xlabel('days')
ax1.legend().remove()
ax2.legend().remove()

ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax2.yaxis.set_visible(False)

ax1.set_xlim(right=700)
ax2.set_xlim(left=1750)

plt.show()

Also see the official matplotlib example

Output:

enter image description here

outliers on the left

NB. I inverted the days here to simulate having the outlier on the left (df['days'] *= -1).

You need to change a few things:

# invert the grispec ratios
f, (ax1, ax2) = plt.subplots(ncols=2, sharey=True,
                             gridspec_kw={'width_ratios':[1, 5]}, figsize=(5,2))

# change the limits
ax1.set_xlim(right=-1750)
ax2.set_xlim(left=-700)

Output:

enter image description here

mozway
  • 194,879
  • 13
  • 39
  • 75
  • Thanks! I tried now to at first transfer to a third ax. What causes "gridspec_kw={'width_ratios':[5,1]}"? I try to found out with matplotlib.org, but I didn't understand. – Kajette Apr 28 '23 at 10:26
  • A second question: in my original dataframe the x-axis started with -2000 and ended up with 0. If i Try to transfer the code, I don't succeed to plot it. I swapped the x_lim, but that doesn't work. – Kajette Apr 28 '23 at 10:28
  • The gridspec is to control the size of each subgraph (here 5/6th vs 1/6th of width). In your case you need to swap ax1/ax2, the left/right keywords etc. Provide an updated example if you need help. – mozway Apr 28 '23 at 10:34
  • Thank you so much, now I think I understand how gridspec works. I created a new thread (https://stackoverflow.com/questions/76153613/how-to-combine-2-boxplots-with-customized-x-axis-using-3-axes-with-matplotlib) that is build up on your help here. Maybe you have also an idea to the new one? – Kajette May 02 '23 at 09:43