Skip to content

BUG: Plotting charts in different orders creates different plot outputs #36083

@cinerea0

Description

@cinerea0
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample

import numpy as np
import pandas as pd

df = pd.DataFrame(
    {
        "date": [
            "2000-01-01",
            "2000-01-01",
            "2000-01-01",
            "2000-01-02",
            "2000-01-02",
            "2000-01-02",
            "2000-01-03",
            "2000-01-03",
            "2000-01-03",
        ],
        "group": ["A", "B", "C", "A", "B", "C", "A", "B", "C"],
    }
)
df["count"] = np.random.randint(0, 5, df.shape[0])
df["date"] = pd.to_datetime(df["date"])

separate_plot = df.groupby(["date", "group"])["count"].sum().unstack().plot()
separate_plot.get_figure().savefig("separate_plot.png")
total_plot = df.groupby("date")["count"].sum().plot()
total_plot.get_figure().savefig("total_plot.png")

Problem description

Plotting the "separate" plot before the "total" plot results in the following graphs:

separate_plot.png:
separate_plot

total_plot.png:
total_plot

As you can see, the lines from the "separate" plot also appear on the "total" plot, despite not grouping by "group" in the "total" plot. In my testing, this only occurs when the "separate" plot is made before the "total" plot.

Expected Output

The graphs should look something like the following regardless of which is plotted first:

separate_plot.png:
separate_plot

total_plot.png:
total_plot

Output of pd.show_versions()

Details

/usr/lib/python3.8/site-packages/_distutils_hack/init.py:30: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")

INSTALLED VERSIONS

commit : f2ca0a2
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.8.5_1
Version : #1 SMP Thu Aug 27 08:23:40 UTC 2020
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.1
numpy : 1.19.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 50.0.0
Cython : None
pytest : 5.4.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.18.1
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.1
numexpr : None
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDuplicate ReportDuplicate issue or pull requestNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions