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: df.plot(kind='barh') does not show xlabel #46129

Closed
3 tasks done
McToel opened this issue Feb 23, 2022 · 5 comments · Fixed by #46445
Closed
3 tasks done

BUG: df.plot(kind='barh') does not show xlabel #46129

McToel opened this issue Feb 23, 2022 · 5 comments · Fixed by #46445
Labels
Milestone

Comments

@McToel
Copy link

McToel commented Feb 23, 2022

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import matplotlib.pyplot as plt

co2 = pd.Series({
    'train': 4.8,
    'car': 128.5,
    'aiplane': 98.5
})

co2.plot(
    kind='barh',
    title='CO₂ Tübingen to Berlin',
    xlabel='kg CO₂-eq'
)

plt.show()

Issue Description

The code produces the following plot, with the xlabel showing up on the y-axis.
image

When only supplying the ylabel, everything works as expected.
When supplying both, the xlabel overrides the ylabel

Expected Behavior

The xlabel should show up on the x-axis.

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.9.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.13.0-30-generic
Version : #33~20.04.1-Ubuntu SMP Mon Feb 7 14:25:10 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.1
numpy : 1.22.0
pytz : 2020.1
dateutil : 2.8.1
pip : 22.0.3
setuptools : 58.1.0
Cython : 0.29.21
pytest : 6.2.5
hypothesis : None
sphinx : 3.4.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : None
pymysql : None
psycopg2 : 2.8.6
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : 0.8.4
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 6.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : 1.3.24
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@McToel McToel added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 23, 2022
@iansheng
Copy link
Contributor

As described in official doc:

xlabel: label, optional
Name to use for the xlabel on x-axis. Default uses index name as xlabel, or the x-column name for planar plots.
ylabel: label, optional
Name to use for the ylabel on y-axis. Default will show no ylabel, or the y-column name for planar plots.

It seems that the default rule of Dataframe.plot takes precedence, causing xlabel not to be displayed.
IMO, these parameters should be consistent with using plt directly.

There is a way to temporarily solve this problem by using the plt library directly.

co2.plot.barh()
plt.title('CO₂ Tübingen to Berlin')
plt.xlabel('kg CO₂-eq')
plt.show()

The xlabel will show up on the x-axis.

@McToel
Copy link
Author

McToel commented Mar 1, 2022

The pie plot has a similar issue that might be related. When not Setting ylabel the plot will show None as ylabel

co2.plot(kind='pie')

image

@iansheng
Copy link
Contributor

iansheng commented Mar 10, 2022

Hi! I tried this example in the main branch and found that the xlabel issue has been fixed. Pls check PR 45145, which sloves ur xlabel problem.
Thus this issue should be closed. As for ur second problem,we'd better check if there have beem similar issues.

@iansheng
Copy link
Contributor

Hi, @McToel !

I find that Pandas plot uses common processing logic: It converts data into DataFrame for subsequent operations, meaning that a Series with name=None will change into DF with columns=["None"]. (By default, the value will changed to 0, because the DF does not support NoneType value in columns.)

That is why None label shows on the pie chart.

if isinstance(data, ABCSeries):
label = self.label
if label is None and data.name is None:
label = "None"
if label is None:
# We'll end up with columns of [0] instead of [None]
data = data.to_frame()
else:
data = data.to_frame(name=label)

I tried to solve the None label problem by dealing with Series and DataFrame separately, and it worked. And 164 testcases became failed :-C. Obviously, modifying the common logic has a big impact.

I'll keep working on it, but before that, I wonder if dealing with Series and DataFrame separately is a step backwards.

@McToel
Copy link
Author

McToel commented Mar 11, 2022

I would suggest to just replace "None" with "" which should work according to my quick and dirty test I did (I have no pandas dev setup on my laptop right now and also not much time).

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