-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Closed
Labels
Community supportUsers in need of help.Users in need of help.
Description
Bug summary
I want lines to be shown as patches in my legends, as a global default. Based on the legend guide, I wrote the following code, but patches.Patch.get_path raises NotImplementedError: Derived must override
.
Code for reproduction
from matplotlib.legend import Legend
from matplotlib.lines import Line2D
from matplotlib.patches import Patch
class ToPatch:
def legend_artist(legend, orig_handle, fontsize, handlebox):
patch = Patch(
color=orig_handle.get_color(),
label=orig_handle.get_label(),
)
handlebox.add_artist(patch)
return patch
Legend.update_default_handler_map({Line2D: ToPatch})
import numpy as np
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
x = np.linspace(-2, 2)
ax.plot(x, x, label='linear')
ax.plot(x, x ** 2, label='quadratic')
ax.legend()
# ax.legend(handles=ax.get_legend().legendHandles)
plt.show()
Actual outcome
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/matplotlib/backend_bases.py", line 1193, in _on_timer
ret = func(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/matplotlib/backends/backend_macosx.py", line 68, in callback_func
callback()
File "/usr/local/lib/python3.10/site-packages/matplotlib/backends/backend_macosx.py", line 88, in _draw_idle
self.draw()
File "/usr/local/lib/python3.10/site-packages/matplotlib/backends/backend_macosx.py", line 50, in draw
super().draw()
File "/usr/local/lib/python3.10/site-packages/matplotlib/backends/backend_agg.py", line 405, in draw
self.figure.draw(self.renderer)
File "/usr/local/lib/python3.10/site-packages/matplotlib/artist.py", line 74, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer)
File "/usr/local/lib/python3.10/site-packages/matplotlib/figure.py", line 3082, in draw
mimage._draw_list_compositing_images(
File "/usr/local/lib/python3.10/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python3.10/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer)
File "/usr/local/lib/python3.10/site-packages/matplotlib/axes/_base.py", line 3100, in draw
mimage._draw_list_compositing_images(
File "/usr/local/lib/python3.10/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python3.10/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer)
File "/usr/local/lib/python3.10/site-packages/matplotlib/legend.py", line 649, in draw
self._legend_box.draw(renderer)
File "/usr/local/lib/python3.10/site-packages/matplotlib/offsetbox.py", line 366, in draw
c.draw(renderer)
File "/usr/local/lib/python3.10/site-packages/matplotlib/offsetbox.py", line 366, in draw
c.draw(renderer)
File "/usr/local/lib/python3.10/site-packages/matplotlib/offsetbox.py", line 366, in draw
c.draw(renderer)
[Previous line repeated 1 more time]
File "/usr/local/lib/python3.10/site-packages/matplotlib/offsetbox.py", line 683, in draw
c.draw(renderer)
File "/usr/local/lib/python3.10/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer)
File "/usr/local/lib/python3.10/site-packages/matplotlib/patches.py", line 585, in draw
path = self.get_path()
File "/usr/local/lib/python3.10/site-packages/matplotlib/patches.py", line 599, in get_path
raise NotImplementedError('Derived must override')
NotImplementedError: Derived must override
Expected outcome
If I uncomment the commented line (which calls ax.legend
a second time), I get the expected output:
Additional information
No response
Operating system
macOS 11.7.3
Matplotlib Version
3.6.3
Matplotlib Backend
MacOSX
Python version
3.10.9
Jupyter version
No response
Installation
pip
Metadata
Metadata
Assignees
Labels
Community supportUsers in need of help.Users in need of help.