0

I need to delete a barchart from the subplot, in order to plot new data on the subplot.

The code I have is as follows:

self.barcontainer = tk.Frame(self)
self.barfigure = Figure(figsize = ((7.5,6.3)),dpi = 100)
self.baraxes = self.barfigure.add_subplot(111)
self.baraxes.set_ylabel('Cases')
self.baraxes.set_xlabel('State/Region')
updateddata = confirmedstates[confirmedstates.columns[-1]]
x = np.arange(len(namelist))
self.baraxes.bar(x,updateddata, label = namelist)
self.barcanvas = FigureCanvasTkAgg(self.barfigure,self.barcontainer)
barcanvas.draw()
barcanvas.get_tk_widget().pack(side = tk.TOP, fill = tk.BOTH, expand = True)

I know that for a normal lineplot, we can remove it using axes_name.lines.remove(), but for a bar chart, the .lines attribute returns an empty list. How can we delete off a bar chart?

  • Does this answer your question? [How to remove the previous drawing in matplotlib, Python?](https://stackoverflow.com/questions/18075721/how-to-remove-the-previous-drawing-in-matplotlib-python) – Joe Apr 25 '20 at 09:43

1 Answers1

0

I think the easiest way is to give a variable name to you bar artist and call the remove method on it. For example:

import matplotlib.pyplot as plt
barArt = plt.bar(...)
barArt.remove()

More information here.

Patol75
  • 4,342
  • 1
  • 17
  • 28