简体   繁体   English

seaborn 中不同 y 轴和不同 y 比例的箱线图

[英]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.我正在尝试在 seaborn 中创建一个具有不同 y 轴和 y 比例的箱线图,但被困在这里。

In matplotlib I can use the following code to obtain my result:在 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()

which yields:产生:

在此处输入图像描述

I have tried to replace the actual boxplot using a seaborn boxplot but it did not work.我尝试使用 seaborn 箱线图替换实际的箱线图,但它没有用。

Now my question is: How can I obtain the same plotting result using seaborn?现在我的问题是:如何使用 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!不管怎么说,还是要谢谢你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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