matplotlib错误地绘制了多个文件

时间:2018-10-15 16:35:41

标签: python matplotlib

我创建了此功能

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')

然后我通过执行以下操作来调用此函数:

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

我的想法是,由于我的ForecastY具有多个输出,因此我想将每个输出绘制在不同的png文件中。但是,输出文件显示相同的图像(第一个图像除外)。我猜我需要清洁画布,或者也许有一些功能告诉Matplotlib启动新图表?

1 个答案:

答案 0 :(得分:0)

您可以运行plt.cla()plt.clf()来清除轴和图形,然后再生成下一个图。我通常将它们放在绘图功能的顶部,在您的情况下为plotPrediction

请参见When to use cla(), clf() or close() for clearing a plot in matplotlib?