两行之间的Matplotlib Fill_

2024-06-01 15:38:08 发布

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

早上好,我正在尝试使用Matplotlib在两行之间填充_,我是这样做的:

plt.figure(figsize = (16,8));
plt.plot(estudo_df["Sales"], label='Original Data');
plt.plot(forecast_2018["yhat"], color='red', label='Predictions');
plt.plot(forecast_2018["yhat_lower"], color='green', label='Predictions Lower');
plt.plot(forecast_2018["yhat_upper"], color='Blue', label='Predictions Blue');
plt.fill_between(forecast_2018["yhat_lower"],forecast_2018["yhat_upper"] , color='grey', alpha='0.5')
plt.legend();
plt.title('Forecast Sales Prophet - '+sh);
plt.xlabel('Date');
plt.ylabel('Sales');

但它不起作用

我使用两个具有相同金额和“索引ID”的DS,即日期estudo_df和forecast_2018

有人知道错误在哪里吗


Tags: dfplotmatplotlibpltblueupperlowerlabel
1条回答
网友
1楼 · 发布于 2024-06-01 15:38:08

在两个位置更改代码:

  • 通过2018年预测。指数作为之间的填充的第一个参数
  • alpha参数更改为float(您传递了一个字符串

我运行您的代码时只注释了一行:

plt.figure(figsize = (8, 6));
#plt.plot(estudo_df["Sales"], label='Original Data');
plt.plot(forecast_2018["yhat"], color='red', label='Predictions');
plt.plot(forecast_2018["yhat_lower"], color='green', label='Predictions Lower');
plt.plot(forecast_2018["yhat_upper"], color='blue', label='Predictions Blue');
plt.fill_between(forecast_2018.index, forecast_2018["yhat_lower"],
    forecast_2018["yhat_upper"], color='#DDDDDD', alpha=0.5)
plt.legend();
sh = 'xx'
plt.title('Forecast Sales Prophet - ' + sh)
plt.xlabel('Date')
plt.ylabel('Sales');

得到:

enter image description here

我的数据框的索引包含日期,但属于字符串类型

相关问题 更多 >