Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 of 3 tasks
cinerea0 opened this issue Sep 3, 2020 · 1 comment
Closed
2 of 3 tasks
Labels
Bug Duplicate Report Duplicate issue or pull request Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@cinerea0
Copy link

cinerea0 commented Sep 3, 2020

  • 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()

/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

@cinerea0 cinerea0 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 3, 2020
@TomAugspurger
Copy link
Contributor

IIUC, this is the same as #8776. DataFrame.plot and Series.plot are inconsistent with whether they plot on an existing figure or create a new one.

@TomAugspurger TomAugspurger added the Duplicate Report Duplicate issue or pull request label Sep 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants