'Correct Year not displayed on matplotlib plot python
So I am using LSTM model for multivariate time series prediction and plotting the predictions using matplotlib but it does not display accurate year which should be 2020 instead starts from 1970. My dataset is yarn market prices for china. Does anyone have any idea how I can fix it?
This is the code at the end for plotting
# ---> Special function: convert <datetime.date> to <Timestamp>
def datetime_to_timestamp(x):
'''
x : a given datetime value (datetime.date)
'''
return datetime.strptime(x.strftime('%Y%m%d'), '%Y%m%d')
y_pred_future = sc_predict.inverse_transform(predictions_future)
y_pred_train = sc_predict.inverse_transform(predictions_train)
PREDICTIONS_FUTURE = pd.DataFrame(y_pred_future, columns=['Cotton Yarn3']).set_index(pd.Series(datelist_future))
PREDICTION_TRAIN = pd.DataFrame(y_pred_train, columns=['Cotton Yarn3']).set_index(pd.Series(datelist_train[2 * n_past + n_future -1:]))
# Convert <datetime.date> to <Timestamp> for PREDCITION_TRAIN
PREDICTION_TRAIN.index = PREDICTION_TRAIN.index.to_series().apply(datetime_to_timestamp)
print(PREDICTION_TRAIN.head(3))
#plt.rcParams["figure.figsize"] = (20,3)
#rcParams['figure.figsize'] = 14, 5
# Plot parameters
START_DATE_FOR_PLOTTING = '2019-12-24'
plt.plot(PREDICTIONS_FUTURE.index, PREDICTIONS_FUTURE['Cotton Yarn3'], color='r', label='Predicted Stock Price')
plt.plot(PREDICTION_TRAIN.loc[START_DATE_FOR_PLOTTING:].index, PREDICTION_TRAIN.loc[START_DATE_FOR_PLOTTING:]['Cotton Yarn3'], color='orange', label='Training predictions')
plt.plot(dataset_train.loc[START_DATE_FOR_PLOTTING:].index, dataset_train.loc[START_DATE_FOR_PLOTTING:]['Cotton Yarn3'], color='b', label='Actual Stock Price')
plt.axvline(x = min(PREDICTIONS_FUTURE.index), color='green', linewidth=2, linestyle='--')
plt.grid(which='major', color='#cccccc', alpha=0.5)
plt.legend(shadow=True)
plt.title('Predcitions and Acutal Stock Prices', family='Arial', fontsize=12)
plt.xlabel('Timeline', family='Arial', fontsize=10)
plt.ylabel('Stock Price Value', family='Arial', fontsize=10)
plt.xticks(rotation=45, fontsize=8)
plt.show()
dataset_train = pd.DataFrame(dataset_train, columns=cols)
dataset_train.index = datelist_train
dataset_train.index = pd.to_datetime(dataset_train.index)
This is the CSV
Date Cotton Yarn1 Cotton Yarn2 Cotton Yarn3 Cotton Yarn4 Cotton Yarn5
12/24/2019 20690 20568 100 122 101
12/25/2019 20660 20538 100 122 101
12/26/2019 20630 20518 100 112 101
12/27/2019 20630 20518 100 112 101
12/28/2019 20630 20518 100 112 101
12/29/2019 20635 20541 100 94 100
12/30/2019 20635 20541 100 94 100
12/31/2019 20700 20583 100 117 101
1/1/2020 20700 20583 100 117 101
1/2/2020 20710 20595 100 115 101
1/3/2020 20715 20604 100 111 101
1/4/2020 20715 20614 100 101 100
1/5/2020 20725 20623 100 102 100
1/6/2020 20725 20659 100 66 100
1/7/2020 20730 20673 101 57 100
1/8/2020 20730 20686 101 44 100
1/9/2020 20730 20686 101 44 100
1/10/2020 20730 20686 101 44 100
1/11/2020 20730 20686 101 44 100
1/12/2020 20730 20686 101 44 100
1/13/2020 20730 20686 101 44 100
1/14/2020 20730 20686 101 44 100
1/15/2020 20730 20686 101 44 100
1/16/2020 20730 20686 101 44 100
1/17/2020 20730 20686 101 44 100
1/18/2020 20730 20686 101 44 100
1/19/2020 20730 20686 101 44 100
1/20/2020 20730 20686 101 44 100
1/21/2020 20730 20686 101 44 100
1/22/2020 20530 20488 100 42 100
1/23/2020 20530 20488 100 42 100
1/24/2020 20530 20488 100 42 100
1/25/2020 20560 20518 100 42 100
1/26/2020 20560 20518 100 42 100
1/27/2020 20560 20518 100 42 100
1/28/2020 20560 20518 100 42 100
1/29/2020 20560 20518 100 42 100
1/30/2020 20560 20518 100 42 100
1/31/2020 20560 20518 100 42 100
2/1/2020 20580 20518 100 62 100
2/2/2020 20580 20518 100 62 100
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
