繁体   English   中英

基于条件的多个箱线图

[英]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了解一下。

  1. 让我们从您提供的初始df1 (数据帧 #1)开始。 然后,让我们添加一个condition列来表示“总计”。 您可以使用print(df1)来查看它的外观。
  2. 然后让我们将 dataframe 复制到df2中,并用component_status中的关闭/打开条件替换conditions
  3. 我们最终的 dataframe df只是df1df2的串联。
  4. 现在我们有一个 dataframe df准备好到 Seaborn 中的 go。

进口及DataFrame

# 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.

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