Seaborn中具有不同y轴和不同y比例的箱线图

时间:2020-06-16 09:06:34

标签: python seaborn

我正在尝试创建一个在海洋中具有不同y轴和y比例的箱形图,但被卡在这里。

在matplotlib中,我可以使用以下代码来获取结果:

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()

产生:

enter image description here

我尝试使用seaborn boxplot替换实际的boxplot,但没有用。

现在我的问题是:如何使用seaborn获得相同的绘图结果?

在此先感谢您的帮助!

欢呼 脐带

1 个答案:

答案 0 :(得分:0)

我自己找到了一个解决方案:

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()

产量:

enter image description here

还是谢谢!

相关问题