[英]Multiple boxplots based on conditions
我有一个带有两列的 dataframe。 功率列表示系统的功耗。 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 |
谢谢您的帮助。
您的第一步是根据条件构建数据框。 有几种方法可以go了解一下。
df1
(数据帧 #1)开始。 然后,让我们添加一个condition
列来表示“总计”。 您可以使用print(df1)
来查看它的外观。df2
中,并用component_status
中的关闭/打开条件替换conditions
。df
只是df1
和df2
的串联。df
准备好到 Seaborn 中的 go。# 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])
df
视图 power component_status condition
0 0.5 150 Total
1 1.5 150 Total
2 2.5 150 Total
3 0.3 153 Total
4 0.5 153 Total
5 1.5 153 Total
6 2.5 150 Total
7 0.3 153 Total
0 0.5 150 Off
1 1.5 150 Off
2 2.5 150 Off
3 0.3 153 On
4 0.5 153 On
5 1.5 153 On
6 2.5 150 Off
7 0.3 153 On
# Step 4
ax = sns.boxplot(data=df, x='condition', y='power')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.