簡體   English   中英

更新嵌入在 PyQt5 中的 matplotlib 圖

[英]Updating matplotlib graph embedded in PyQt5

我有一個FigureCanvasQTAgg ,我在其中繪制了一個圖表。 然后將其嵌入到QVBoxLayout並且效果非常好。 我在其中繪制圖形的類提供的所有功能都在那里,但每當我在同一窗口中繪制新圖形時,舊圖形仍然在下方可見,如下所示:

在此處輸入圖片說明

在重新繪制之前,我嘗試了多種方法來清除圖形,但結果是相同的。 下面我提供了類的代碼(刪除了一些不相關的方法),因為在主窗口中我只是創建了這個類的一個實例self.pieGraph = DrawSpent(path)

class DrawSpent(FigureCanvas):
    def __init__(self, path=''):
        self.figure = Figure()
        self.drawFigure = self.figure.subplots()
        self.annotate = self.drawFigure.annotate("", xy=(0, 0), xytext=(-20, 20), textcoords="offset points",
                                                 color='#f4b41a',
                                                 bbox=dict(boxstyle="round", fc="#143d59", ec="#28559a", lw=2),
                                                 arrowprops=dict(arrowstyle="->"))
        self.annotate.set_visible(False)
        FigureCanvas.__init__(self, self.figure)
        FigureCanvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
        self.SetFigureAspect()
        self.DrawGraph(path)


    def SetFigureAspect(self):
        self.figure.tight_layout()
        self.figure.set_facecolor('#acc2d9')
        self.figure.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)


    def OpenFile(self, filePath):
        with open(filePath) as graphData:
            graphDict = json.load(graphData)
        return graphDict


    def DrawGraph(self, path, file="skip"):
        if file == "skip":
            path = os.path.join(path, 'Spent\\daily.json')
        else:
            path = os.path.join(path, f'Spent\\{file}.json')

        days, amounts = zip(*self.OpenFile(path).items())
        self.containedData = self.OpenFile(path)
        self.wedges, _ = self.drawFigure.pie(amounts, labels=days, textprops={'visible': False},
                                             colors=pyp.get_cmap('plasma', len(days)).colors)
        self.drawFigure.axis('equal')
        # pyp.show()


    def UpdateNewPlot(self, path): # I tried using this method so I can clear the figure before replotting
        self.figure.canvas.flush_events() # I tried .clf(), .cla() and everything I found online
        self.DrawGraph(path)

抱歉,如果此方法代碼不夠,但我認為如果我還包含一個主窗口,代碼會太多,因為我的項目變得非常大。

如果您仍然為此而苦惱,我只是遇到了同樣的問題並解決了它在Figure()實例上調用clear()以在重新繪制之前刪除整個圖形。 即,這樣的事情應該工作:

編輯:

    def UpdateNewPlot(self, path):
        self.figure.clear()
        self.plot(<your arguments>)
        
    def plot(self, <your parameters>):
        self.drawFigure = self.figure.subplots()
        self.annotate = self.drawFigure.annotate("", xy=(0, 0), xytext=(-20, 20), textcoords="offset points",
                                                 color='#f4b41a',
                                                 bbox=dict(boxstyle="round", fc="#143d59", ec="#28559a", lw=2),
                                                 arrowprops=dict(arrowstyle="->"))
        self.annotate.set_visible(False)
        FigureCanvas.__init__(self, self.figure)
        FigureCanvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
        self.SetFigureAspect()
        self.DrawGraph(path)

暫無
暫無

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

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