Skip to content

BUG: Wrong legend when combining multiple plots on the same axes #39522

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

Open
david-zwicker opened this issue Feb 1, 2021 · 6 comments
Open
Labels

Comments

@david-zwicker
Copy link

The code

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame([{'x': 1, 'a': 1, 'b': 1}, {'x': 2, 'a': 2, 'b': 3}])

ax = plt.gca()
df.plot('x', 'a', yerr=0.1, ax=ax, label='with errors')
df.plot('x', 'b', ax=ax, label='without errors')

results in the following image on my platform:
download
Note that the labels in the legend are swapped!
I obviously expected the legend to show the correct labels.

INSTALLED VERSIONS
------------------
commit           : 9d598a5e1eee26df95b3910e3f2934890d062caa
python           : 3.8.7.final.0
python-bits      : 64
OS               : Darwin
OS-release       : 19.6.0
Version          : Darwin Kernel Version 19.6.0: Tue Nov 10 00:10:30 PST 2020; root:xnu-6153.141.10~1/RELEASE_X86_64
machine          : x86_64
processor        : i386
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas : 1.2.1
numpy : 1.19.5
pytz : 2020.5
dateutil : 2.8.1
pip : 21.0
setuptools : 51.3.3
Cython : 0.29.21
pytest : 6.2.2
hypothesis : None
sphinx : 3.4.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.1
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : None
tables : None
tabulate : 0.8.7
xarray : None
xlrd : None
xlwt : None
numba : 0.52.0

@david-zwicker david-zwicker added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 1, 2021
@robertzee
Copy link

Same issue here.

@attack68
Copy link
Contributor

attack68 commented Feb 3, 2021

if you try:

df.plot('x', 'a', yerr=0.1, ax=ax, label='one red', color='r')
df.plot('x', 'b', ax=ax, label='two green', color='g')
df.plot('b', 'a', ax=ax, label='three blue', color='b')
df.plot('b', 'a', ax=ax, label='four yellow', color='y')

The legend shows the text in the right order but the colours have been permuted backwards by one:

<green> one red
<blue> two green
<yellow> three blue
<red> four yellow

If you remove yerr=0.1 the problem disappears.
If you add yerr=0.1 to all methods the problem disappears.

If you have 2 methods with yerr=0.1 then the color permutation shifts by 2 instead of one, for 3 the permutation is 3.

@nmay231
Copy link
Contributor

nmay231 commented Feb 4, 2021

I assume the culprit is somewhere around this section of code, but I don't understand how the kwargs are shifted across calls to df.plot().

Unless there is something funky with how the matplotlib wrapper applies keyword arguments on shared plots (in which case, maybe this function is messed up? 🤷).

Also note that removing yerr from @attack68's first df.plot() and adding it to the last one switches only the last two plot colors.

ax = plt.gca()
# Only blue and yellow are switched now; red and green are fine
df.plot('x', 'a', ax=ax, label='one red', color='r')
df.plot('x', 'b', ax=ax, label='two green', color='g')
df.plot('b', 'a', ax=ax, label='three blue', color='b')
df.plot('b', 'a', yerr=0.1, ax=ax, label='four yellow', color='y')

@MarcoGorelli MarcoGorelli added Visualization plotting and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 22, 2021
@MarcoGorelli
Copy link
Member

Thanks @david-zwicker for the report - indeed, I can confirm this reproduces on master, will look into it

@MarcoGorelli
Copy link
Member

MarcoGorelli commented Feb 24, 2021

Just tried this out with matplotlib directly, and it displays fine, so the bug is probably to do with pandas

image


this is related to #40044

@MarcoGorelli
Copy link
Member

Update: since #40777 , the colours in the legend are now correct. However, due to an open issue in matplotlib, marker info is not yet preserved in the legend, so let's keep this open until then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants