Skip to content

BUG: xticks in bar plot breaks plot #55508

@gdementen

Description

@gdementen

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
pd.__version__
# '2.0.3'
s = pd.Series([1, 3, 2], index=[2010, 2015, 2020])
# works fine (see line.png)
s.plot(xticks=[2010, 2020])
# ticks and bars are all squashed to the right of the plot (see bar.png)
s.plot.bar(xticks=[2010, 2020])
# works fine too
s.plot.bar()

Issue Description

When using the xticks argument on a bar plot, it is kind of "squashed" to the right of the plot.
Ticks and bars are all on a single tick on the right:
bar

For line plots, this works fine:
line

The issue is also present in Pandas 2.1.1 (latest stable version as of today) and Pandas 1.5.3. It might be related to #11465.

Expected Behavior

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.bar([2010, 2015, 2020], [1, 3, 2])
ax.set_xticks([2010, 2020])
plt.show()

bar_good

Installed Versions

INSTALLED VERSIONS

commit : 0f43794
python : 3.10.13.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : French_Belgium.1252

pandas : 2.0.3
numpy : 1.22.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2.1
Cython : 0.29.33
pytest : 7.3.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.1.1
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.15.0
pandas_datareader: None
bs4 : 4.12.2
bottleneck : 1.3.5
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : 0.57.0
numexpr : 2.8.7
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : 3.8.0
tabulate : None
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : 2.2.0
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team memberVisualizationplotting

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions