MatplotLib:stackplot由于具有相同x值的多个y值而删除垂直线

2024-09-30 10:35:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着画两个线条图,下面的区域有阴影。我有三张单子。 其中一个日期。 两个数据集。 每个日期大约有96个值。由于多个y轴(来自两个数据集的值)具有相同的x轴(日期),我认为这会导致图形中出现垂直线来表示这些值

如何让matplotlib绘制平滑线和曲线? 如何删除垂直线而不是显示平均值

list_1= [562.2, 550.8, 531.0, 0.0, .... 524.4, 492.6, 509.4, 502.2, 496.2, 490.2, 4152.48, 149.96, 15.0]
list_2= [562.2, 550.8, 531.0, 0.0, .... 524.4, 492.6, 509.4, 502.2, 496.2, 490.2, 4152.48, 149.96, 15.0]
time = ['11-01', '11-01', '11-01', '11-01', ....  '11-30', '11-30' '11-30', '11-30', '11-30', '11-30', '11-30', '11-30', '12-01']

plt.stackplot(time, current_readings, alpha=0.5, color="#ff7f7f", )
plt.stackplot(time, historic_readings, alpha=0.5, color="#7f7fff",)

当前输出:enter image description here

期望输出: enter image description here


Tags: 数据alpha图形区域timepltlist单子
1条回答
网友
1楼 · 发布于 2024-09-30 10:35:13

如果要显示平均值,可以将它们放入data.frame,将groupby放入data.frame(下面称为avg)。您可以调用plot作为一种方法,我想在您的例子中,您希望stacked=False,以便它们重叠:

import matplotlib.pyplot as plt
import seaborn as sns

Days = pd.date_range('2018-01-01', '2018-01-30',freq='D')
time = np.repeat(Days,5)
current_readings = np.random.uniform(0,500,len(time))
historic_readings = np.random.uniform(0,500,len(time))

df = pd.DataFrame({'time':time,
                   'current_readings':current_readings,
                   'historic_readings':historic_readings})

avg = df.groupby('time').agg('mean')
avg.plot.area(alpha=0.1,stacked=False)

enter image description here

或者使用上面的分组数据框,调用matplotlib:

plt.figure(figsize=(10,5))
plt.stackplot(avg.index, avg['current_readings'], alpha=0.5, color="#ff7f7f" )
plt.stackplot(avg.index, avg['historic_readings'], alpha=0.5, color="#7f7fff")

enter image description here

相关问题 更多 >

    热门问题