pandas: some markers missing from legend

Code Sample, a copy-pastable example if possible

# Your code here
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.randn(8, 3), 
                  columns=['A', 'B', 'C'])
ax = df.plot (y=['A'], marker='x', linestyle='solid')
df.plot (y=['B'], marker='o', linestyle='dotted', ax=ax)
df.plot (y=['C'], marker='<', linestyle='dotted', ax=ax)
plt.grid()
plt.show()


Problem description

The legend is missing some markers. If I add plt.legend() then they show up.

See attached figure1.pdf

[this should explain why the current behaviour is a problem and why the expected output is a better solution.]

Expected Output

Output of pd.show_versions()

# Paste the output here pd.show_versions() here INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Linux OS-release: 4.8.14-300.fc25.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.19.1 nose: 1.3.7 pip: 9.0.1 setuptools: 31.0.0 Cython: 0.25.2 numpy: 1.12.0rc1 scipy: 0.18.1 statsmodels: None xarray: None IPython: 5.1.0 sphinx: 1.5 patsy: None dateutil: 2.6.0 pytz: 2016.10 blosc: None bottleneck: None tables: 3.3.0 numexpr: 2.6.1 matplotlib: 2.0.0rc2 openpyxl: None xlrd: None xlwt: None xlsxwriter: None lxml: None bs4: 4.5.1 html5lib: 0.999 httplib2: None apiclient: None sqlalchemy: None pymysql: None psycopg2: None jinja2: 2.8 boto: None pandas_datareader: None

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 18 (13 by maintainers)

Most upvoted comments

No, I think this is probably just a pandas issue. In matplotlib you need to call legend explicitly in any case. It is pandas that gives the convenience of trying to do this for you.

Eg in this pure matplotlib example, only after calling the first ax.legend(), the legend is drawn. When adding another line, you need to call it again to update the legend:

fig, ax = plt.subplots()
ax.plot(df.index, df.A, marker='x', linestyle='solid')
ax.legend()
ax.plot(df.index, df.B, marker='o', linestyle='dotted')
ax.legend()

i think this has been fixed, and i cannot reproduce it on master, seems to be fixed by #27808

Screen Shot 2020-01-04 at 6 03 19 PM

so i think this can be closed @TomAugspurger @jreback