簡體   English   中英

seaborn 中不同 y 軸和不同 y 比例的箱線圖

[英]Boxplot with different y-axes and different y-scales in seaborn

我正在嘗試在 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()

產生:

在此處輸入圖像描述

我嘗試使用 seaborn 箱線圖替換實際的箱線圖,但它沒有用。

現在我的問題是:如何使用 seaborn 獲得相同的繪圖結果?

在此先感謝您的幫助!

歡呼線

我自己找到了解決方案:

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

產量:

在此處輸入圖像描述

不管怎么說,還是要謝謝你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM