Skip to content

[Bug]: plt.bar legend displays incorrect style for empty data #21506

Open

Description

@phoenix-meadowlark

Bug summary

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

import matplotlib.pyplot as plt
# The bug happens with or without this import.
import numpy as np
# Actual outcome code
plt.bar([], [], alpha=0.3, label="[], [], alpha=0.3")
plt.bar([], [], alpha=0.3, label="[], [], alpha=0.3")
plt.bar([np.nan], [np.nan], alpha=0.3, label="[np.nan], [np.nan], alpha=0.3")
plt.bar([], [], alpha=0.3, label="[], [], alpha=0.3")
plt.bar([1], [1], alpha=0.3, label="[1], [1], alpha=0.3")
plt.legend()
# Expected outcome code
plt.plot([], [], alpha=0.5, label="[], [], alpha=0.5")
plt.plot([], [], alpha=0.5, label="[], [], alpha=0.5")
plt.plot([np.nan], [np.nan], alpha=0.5, label="[np.nan], [np.nan], alpha=0.5")
plt.plot([], [], alpha=0.5, label="[], [], alpha=0.5")
plt.plot([1], [1], alpha=0.5, label="[1], [1], alpha=0.5")
plt.legend()

Actual outcome

Color cycling and alpha are not respected for the plt.bar([], [], ...) calls.

image

Expected outcome

Color cycling and alpha are respected for the plt.plot([], [], ...) calls.

image

Operating system

Linux

Matplotlib Version

3.2.2

Matplotlib Backend

module://ipykernel.pylab.backend_inline

Python version

3.7.12 (default, Sep 10 2021, 00:21:48) [GCC 7.5.0]

Jupyter version

Colab

Other libraries

No response

Installation

No response

Conda channel

No response

Activity

  1. added 3 commits that reference this issue on Jan 10, 2022
    0098369
    4f260a4
    d870cca
  2. added a commit that references this issue on Mar 28, 2022
    5d7b820
  3. kma-code commented on Apr 3, 2025

    @kma-code

    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:

    # matplotlib plt.bar doesn't apply style to empty data, so we set width = 0
    # https://github.com/matplotlib/matplotlib/issues/21506
    # instead of this:
    # ax.bar([],[], label="label", color='C2')
    # use this:
    ax.bar([1],[1], label="label", color='C2', 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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