0

I have a FigureCanvasQTAgg in which I plot a graph. That is then embedded into a QVBoxLayout and works really well. All functionality provided by the class I draw my graph in is there, with the exception that whenever I draw a new graph in the same window, the old one is still visible underneath, like so:

enter image description here

I tried in any number of ways to clear the figure before replotting but the result is the same. Below I have provided the code for the class(with a few unrelated methods removed), as in the main window I simply create an instance of this class 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)

Sorry if this method is not enough code but I figured there would be too much code if I were to also include a main window as my project is getting quite large.

1 Answer 1

0

In case you still struggle with this, I just had the same problem and solved it calling clear() on the Figure() instance to delete the whole graph before replotting again. I.e., something like this should work:

Edit:

    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)
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't seem to work neither by clearing self.figure nor self.drawFigure
Check if the order in which you call UpdateNewPlot is correct. In the snippet you shared it's not present anywhere
I have mistakenly removed that part. Unfortunately, it still doesn't work.
Ok, your DrawGraph() function doesn't really draw anything as is. You are plotting in __init__. Make a different method containing that code (see edit) and invoke it in your __init__ and UpdateNewPlot methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.