簡體   English   中英

可以在matplotlib / pyplot中創建一個圖,但不能創建另一個圖

[英]Can create one figure but not another in matplotlib/pyplot

我正在嘗試使用PyPlot在單個窗口中顯示多個圖形。 我可以使用以下代碼做到這一點:

def create_figure_one(self):
    plt.figure(1)
    plt.subplot(311)
    plt.plot_date(self.dates, self.PREC, '-', color='b')
    plt.title('Precipitation', fontsize=20)
    plt.ylabel('MM/DT', fontsize=15)
    plt.tick_params(axis='both', which='major', labelsize=10)
    plt.tick_params(axis='both', which='minor', labelsize=10)
    plt.grid()

    plt.subplot(312)
    plt.plot_date(self.dates, self.PET, '-', color='b')
    plt.plot_date(self.dates, self.AET, '-', color='r')
    plt.title('Evapotranspiration', fontsize=20)
    plt.ylabel('MM/DT', fontsize=15)
    plt.tick_params(axis='both', which='major', labelsize=10)
    plt.tick_params(axis='both', which='minor', labelsize=10)
    red_patch = mpatches.Patch(color='blue', label='Potential')
    blue_patch = mpatches.Patch(color='red', label='Actual')
    plt.legend(handles=[red_patch, blue_patch])
    plt.grid()

    plt.subplot(313)
    plt.plot_date(self.dates, self.Q, '-', color='b')
    plt.title('Flow', fontsize=20)
    plt.ylabel('CMS', fontsize=15)
    plt.xlabel('Time', fontsize=15)
    plt.tick_params(axis='both', which='major', labelsize=10)
    plt.tick_params(axis='both', which='minor', labelsize=10)
    plt.grid()

    plt.show()

單擊我的GUI中的按鈕后,將調用此函數。 同樣,我的GUI中還有另一個按鈕可以調用另一個函數:

def create_figure_two(self): 
    plt.figure(1)
    #UZTWC
    plt.subplot(611)
    plt.plot_date(self.dates, self.UZTWC, '-', color='b')
    self.title('UZTWC', fontsize=15)
    plt.ylabel('MM', fontsize=10)
    plt.tick_params(axis='both', which='major', labelsize=10)
    plt.tick_params(axis='both', which='minor', labelsize=10)
    plt.grid()

    #UZFWC 
    plt.subplot(612)
    plt.plot_date(self.dates, self.UZFWC, '-', color='b')
    self.title('UZFWC', fontsize=15)
    plt.ylabel('MM', fontsize=10)
    plt.tick_params(axis='both', which='major', labelsize=10)
    plt.tick_params(axis='both', which='minor', labelsize=10)
    plt.grid()

    #LZTWC 
    plt.subplot(613)
    plt.plot_date(self.dates, self.LZTWC, '-', color='b')
    self.title('LZTWC', fontsize=15)
    plt.ylabel('MM', fontsize=10)
    plt.tick_params(axis='both', which='major', labelsize=10)
    plt.tick_params(axis='both', which='minor', labelsize=10)
    plt.grid()

    #LZFPC 
    plt.subplot(614)
    plt.plot_date(self.dates, self.LZFPC, '-', color='b')
    self.title('LZFPC', fontsize=15)
    plt.ylabel('MM', fontsize=10)
    plt.tick_params(axis='both', which='major', labelsize=10)
    plt.tick_params(axis='both', which='minor', labelsize=10)
    plt.grid()

    #LZFSC 
    plt.subplot(615)
    plt.plot_date(self.dates, self.LZFSC, '-', color='b')
    self.title('LZFSC', fontsize=15)
    plt.ylabel('MM', fontsize=10)
    plt.tick_params(axis='both', which='major', labelsize=10)
    plt.tick_params(axis='both', which='minor', labelsize=10)
    plt.grid()

    #ADIMC 
    plt.subplot(616)
    plt.plot_date(self.dates, self.ADIMC, '-', color='b')
    self.title('ADIMC', fontsize=15)
    plt.ylabel('MM', fontsize=10)
    plt.tick_params(axis='both', which='major', labelsize=10)
    plt.tick_params(axis='both', which='minor', labelsize=10)
    plt.xlabel('Time', fontsize=10)
    plt.grid()
    plt.show()

但是什么也沒發生。 我的終端沒有任何錯誤,我的程序沒有終止,並且沒有顯示圖形的窗口。 我看不到我的兩個功能之間的差異可能可以解釋為什么第一個有效而第二個無效。

self.dates:

    self.list_of_datetimes = []
    skipped_header = False;
    with open(data_file, 'rt') as f:
        reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE)
        for row in reader:
            if skipped_header:
                date_string = "%s/%s/%s %s" % (row[0].strip(), row[1].strip(), row[2].strip(), row[3].strip())
                dt = datetime.strptime(date_string, "%Y/%m/%d %H")
                self.list_of_datetimes.append(dt)
            skipped_header = True

    self.dates = matplotlib.dates.date2num(self.list_of_datetimes)

如果有人有任何見識,將不勝感激。

我不是一個聰明人...在第二個數字中我應該有“ self.title”,應該有“ plt.title”。 這樣就解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM