0

Here is my code and I have achieve simple graph but I want to elaborate it more.

temp_df = pd.read_excel('xTraders_Temperature_Wind.xlsx', sheet_name = 'Temperature')
temp_df = temp_df.drop(columns=['Timestamp end']).set_index('Timestamp start').dropna(subset = \
    ['Average forecast GEFS Turkey National (ºC)', 'Observation Turkey National (ºC)'])


year_data = temp_df.index 
actual_data = temp_df['Observation Turkey National (ºC)']
predicted_data = temp_df['Average forecast GEFS Turkey National (ºC)']

plt.plot(year_data, actual_data, color='red', linewidth = 1)
plt.plot(year_data, predicted_data, color='yellow', linewidth = 0.1)

plt.xlabel('Time')
plt.ylabel('Potential Temperature')
plt.title('Foreast and Observation Temperature')
plt.show()

These are my x-axis:

[2017-01, 2017-05, 2017-09, 2018-01, 2018-05, 2018-09, 2019-01, 2019-05, 2019-09, 2020-01]

(My desired x-axis) I want to add more months like this:

[2017-01, 2017-02, 2017-03, 2017-04, 2017-05, 2017-06, 2017-07, 2017-08, 2017-09, 2017-10, 2017-11, 2017-12, \
2018-01, 2018-02, 2018-03, 2018-04, 2018-05, 2018-06, 2018-07, 2018-08, 2018-09, 2018-10, 2018-11, 2018-12, \
2019-01, 2019-02, 2019-03, 2019-04, 2019-05, 2019-06, 2019-07, 2019-08, 2019-09, 2019-10, 2019-11, 2019-12]

How can I achieve that?

JuniorESE
  • 267
  • 1
  • 7

1 Answers1

0

Try using Series.reindex.

idx = pd.date_range('01-2017', '12-2019')

temp_df.index = pd.DatetimeIndex(temp_df.index)

temp_df = temp_df.reindex(idx, fill_value=0)
Dvir Cohen
  • 106
  • 1
  • 3
  • What about the nan values because I droped some nan values. If I use your way, Wouldn't I make a mistake? – JuniorESE Jan 27 '20 at 12:00