question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataFrame.plot(): changing position of legend with secondary_y removes legend entries

See original GitHub issue

Code Sample

df = pd.DataFrame([[1, 1], [2, 4], [3, 7]], index=[1, 2, 3],
                  columns=['col a', 'col b'])
df.plot(kind='bar', secondary_y=['col b']).legend(bbox_to_anchor=(1.5, 1))

Problem description

The code above produces a barplot with secondary y axis for ‘col b’. The last command (.legend(…)) tells it to move the legend around, which works but produces a new legend that only includes ‘col a’.

Expected Output

I believe it should include both columns in the legend, like it does when the .legend part is omitted. I have also tried to move the legend around with other methods, but with no success.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Windows OS-release: 7 machine: AMD64 processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.19.2 nose: 1.3.7 pip: 8.1.2 setuptools: 35.0.2 Cython: 0.24.1 numpy: 1.12.1 scipy: 0.18.1 statsmodels: 0.6.1 xarray: None IPython: 6.0.0 sphinx: 1.4.6 patsy: 0.4.1 dateutil: 2.6.0 pytz: 2017.2 blosc: None bottleneck: 1.1.0 tables: 3.2.2 numexpr: 2.6.1 matplotlib: 1.5.3 openpyxl: 2.3.2 xlrd: 1.0.0 xlwt: 1.1.2 xlsxwriter: 0.9.3 lxml: 3.6.4 bs4: 4.5.1 html5lib: None httplib2: None apiclient: None sqlalchemy: 1.0.13 pymysql: 0.7.9.None psycopg2: None jinja2: 2.8 boto: 2.42.0 pandas_datareader: None

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
lmarchesoticommented, May 19, 2017

Hm… it seems to keep the labels this way, although the legend locations differ a bit.

Here’s with

ax.get_legend().set_bbox_to_anchor((0.7, 1))

pandas_sec_y_bbox_output

Increasing it to more than 0.7 puts it out of the frame, although that is what I originally intended since I can still view it in IPython Notebook.

Edit: Here’s the original, with

df = pd.DataFrame([[1, 1, 1], [2, 4, 3], [3, 7, 9]], index=[1, 2, 3],
                  columns=['col a', 'col b', 'col c'])
df.plot(kind='bar', secondary_y=['col b']).legend(bbox_to_anchor=(1.1, 1))

pandas_plot_bbox_orig

1reaction
lmarchesoticommented, May 18, 2017
df = pd.DataFrame([[1, 1, 1], [2, 4, 3], [3, 7, 9]], index=[1, 2, 3],
                  columns=['col a', 'col b', 'col c'])
df.plot(kind='bar', secondary_y=['col b']).legend(bbox_to_anchor=(1.5, 1))

it seems only the column in the secondary axis is left out

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set legend position when plotting a pandas dataframe with a ...
Does anyone know how I can set the legend position and keep all entries? Thanks in advance! python · pandas · matplotlib ·...
Read more >
pandas.DataFrame.plot — pandas 1.5.2 documentation
Only used if data is a DataFrame. ylabel, position or list of label, positions, default None. Allows plotting of one column versus another....
Read more >
matplotlib.pyplot.legend — Matplotlib 3.6.2 documentation
line, = ax.plot([1, 2, 3]) line.set_label('Label via method') ax.legend() ... For full control of which artists have a legend entry, it is possible...
Read more >
Pandas: How to Create and Customize Plot Legends - Statology
This tutorial explains how to create and customize a plot legend in pandas, ... DataFrame({'A':7, 'B':12, 'C':15, 'D':17}, index=['Values']).
Read more >
Controlling legend appearance in ggplot2 with override.aes
You can see using alpha and size changes the way the points are shown in both the plot and the legend. ggplot(data =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found