0

I'm trying to plot two figures, one which plots as expected, but the other which is very similar, is blank with nothing in it. I want plot2 to include the data points in plot1 and the few extra data points in df2. plot1 goes up to the date 12/01/2019 and I want plot2 to include those points and points beyond that date (which are in df2). The second plot also throws the error ValueError: view limit minimum -36839.7000 is less than 1 and is an invalid Matplotlib date value. According to this, to address this error one needs to check if the 'datetime' column is a datetime64[ns] type, which I checked and it is. The code is as follows:

###First plot
plot1 = df1.plot()
plot1.set_xlabel('Date')
plot1.yaxis.set_major_formatter(PercentFormatter(xmax=1,decimals=0))
leg = plot1.legend()
leg.get_texts()[0].set_text('Actual')
leg.get_texts()[1].set_text('Predicted')
plt.show()
plt.savefig('C:/Documents/Fig1.png')

###Second plot, which shows a plot that is blank
df2['Date'] = pd.to_datetime(df2['Date'])
print(df2.info()) ###confirms that 'Date' is in fact datetime64[ns] date type
plot2 = df2.plot()
plt.axvline(len(df1.index), color = 'b')
plot2.set_xlabel('Date')
plot2.yaxis.set_major_formatter(PercentFormatter(xmax=1,decimals=0))
leg = plot2.legend()
leg.get_texts()[0].set_text('Actual')
leg.get_texts()[1].set_text('Predicted')
plt.show()
plt.savefig('C:/Documents/Fig2.png')
print("df2 data points begin at: ",df1.index.max())

Part of df2 looks like:

Date        Predicted
01/01/2020  756.42
02/01/2020  748.14
03/01/2020  709.11

What could be causing the ValueError and why is the 2nd figure blank?

Jojo
  • 1,117
  • 2
  • 15
  • 28
  • 2
    There is some incompatibility between pandas and matplotlib time ordinal representation. You cannot use simultaneously (in the same axes) DataFrame.plot and pyplot methods. Choose one way to plot and stick to it, it will solve your problem. – jlandercy Jan 22 '21 at 06:29
  • 1
    You are welcome, I faced this one many years ago (https://stackoverflow.com/q/43891136/3067485), glad you solve this problem. – jlandercy Jan 23 '21 at 05:37

0 Answers0