Skip to content

Missing marker when using style=... #14563

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
tamasgal opened this issue Nov 2, 2016 · 3 comments · Fixed by #30687
Closed

Missing marker when using style=... #14563

tamasgal opened this issue Nov 2, 2016 · 3 comments · Fixed by #30687
Labels
Milestone

Comments

@tamasgal
Copy link

tamasgal commented Nov 2, 2016

There is an issue with the markers in the legend when working with the built-in matplotlib plot-wrapper in pandas. The problem is also in earlier versions and the only workaround I found is using matplotlib directly.

A small, complete example of the issue

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'A': [1, 2, 3, 4, 5, 6],
                   'B': [2, 4, 1, 3, 2, 4],
                   'C': [3, 3, 2, 6, 4, 2],
                   'X': [1, 2, 3, 4, 5, 6]})

fig, ax = plt.subplots()

for kind in 'ABC':
    df.plot('X', kind, label=kind, ax=ax, style='.')

plt.show()

markers

Expected Output

The markers in the legend are missing, except for the last label. It however works if I don't use style=..., but the default line-plot, like in this example:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'A': [1, 2, 3, 4, 5, 6],
                   'B': [2, 4, 1, 3, 2, 4],
                   'C': [3, 3, 2, 6, 4, 2],
                   'X': [1, 2, 3, 4, 5, 6]})

fig, ax = plt.subplots()

for kind in 'ABC':
    df.plot('X', kind, label=kind, ax=ax)

plt.show()

markers_with_lines

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Darwin OS-release: 16.1.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: en_US.UTF-8 LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.19.0
nose: None
pip: 8.1.2
setuptools: 25.2.0
Cython: 0.24.1
numpy: 1.11.2
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: None
patsy: None
dateutil: 2.5.3
pytz: 2016.7
blosc: None
bottleneck: None
tables: 3.3.0
numexpr: 2.6.1
matplotlib: 1.5.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None

@tamasgal
Copy link
Author

I tracked down the problem to a single line in the sources.

This call is the final one, which sets the legends with the missing points except for the last ax when using style='.'
https://github.com/pandas-dev/pandas/blob/master/pandas/tools/plotting.py#L1293

If you change it from ax.legend(handles, labels, loc='best', title=title) to a plain ax.legend(), every marker is shown correctly in the legend. However, all optional settings are then of course skipped.

This line is the one which adds the legend handle in this case: https://github.com/pandas-dev/pandas/blob/master/pandas/tools/plotting.py#L1744

self._add_legend_handle(newlines[0], label, index=i)

However, I am still not able to figure out what is wrong there, the variable values look fine there too...

@charlesdong1991
Copy link
Member

Screen Shot 2020-01-04 at 6 47 41 PM

work on master, previously uploaded wrong pic, sorry

@tamasgal
Copy link
Author

tamasgal commented Jan 4, 2020

Thanks!

@tamasgal tamasgal closed this as completed Jan 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants