Dear matplotlib community,
I am facing an annoying issue in combining host_subplot with artist.remove(). This code is an enormous simplification of the app I am porting to Python 3:
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import numpy as np
import wx
class CanvasFrame(wx.Frame):
def __init__(self):
super().__init__(None, -1, 'CanvasFrame', size=(550, 350))
self.figure = Figure()
self.axes = host_subplot(111, axes_class=AA.Axes, figure=self.figure)
t = np.arange(0.0, 3.0, 0.01)
s = np.sin(2 * np.pi * t)
self.axes.plot(t, s)
self.patch = self.axes.fill_between(t, 0, s/2.0, facecolor='r', edgecolor='r',
alpha=0.2, linewidth=1.5, interpolate=True)
self.canvas = FigureCanvas(self, -1, self.figure)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.EXPAND)
self.SetSizer(self.sizer)
self.Fit()
button = wx.Button(self, -1, 'Remove Collection')
self.sizer.Add(button, 0)
button.Bind(wx.EVT_BUTTON, self.OnRemoveCollection)
def OnRemoveCollection(self, event):
if self.patch is not None:
self.patch.remove()
del self.patch
self.patch = None
self.canvas.draw()
# Alternatively you could use:
class App(wx.App):
def OnInit(self):
"""Create the main window and insert the custom frame."""
frame = CanvasFrame()
frame.Show(True)
return True
if __name__ == "__main__":
app = App()
app.MainLoop()
Shows a very simple plot embedded in wx. There is a button with the label “Remove Collection” at the bottom left of the window. Clicking on it - to try and remove the collection stored in the self.patch attribute from the axes - results in the following:
Traceback (most recent call last):
File "C:\Users\J0514162\MyProjects\mpl_remove_collections.py", line 42, in OnRemoveCollection
self.canvas.draw()
File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\matplotlib\backends\backend_wxagg.py", line 29, in draw
FigureCanvasAgg.draw(self)
File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\matplotlib\backends\backend_agg.py", line 436, in draw
self.figure.draw(self.renderer)
File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\matplotlib\artist.py", line 73, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\matplotlib\figure.py", line 2810, in draw
mimage._draw_list_compositing_images(
File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\mpl_toolkits\axes_grid1\parasite_axes.py", line 212, in draw
super().draw(renderer)
File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\matplotlib\axes\_base.py", line 3082, in draw
mimage._draw_list_compositing_images(
File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\matplotlib\artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\matplotlib\collections.py", line 990, in draw
self.set_sizes(self._sizes, self.figure.dpi)
AttributeError: 'NoneType' object has no attribute 'dpi'
Using a standard add_subplot() rather than host_subplot() works all right, but I really do need host_subplot()…
Could anyone please point to my mistake? This is on Windows 10 64 bit, Python 3.9.10, matplotlib 3.5.1.
EDIT: this of course used to work in old versions of matplotlib (1.X) on Python 2.
Thank you in advance.
Andrea.