0

I created this function

def plotPrediction(testY, predictY, testYIndex, predictIndex, fileName):
    # Use datetime for creating date objects for plotting
    # Plot the actual values
    plt.plot(testY.index, testY[testYIndex], 'b-', label = 'actual');
    # Plot the predicted values
    plt.plot(testY.index, predictY[:, predictIndex] , 'ro', label = 'prediction')
    plt.xticks(rotation = '60'); 
    plt.legend()
    # Graph labels
    plt.xlabel('Date'); plt.ylabel(testYIndex); plt.title('Actual and Predicted DepositCount');
    plt.savefig(fileName+testYIndex+'.png')

Then I call this function by doing:

plotPrediction(testY, predictY, 'DepositCount',0, 'forestpredict');
plotPrediction(testY, predictY, 'DepositAmount',1, 'forestpredict');
plotPrediction(testY, predictY, 'WithdrawCount',2, 'forestpredict');
plotPrediction(testY, predictY, 'WithdrawAmount',3, 'forestpredict');

My idea was that as my predictY has multiple outputs, I would like to plot each output in different png file. However, the output files show the same image (other than first one). I am guessing that I need to clean the canvas or maybe there are some functions to tell matplotlib to start a new chart?

daxu
  • 3,514
  • 5
  • 38
  • 76

1 Answers1

0

You could run plt.cla() and plt.clf() to clear the axis and figure before generating the next plot; I usually put these at the top of my plotting functions, which would be plotPrediction in your case.

See When to use cla(), clf() or close() for clearing a plot in matplotlib?.

Shashank Agarwal
  • 2,769
  • 1
  • 22
  • 24