首页 > 解决方案 > Matplotlib plt 对象在绘图间共享

问题描述

我有 2 个函数,一个绘制时间序列线,另一个绘制自相关。

def plotacorr(dfasst):
    # Plot autocorrelation
    plt.acorr(dfasst, maxlags=3)
    # Add labels to autocorrelation plot
    plt.title('Autocorrelation of Asset Balances with previous Months Balances')
    plt.xlabel('Lag in Months')
    plt.ylabel('Autocorrelation')

    # Display the autocorrelation plot
    #plt.show()
    plt.savefig('C:/acorr_assets.jpeg')

def plottrend(df_acctsmry2):
    fig, ax = plt.subplots()
    fmt = '${x:,.0f}'
    tick = mtick.StrMethodFormatter(fmt)
    ax.yaxis.set_major_formatter(tick) 

    df_acctsmry2.plot(x='REPORTING_DATE',ax=ax,figsize=(20,12))
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    plt.xlabel('REPORTING_DATE', fontsize=18)
    #plt.show()
    plt.savefig('C:/output.jpeg')

我首先调用 plottrend,然后调用 plotacorr 但似乎 plt 对象以某种方式在 2 个图之间共享,因此在自相关图中,我看到与 plottrend 相同的结果。

标签: matplotlib

解决方案


取消注释plt.show()每个函数,应该这样做(或plt.show()在调用函数之间调用 a )。


推荐阅读