Skip to content
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

BUG: histogram plot ignores xlabel and ylabel arguments #41233

Open
2 of 3 tasks
Battleman opened this issue Apr 30, 2021 · 2 comments
Open
2 of 3 tasks

BUG: histogram plot ignores xlabel and ylabel arguments #41233

Battleman opened this issue Apr 30, 2021 · 2 comments
Labels

Comments

@Battleman
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import numpy as np
import pandas as pd
data = pd.Series(np.random.random(10))

##problem
data.plot.hist(xlabel="My xlabel", ylabel="My ylabel")

Output graph:
wrong

Problem description

The method accepts the keyword arguments but ignores them and overrides them with defaults (respectively and "Frequency". While those defaults are sensible, specifying those keyword arguments should either be met with the intended output (custom x and y labels) or an error (of the type NotImplementedError)

The documentation (for hist and plot) seem to indicate this is supported (although a bit unclear)

Expected Output

The outputted graph should be the same as when produced with the following code:

import numpy as np
import pandas as pd
data = pd.Series(np.random.random(10))
ax = data.plot.hist()
ax.set_xlabel("My xlabel")
ax.set_ylabel("My ylabel")

Which is the following:
fixed

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : 2cb96529396d93b46abab7bbc73a208e708c642e
python           : 3.9.4.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.11.0-7614-generic
Version          : #15~1618626693~20.10~ecb25cd-Ubuntu SMP Thu Apr 22 16:00:45 UTC 
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.2.4
numpy            : 1.19.2
pytz             : 2021.1
dateutil         : 2.8.1
pip              : 21.0.1
setuptools       : 52.0.0.post20210125
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : 4.6.2
html5lib         : None
pymysql          : 1.0.2
psycopg2         : None
jinja2           : None
IPython          : 7.22.0
pandas_datareader: None
bs4              : None
bottleneck       : None
fsspec           : None
fastparquet      : None
gcsfs            : None
matplotlib       : 3.3.4
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pyxlsb           : None
s3fs             : None
scipy            : 1.6.2
sqlalchemy       : 1.4.7
tables           : None
tabulate         : 0.8.9
xarray           : None
xlrd             : None
xlwt             : None
numba            : None
@Battleman Battleman added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 30, 2021
@regmibijay
Copy link

regmibijay commented Apr 30, 2021

I started looking into this and there seems to be possible interaction with orientation, because xlabel and ylabel seemed to be set depending on orientation. I changed HistPlot.__init__() to

def __init__(self, data, bins=10, bottom=0, **kwargs):
        self.bins = bins  # use mpl default
        self.bottom = bottom
        # Do not call LinePlot.__init__ which may fill nan
        MPLPlot.__init__(self, data, **kwargs)
       # getting x and y label
        if "xlabel" in kwargs.keys():
            self.xlabel = kwargs["xlabel"]
        if "ylabel" in kwargs.keys():
            self.ylabel = kwargs["ylabel"]

and then changed the label setter in HistPlot._plot_plot_logic to

def _post_plot_logic(self, ax: Axes, data):
        if self.orientation == "horizontal":
            if self.xlabel is not None:
                ax.set_xlabel(self.xlabel)
            else:
                ax.set_xlabel("Frequency")
        else:
            if self.ylabel is not None:
                ax.set_ylabel(self.ylabel)
            else:
                ax.set_ylabel("Frequency")

and xlabel and ylabel are set depending upon orientation of the plot.
Figure_1
Figure_1_2

@pablodz
Copy link

pablodz commented May 10, 2021

image

Also broken with table=True
Orientation is the problem, change to horizontal and the xlabel appears

I think that can be fixed with this https://stackoverflow.com/a/6776578/10491422 on backend

@jbrockmendel jbrockmendel added Visualization plotting and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants