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:
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.
