You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: plt.bar legend displays incorrect style for empty data #21506
Legend items created from empty plt/ax.bar calls are displayed with the default style and the first color of the theme's cycle. This behavior contradicts the behavior of plt/ax.plot and plt/ax.scatter.
Code for reproduction
importmatplotlib.pyplotasplt# The bug happens with or without this import.importnumpyasnp
Activity
Fixed bug with legend color and empty bars. Closes matplotlib#21506.
Fixed bug with legend color and empty bars. Closes matplotlib#21506.
Fixed bug with legend color and empty bars. Closes matplotlib#21506.
Fixed bug with legend color and empty bars. Closes matplotlib#21506.
kma-code commentedon Apr 3, 2025
Bug persists in Matplotlib 3.3.4.
If you are trying to generate a legend using an empty plt.bar, a workaround is to plot a bar with some data and set width = 0:
Operating system
Mac OS
Matplotlib Version
3.2.4
Matplotlib Backend
module://ipykernel.pylab.backend_inline
Python version
3.11
Jupyter version
IPython : 8.26.0
ipykernel : 6.29.5
ipywidgets : not installed
jupyter_client : 8.6.2
jupyter_core : 5.7.2
jupyter_server : 2.15.0