Skip to content

BUG: xaxis formatter receives index instead of values #50508

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

Open
3 tasks done
gonzalobg opened this issue Dec 30, 2022 · 2 comments
Open
3 tasks done

BUG: xaxis formatter receives index instead of values #50508

gonzalobg opened this issue Dec 30, 2022 · 2 comments
Labels

Comments

@gonzalobg
Copy link

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
from matplotlib.ticker import FuncFormatter

df = pd.DataFrame({'xs': [13.2, 42.2, 55.2], 'ys': [5.1, 7.1, 8.1]})

def formatter(value, pos):
    print(f'{value},{pos}')
    return value

ax = df.plot(kind='bar', x="xs", y="ys")
#ax.yaxis.set_major_formatter(FuncFormatter(formatter)) # This works, formatter gets yaxis value
ax.xaxis.set_major_formatter(FuncFormatter(formatter)) # This fails, formatter gets index as value

Issue Description

When using ax.yaxis.set_major_formatter with a FuncFormatter, the function taking a value and pos correctly receive the y-axis values as value.

When using an ax.xaxis.set_major_formatter, instead of getting the x-axis values, the function gets the index as value.

Expected Behavior

I'd expect the same behavior for both axis: the values get passed to value.

This behavior is currently correct for the y-axis, but incorrect for the x-axis.

Installed Versions

INSTALLED VERSIONS

commit : 66e3805
python : 3.8.16.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.133+
Version : #1 SMP Fri Aug 26 08:44:51 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.3.5
numpy : 1.21.6
pytz : 2022.6
dateutil : 2.8.2
pip : 21.1.3
setuptools : 57.4.0
Cython : 0.29.32
pytest : 3.6.4
hypothesis : None
sphinx : 1.8.6
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.9.2
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.9.5 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : 7.9.0
pandas_datareader: 0.9.0
bs4 : 4.6.3
bottleneck : None
fsspec : 2022.11.0
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.8.4
odfpy : None
openpyxl : 3.0.10
pandas_gbq : 0.17.9
pyarrow : 9.0.0
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : 1.4.45
tables : 3.7.0
tabulate : 0.8.10
xarray : 2022.12.0
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.56.4

@gonzalobg gonzalobg added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 30, 2022
@dicristina
Copy link
Contributor

Try the example with an index other than [0, 1, 2] and you will see that your function will still get the values 0, 1, 2 because it does not get the DataFrame index at all. I don't think that this is an issue with pandas.

@mfloryan
Copy link

I don't know enough of the internals here but it seems that the same example works correctly when this line:

ax = df.plot(kind='bar', x="xs", y="ys")

is replaced with a direct call to matplotlib.

fig, ax = plt.subplots()
ax.bar(df['xs'], df['ys'])

@mroeschke mroeschke removed the Needs Triage Issue that has not been reviewed by a pandas team member label Jul 17, 2024
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

5 participants