Skip to content

[Bug]: Unable to set facecolor in legend_handles #32178

Description

@ArchangeGabriel

Bug summary

I had this code some years ago to changes the dot color in the legend of a scatter plot:

legend = plt.gca().get_legend()
legend.legendHandles[0].set_color(plt.cm.viridis(.5))

I have to redo these old figs, and after changing legendHandles to the new legend_handles, I find myself unable to change the facecolor. Neither set_facecolor, set_color, set(facecolor=<>) or set(color=<>) work.

Code for reproduction

import matplotlib.pyplot as plt

plt.scatter([0,1],[0,1],c=[0,1],label='test')
plt.colorbar()
plt.legend()
legend = plt.gca().get_legend()
legend.legend_handles[0].set(color=plt.cm.viridis(.5))
plt.show()

Actual outcome

Only the edge color is changed (or nothing if using facecolor).
Image

Expected outcome

Face color should be changed.

Additional information

It worked in 2022 (not sure of the matplotlib version back then).

Operating system

Arch

Matplotlib Version

3.11.0

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

Linux package manager

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions