I want to split the following data into two parts: observed
from 2018-09
to 2019-11
and predicted
from 2019-12
to the end of date column, plot them with solid and dashed lines respectively with matplotlib
, plotly
or seaborn
, etc.
date price pct
0 2018-09 10.599 0.020
1 2018-10 10.808 0.020
2 2018-11 10.418 -0.036
3 2018-12 10.166 -0.024
4 2019-01 9.995 -0.017
5 2019-02 10.663 0.067
6 2019-03 10.559 -0.010
7 2019-04 10.055 -0.048
8 2019-05 10.691 0.063
9 2019-06 10.766 0.007
10 2019-07 10.667 -0.009
11 2019-08 10.504 -0.015
12 2019-09 10.284 -0.021
13 2019-10 10.047 -0.023
14 2019-11 9.717 -0.033
15 2019-12 9.908 -0.029
16 2020-01 9.570 -0.045
17 2020-02 9.754 -0.023
18 2020-03 9.779 -0.025
19 2020-04 9.777 -0.031
20 2020-05 9.932 -0.020
I have tried with code as follows, firstly I get an error, second I didn't plot pct
yet. Someone could help ? Thank you.
df = df.set_index('date')
plt.plot('date', 'price', data=df.loc['2018-09':'2019-11'], marker='o', color='green', linewidth=2)
plt.plot('date', 'price', data=df.loc['2019-12':], marker='o', color='green', linewidth=2, linestyle = '--')
It generates ValueError: x and y must have same first dimension, but have shapes (1,) and (15,)
EDIT: this code have successfully draw the plot for price
, but I need to draw pct
on the same plot.
df['date'] = pd.to_datetime(df['date'])
# https://stackoverflow.com/questions/46230864/split-dataframe-on-the-basis-of-date
split_date ='2019-12-01'
plt.figure(figsize=(10, 5))
plt.plot('date', 'rent_price', data = df.loc[df['date'] <= split_date], marker='o', color='red', linewidth=2)
plt.plot('date', 'rent_price', data = df.loc[df['date'] >= split_date], marker='o', color='green', linewidth=2, linestyle = '--')