简体   繁体   English

Matplotlib Fill_Between 两行

[英]Matplotlib Fill_Between Two Lines

Good Morning, I'm Trying to use Matplotlib to Fill_between two Lines and i'm doing like this:早上好,我正在尝试使用 Matplotlib 来 Fill_between 两行,我正在这样做:

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');

But it is not working...但它不起作用......

I use two DS that have the same amount and "index ID", which is a DATE, estudo_df and forecast_2018我使用两个具有相同数量和“索引 ID”的 DS,即 DATE、estudo_df 和 forecast_2018

Does anyone knows where the error is?有谁知道错误在哪里?

Change your code in 2 places:在 2 个地方更改您的代码:

  • pass forecast_2018.index as the first parameter of fill_between ,通过forecast_2018.index作为fill_between第一个参数,
  • change alpha parameter to float (you passed a string ).alpha参数更改为float (您传递了一个string )。

I ran your code with one line commented out:我用注释掉了一行来运行你的代码:

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');

and got:并得到:

在此处输入图片说明

The index of my DataFrame contains dates but is of string type.我的 DataFrame 的索引包含日期但属于字符串类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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