Skip to content

[Bug]: Inconsistent treatment of list of labels in plot when the input is a dataframe #27762

@Gattocrucco

Description

@Gattocrucco

Bug summary

plt.plot interprets a dataframe as a sequence of series to be plotted, one per column. Labels can be specified by setting label to a list of labels matching the number of columns. If the dataframe has only one column, though, the label argument is interpreted as a single label, instead of a list with one element.

Code for reproduction

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np

x = np.arange(5)
y = pd.DataFrame({'a': x, 'b': x + 1})

plt.figure('double', clear=True)
plt.plot(x, y, label=['A', 'B'])
plt.legend() # -> ok

plt.figure('single', clear=True)
plt.plot(x, y.drop('b', axis=1), label=['A'])
plt.legend() # -> legend label is "['A']"

plt.show()

Actual outcome

immagine

Expected outcome

immagine

Additional information

No response

Operating system

macOS 14.2.1

Matplotlib Version

3.8.2

Matplotlib Backend

MacOSX

Python version

3.12.1 | packaged by conda-forge | (main, Dec 23 2023, 08:01:35) [Clang 16.0.6 ]

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions