You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用ggplot2叠加绘制Predicted RUL与Actual RUL对比图?

Predicted RUL 与 Actual RUL 对比可视化方案

下面提供两种常用的Python实现方法,帮你完成每个Engine对应两组数据的对比展示:

方法1:Matplotlib 分组柱状图(手动控制布局)

适合需要精细调整样式的场景,代码逻辑清晰:

import pandas as pd
import matplotlib.pyplot as plt

# 替换为你的实际数据加载代码,比如 pd.read_csv("your_data.csv")
data = {
    'Engine': range(1, 101),
    'Predicted RUL': [172, 126, 27, 52, 84] + list(range(10, 60, 1))*19,
    'Actual RUL': [112, 98, 69, 82, 91] + list(range(20, 70, 1))*19
}
df = pd.DataFrame(data)

# 设置柱子宽度与x轴位置,实现并排效果
bar_width = 0.35
x_indices = range(len(df['Engine']))

plt.figure(figsize=(12, 6))
# 绘制预测值柱子
plt.bar([i - bar_width/2 for i in x_indices], df['Predicted RUL'], width=bar_width, label='Predicted RUL')
# 绘制实际值柱子
plt.bar([i + bar_width/2 for i in x_indices], df['Actual RUL'], width=bar_width, label='Actual RUL')

# 配置轴标签与样式
plt.xticks(x_indices, df['Engine'], rotation=45, fontsize=8)
plt.xlabel('Engine')
plt.ylabel('RUL')
plt.title('Predicted vs Actual RUL by Engine')
plt.legend()

plt.tight_layout()
plt.show()

方法2:Seaborn 分组柱状图(简洁高效)

利用Seaborn的内置功能,无需手动调整柱子位置,只需先将数据转为长格式:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# 加载数据(同上)
data = {
    'Engine': range(1, 101),
    'Predicted RUL': [172, 126, 27, 52, 84] + list(range(10, 60, 1))*19,
    'Actual RUL': [112, 98, 69, 82, 91] + list(range(20, 70, 1))*19
}
df = pd.DataFrame(data)

# 将宽格式数据转为长格式,适配Seaborn
df_melted = df.melt(
    id_vars='Engine',
    value_vars=['Predicted RUL', 'Actual RUL'],
    var_name='RUL Category',
    value_name='RUL Value'
)

plt.figure(figsize=(12, 6))
sns.barplot(x='Engine', y='RUL Value', hue='RUL Category', data=df_melted)

# 配置样式
plt.xlabel('Engine')
plt.ylabel('RUL')
plt.title('Predicted vs Actual RUL by Engine')
plt.xticks(rotation=45, fontsize=8)
plt.tight_layout()
plt.show()

可选:叠加柱状图

如果需要将两组数据堆叠展示(而非并排),可以使用Matplotlib的bottom参数:

import pandas as pd
import matplotlib.pyplot as plt

# 加载数据(同上)
data = {
    'Engine': range(1, 101),
    'Predicted RUL': [172, 126, 27, 52, 84] + list(range(10, 60, 1))*19,
    'Actual RUL': [112, 98, 69, 82, 91] + list(range(20, 70, 1))*19
}
df = pd.DataFrame(data)

plt.figure(figsize=(12, 6))
# 先绘制实际值作为基底
plt.bar(df['Engine'], df['Actual RUL'], label='Actual RUL')
# 将预测值叠加在实际值上方
plt.bar(df['Engine'], df['Predicted RUL'], bottom=df['Actual RUL'], label='Predicted RUL')

plt.xlabel('Engine')
plt.ylabel('RUL')
plt.title('Predicted vs Actual RUL (Stacked)')
plt.legend()
plt.xticks(rotation=45, fontsize=8)
plt.tight_layout()
plt.show()

内容的提问来源于stack exchange,提问作者benny86

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 00:01:11