基于不同条件的 Seaborn 多重箱线图
Seaborn multiple boxplot based by different conditions
我有一个包含两列的数据框。功率列表示系统的功耗。 component_status 列根据组件关闭或打开的时间将数据一分为二。当值为 153 时组件打开,当值为 150 时组件关闭。
我正在寻找的结果是使用 sns.boxplot
得到一个包含三个箱线图的箱线图。一个是包含所有数据的功耗,称为“TOTAL”。另外两个,基于组件关闭或打开的功耗,称为“COMPONENT = ON”“COMPONENT = OFF”。
数据框示例如下:
power|component_status |
0.5 | 150 |
1.5 | 150 |
2.5 | 150 |
0.3 | 153 |
0.5 | 153 |
1.5 | 153 |
2.5 | 150 |
0.3 | 153 |
感谢您的帮助。
您的第一步是根据条件构建数据框。有几种方法可以解决这个问题。
让我们从您提供的初始 df1
(数据帧 #1)开始。然后,让我们添加一个 condition
列来表示“总计”。您可以使用 print(df1)
来查看它的样子。
然后让我们将该数据帧复制到 df2
,然后用 component_status
.[=23 中的 off/on 条件替换 conditions
=]
我们的最终数据框 df
只是 df1
和 df2
.
的串联
现在我们有一个数据框 df
可以在 Seaborn 中使用了。
### Set up
import pandas as pd
import numpy as np
import seaborn as sns
power = [0.5, 1.5, 2.5, 0.3, 0.5, 1.5, 2.5, 0.3]
component_status = [150, 150, 150, 153, 153, 153, 150, 153]
df1 = pd.DataFrame(
data=zip(power, component_status), columns=["power", "component_status"]
)
### Step 1
df1["condition"] = "Total"
# print(df1)
### Step 2
df2 = df1.copy()
df2["condition"] = np.where(df2["component_status"] == 153, "On", "Off")
### If you have several criteria, it can be easier to use np.select
### ... or just use Pandas directly:
# df2.loc[(df2['component_status'] == 153), 'condition'] = 'On'
# df2.loc[(df2['component_status'] == 150), 'condition'] = 'Off'
### Step 3
df = pd.concat([df1,df2])
print(df)
### Step 4
sns.boxplot(data=df, x='condition', y='power')
我有一个包含两列的数据框。功率列表示系统的功耗。 component_status 列根据组件关闭或打开的时间将数据一分为二。当值为 153 时组件打开,当值为 150 时组件关闭。
我正在寻找的结果是使用 sns.boxplot
得到一个包含三个箱线图的箱线图。一个是包含所有数据的功耗,称为“TOTAL”。另外两个,基于组件关闭或打开的功耗,称为“COMPONENT = ON”“COMPONENT = OFF”。
数据框示例如下:
power|component_status |
0.5 | 150 |
1.5 | 150 |
2.5 | 150 |
0.3 | 153 |
0.5 | 153 |
1.5 | 153 |
2.5 | 150 |
0.3 | 153 |
感谢您的帮助。
您的第一步是根据条件构建数据框。有几种方法可以解决这个问题。
让我们从您提供的初始
df1
(数据帧 #1)开始。然后,让我们添加一个condition
列来表示“总计”。您可以使用print(df1)
来查看它的样子。然后让我们将该数据帧复制到
df2
,然后用component_status
.[=23 中的 off/on 条件替换conditions
=]我们的最终数据框
的串联df
只是df1
和df2
.现在我们有一个数据框
df
可以在 Seaborn 中使用了。### Set up import pandas as pd import numpy as np import seaborn as sns power = [0.5, 1.5, 2.5, 0.3, 0.5, 1.5, 2.5, 0.3] component_status = [150, 150, 150, 153, 153, 153, 150, 153] df1 = pd.DataFrame( data=zip(power, component_status), columns=["power", "component_status"] ) ### Step 1 df1["condition"] = "Total" # print(df1) ### Step 2 df2 = df1.copy() df2["condition"] = np.where(df2["component_status"] == 153, "On", "Off") ### If you have several criteria, it can be easier to use np.select ### ... or just use Pandas directly: # df2.loc[(df2['component_status'] == 153), 'condition'] = 'On' # df2.loc[(df2['component_status'] == 150), 'condition'] = 'Off' ### Step 3 df = pd.concat([df1,df2]) print(df) ### Step 4 sns.boxplot(data=df, x='condition', y='power')