2014-07-14 47 views
1

我想在繪圖窗口關閉後根據用戶選擇更改繪圖標題。如何在繪圖窗口在Python中關閉後設置繪圖標題

ax = pd.rolling_mean(dataToPlot_plot[startTime:endTime][['plotValue']],mar).plot(linestyle='-', linewidth=3, markersize=9, color='#FECB00') 
    ax.legend().set_visible(False) 
    titObj = plt.title('Data Plot - '+"\n", fontsize=13)#plot title 
    plt.show()#showing the plot 

    fig = ax.get_figure() 
    fig.set_size_inches(12, 6) 
    fig.savefig(savePlot) 

現在我需要改變的情節標題動態基礎上對劇情的用戶選擇,並保存它...

ax = pd.rolling_mean(dataToPlot_plot[startTime:endTime][['plotValue']],mar).plot(linestyle='-', linewidth=3, markersize=9, color='#FECB00') 
    ax.legend().set_visible(False) 
    titObj = plt.title('Data Plot - '+"\n", fontsize=13)#plot title 
    plt.show()#showing the plot 

    curVal = ax.get_xlim() 
    stdate = int(curVal[0]) 
    endate = int(curVal[1]) 
    difdate = endate - stdate 

    fig = ax.get_figure() 
    if stdate > 0 and endate > 0: 
     if difdate > 365: 
      newplotTitle = 'Data Plot - Since from start' 
     elif difdate > 28 and difdate < 365: 
      newplotTitle = 'Data Plot - Past Month' 
     elif difdate < 28: 
      newplotTitle = 'Data Plot - Past Days' 

     plt.title(newplotTitle+"\n", fontsize=13)#plot title 

    fig.set_size_inches(12, 6) 
    fig.savefig(savePlot) 

新的變化不與舊的標題修改,有什麼其他的方法來解決這個問題... 在此先感謝...

回答

1

發生什麼事是plt接口總是指「當前」圖/軸/等。

關閉此圖後,「當前」軸是一個新的空白圖。

對於這個和其他一些原因,這是最好堅持軸方法(除了的功能,如plt.figureplt.subplotsplt.show了一把,等,使處理創建和顯示的數字更容易)。

使用座標軸或圖形對象的方法可以更清楚地顯示正在修改哪些座標軸/圖形的關係。否則,您需要知道「當前」軸是什麼。


在你的情況,而不是做plt.title('blah'),使用ax.set(title='blah')(或ax.set_title)來代替。

+0

謝謝@Joe Kington,很好的幫助......它的工作,也試圖用「fig.suptitle(newplotTitle +」\ n「,fontsize = 13)#plot title」正在工作。感謝您的時間和幫助 –