-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DataFrame.plot() produces incorrect legend labels when plotting multiple series on the same axis #18222
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
Comments
@scottlawsonbc : Could you provide snapshots of what you're seeing currently? |
@gfyoung there are two screenshots under the expected output section |
Yes, I see that, but which, if any, represent what you are seeing currently? Or are both snapshots representative of what you would expect to see? |
@gfyoung I understand your question now. I see how the plots are confusing and I should have explained their meaning in a way that is more clear. At the bottom of my example code, I commented out several lines:
When these lines are commented out, the sample code produces a plot with incorrect legend labels. If you run the sample code exactly as I provided in the original issue, then it will produce the erroneous incorrect output. When these lines are uncommented, the sample code produces a plot with the correct legend labels. However, the issue is that pandas should be producing this output without requiring these lines to be run. In the expected output section of my issue, the first plot shows the output which is incorrect, and the second plot shows what the correct output should be (intended as a reference). Does this answer your question? I will update my issue to make this more clear. |
Ah, okay. That makes sense. If you could update the issue to reflect your answer, that would be great. |
@scottlawsonbc do you have time to debug where things are going wrong? pandas/pandas/plotting/_core.py Line 495 in 7f4c960
pandas/pandas/plotting/_core.py Line 384 in 7f4c960
|
if you still using multiple series with one axis (like barplot with x,y, and hue options) and the legend still incorect, you can modified the legend using get_texts() after df.plot(). example: |
If you need another example of this problem let me know |
Hi @jzlcdh - if you're aware of a new bug then please open a new issue, thanks |
Code Sample, a copy-pastable example if possible
Problem description
Note: Possibly related to #14958, #17939, #14563, however this issue discusses how the behaviour depends on the order in which
df.plot()
is calleddf.plot()
may generate incorrect legend labels (see example)df.plot()
plots multiple series on the same axisdf.plot()
call will produce the correct legend labelax.legend()
after the finaldf.plot()
will produce the correct legend entrydf.plot()
withmarkerstyle=''
always appears produces the correct legend label for that series regardless of the order in whichdf.plot()
is called, or the number of other series on the same axisExpected Output
This is the incorrect output produced by the sample code


Note that in each plot, only the last series to be plotted has a correct legend label
This is the expected output
Output of
pd.show_versions()
[paste the output of
pd.show_versions()
here below this line]INSTALLED VERSIONS
commit: None
python: 3.5.4.final.0
python-bits: 64
OS: Linux
OS-release: 4.10.0-37-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.21.0
pytest: None
pip: 9.0.1
setuptools: 36.5.0.post20170921
Cython: None
numpy: 1.13.3
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 6.1.0
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: