简体   繁体   中英

Boxplot with different y-axes and different y-scales in seaborn

I am trying to create a boxplot with different y-axes and y-scales in seaborn but got stuck here.

In matplotlib I can use the following code to obtain my result:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


# create random dataframe with different scales
df = pd.DataFrame(np.random.rand(30, 5), columns=['A', 'B', 'C', 'D', 'E'])
df['A'] *= 5
df['C'] *= 10
df['E'] *= 15


# create boxplot with a different y scale for different rows
selection = ['A', 'C', 'E']
fig, ax = plt.subplots(1, len(selection), figsize=(10, len(selection)))
i = 0
for col in selection:
    axo = df[col].plot(kind='box', ax=ax[i], showfliers=False, grid=True)
    axo.set_ylim(df[col].min(), df[col].max())
    axo.set_ylabel(col + ' / Unit')
    i += 1

plt.tight_layout()
plt.show()

which yields:

在此处输入图像描述

I have tried to replace the actual boxplot using a seaborn boxplot but it did not work.

Now my question is: How can I obtain the same plotting result using seaborn?

Thanks in advance for your help!

Cheers Cord

I found a solution on my own:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns


# create random dataframe with different scales
df = pd.DataFrame(np.random.rand(30, 5), columns=['A', 'B', 'C', 'D', 'E'])
df['A'] *= 5
df['C'] *= 10
df['E'] *= 15

# create boxplot with a different y scale for different rows
selection = ['A', 'C', 'E']
fig, axes = plt.subplots(1, len(selection))
for i, col in enumerate(selection):
    ax = sns.boxplot(y=df[col], ax=axes.flatten()[i])
    ax.set_ylim(df[col].min(), df[col].max())
    ax.set_ylabel(col + ' / Unit')
plt.show()

yields:

在此处输入图像描述

Thanks anyway!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM