liaison
liaison

Reputation: 55

Select specific part of x-axis in Plot

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()

Upvotes: 1

Views: 2889

Answers (2)

Gramatik
Gramatik

Reputation: 133

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

Upvotes: 1

GrimTrigger
GrimTrigger

Reputation: 591

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)

Upvotes: 0

Related Questions