1

I'm looking for a way where I can only plot a specific part of my time series. I have an prediction model via the FB Prophet feature. Please also see the plot below and code below. Any ideas how I could only plot dates between 2019 and 2021?

Plot where I want to visluaize only dates between 2019 and 2021

# plotting of predictions with actual values from 30-09-2020 until 30-10-2020; Combine Prediction and actual values
m_data_DJI_recent.plot(prediction_2)
plt.plot(df_DJI, color = "red")
plt.title("Prediction using the Prophet")
plt.xlabel("Date")
plt.ylabel("Price")
plt.show()
liaison
  • 55
  • 5
  • What part of [plt.xlim()](https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.xlim.html) was the problem? – Mr. T Nov 08 '20 at 12:55
  • I tried also via plt.xlim() but struggled to find correct values. The plot then only showed a plank output. I don't really know what values to put for xlim. The date format used is year-month-day. @Mr.T – liaison Nov 08 '20 at 13:13
  • 1
    Does this answer your question? https://stackoverflow.com/a/21424306/8881141 This is difficult to say without you providing an MCVE. – Mr. T Nov 08 '20 at 13:23

2 Answers2

1

You were almost there with Mr. T's comment, their linked comment has the details. You can use

plt.xlim([datetime.date(2020, 1, 1), datetime.date(2021, 3, 30)])

Replace the dates with the applicable dates for your case

Gramatik
  • 133
  • 1
  • 13
0

You can restrict the data directly like so (just simulated data used therefore a simple index, not a df, blue for reference):

# plotting of predictions with actual values from 30-09-2020 until 30-10-2020; Combine Prediction and actual values
#m_data_DJI_recent.plot(prediction_2)
df_DJI = np.random.randint(0,10,size=20)
plt.plot(df_DJI, color = "blue")
plt.plot(df_DJI[3:], color = "red") # limit range of plot
plt.title("Prediction using the Prophet")
plt.xlabel("Date")
plt.ylabel("Price")
plt.show()

For a dataframe this would look like this for dates:

df.loc['2020-01-01':'2020-02-01']

(or see Filtering Pandas DataFrames on dates for df date restrictions)

GrimTrigger
  • 571
  • 1
  • 5
  • 10
  • This still plots both on the same chart, I believe OP means how to filter the plot to just that timeframe – Gramatik Jun 14 '21 at 21:35