Skip to content

Using legend(labelcolor="markerfacecolor") with a scatter plot throws an error #20577

Closed
@agude

Description

@agude

Bug report

legend(labelcolor="markerfacecolor") raises error when used with scatter()

Adding a legend with colored text using any of linecolor, markerfacecolor, or markeredgecolor as the values for the argument labelcolor with a scatter plot results in a TypeError: unhashable type: 'numpy.ndarray'.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

ax.scatter(np.random.rand(500)-0.65, np.random.rand(500), label="First dataset")
ax.scatter(np.random.rand(500)-0.35, np.random.rand(500), label="Second dataset")

ax.legend(labelcolor="linecolor")

fig.savefig("/tmp/test.png")

Actual outcome

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    fig.savefig("/tmp/test.png")
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/figure.py", line 3005, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_qt5agg.py", line 78, in print_figure
    super().print_figure(*args, **kwargs)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 2255, in print_figure
    result = print_method(
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 1669, in wrapper
    return func(*args, **kwargs)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 508, in print_png
    FigureCanvasAgg.draw(self)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 406, in draw
    self.figure.draw(self.renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 74, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/figure.py", line 2780, in draw
    mimage._draw_list_compositing_images(
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/_api/deprecation.py", line 431, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 2921, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/legend.py", line 606, in draw
    bbox = self._legend_box.get_window_extent(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 352, in get_window_extent
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 438, in get_extent_offsets
    whd_list = [c.get_extent(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 438, in <listcomp>
    whd_list = [c.get_extent(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 347, in get_extent
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 474, in get_extent_offsets
    whd_list = [c.get_extent(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 474, in <listcomp>
    whd_list = [c.get_extent(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 347, in get_extent
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 438, in get_extent_offsets
    whd_list = [c.get_extent(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 438, in <listcomp>
    whd_list = [c.get_extent(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 347, in get_extent
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 474, in get_extent_offsets
    whd_list = [c.get_extent(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 474, in <listcomp>
    whd_list = [c.get_extent(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/offsetbox.py", line 823, in get_extent
    bbox, info, yd = self._text._get_layout(renderer)
  File "/home/agude/bin/anaconda3/lib/python3.8/site-packages/matplotlib/text.py", line 294, in _get_layout
    if key in self._cached:
TypeError: unhashable type: 'numpy.ndarray'

Expected outcome

The legend text should match the color of the points.

Matplotlib version

  • Operating system: Ubuntu Linux (Linux hostname 5.4.0-77-generic #86~18.04.1-Ubuntu SMP Fri Jun 18 01:23:22 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux)
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4.2
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: Python 3.8.10
  • Other libraries: Numpy 1.20.2

Matplotlib is currently installed with pip install matplotlib==3.4.2 in a Conda install to force the most recent version... Yeah I know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesGood first issueOpen a pull request against these issues if there are no active ones!topic: legend

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions