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.

Series.plot should label y axis with Series name

See original GitHub issue

Code Sample, a copy-pastable example if possible

import pandas as pd
import matplotlib.pyplot as plt
ind = pd.Series(['first', 'second', 'third'], name='count')
s = pd.Series([1, 2, 3], index=ind, name='i should be a y label')
s.plot()
plt.show()

Problem description

So when you call Series.plot, xlabel is set to the name of the index, but ylabel is not set to the name of the series itself. To me it seems asymmetric and believe it should set ylabel automatically. Also, if I plot it with s.plot(kind='hist'), I get ylabel “Frequency” but no xlabel, which I would expect to be the name of the series as well.

Expected Output

ylabel set to name of the series for usual plot, and xlabel set to name of the series for hist plot

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line] INSTALLED VERSIONS

commit: None python: 3.6.2.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: AMD64 Family 16 Model 4 Stepping 3, AuthenticAMD byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.20.3 pytest: 3.2.1 pip: 9.0.1 setuptools: 36.5.0.post20170921 Cython: 0.26.1 numpy: 1.13.1 scipy: 0.19.1 xarray: None IPython: 6.1.0 sphinx: 1.6.3 patsy: 0.4.1 dateutil: 2.6.1 pytz: 2017.2 blosc: None bottleneck: 1.2.1 tables: 3.4.2 numexpr: 2.6.2 feather: None matplotlib: 2.0.2 openpyxl: 2.4.8 xlrd: 1.1.0 xlwt: 1.3.0 xlsxwriter: 0.9.8 lxml: 3.8.0 bs4: 4.6.0 html5lib: 0.999999999 sqlalchemy: 1.1.13 pymysql: None psycopg2: None jinja2: 2.9.6 s3fs: None pandas_gbq: None pandas_datareader: None

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Eoksnicommented, Nov 5, 2017

Actually, I made my workaround even more convenient. I just have this snippet at the start of my python notebook, and then I can use Series.plot in ordinal way, but it now puts ylabel as I wanted.

from functools import wraps

def monkey_patch_series_plot():
    """Makes Series.plot to show series name as ylabel/xlabel according to plot kind."""
    f = pd.Series.plot.__call__
    @wraps(f)
    def _decorator(*kargs, **kwargs):
        res = f(*kargs, **kwargs)
        s = kargs[0].__dict__['_data']
        if s.name:
            try:
                kind = kwargs['kind']
            except KeyError:
                kind = 'line'
            if kind == 'line' or kind == 'scatter':
                plt.ylabel(s.name)
            elif kind == 'hist':
                plt.xlabel(s.name)
        return res
    pd.Series.plot.__call__ = _decorator
monkey_patch_series_plot()

So, I’m entirely satisfied with this so I close the issue.

0reactions
Eoksnicommented, Apr 16, 2019

@yudai-nkt I don’t think you are doing something wrong, your exact code piece worked flawlessly for me back in the days. I haven’t been using pandas for a while and not exactly sure how it can be fixed for newer version.

As a suggestion, you can put a breakpoint before kargs[0].__dict__ line and see what is inside and change the code accordingly. And yeah, all of this is fiddling with internal data structures and such structure probably can be changed even in minor releases as it is not part of public API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add a title and axis labels to your charts using matplotlib
In this post, you will see how to add a title and axis labels to your python charts using matplotlib. If you're new...
Read more >
Change axis labels in a chart - Microsoft Support
Right-click the category labels you want to change, and click Select Data. Right-click the category axis and Select Data · In the Horizontal...
Read more >
pandas.Series.plot — pandas 1.5.2 documentation
In case subplots=True , share x axis and set some x axis labels to invisible; ... Default will show no ylabel, or the...
Read more >
Chart Elements
Axis titles are words or phrases that describe the entire axis. Markers identify data points. You can put markers on all data points...
Read more >
Pandas Time Series Plot: Secondary y-Axis Label , Set Lower ...
Assign the return value of df.plot to a variable; it will be of type matplotlib.axes._subplots.AxesSubplot . Then twinx it to get access to ......
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