Description
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 matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
times=pd.date_range(start='2021-01-01', end='2021-12-31', freq='1h')
adf = pd.DataFrame({
'time': times,
'A': np.random.rand(times.shape[0]),
'B': np.random.rand(times.shape[0]) * 100
})
# uncomment to produce second image
#for col in adf.columns:
# adf.loc[adf.sample(frac=0.7).index, col] = np.nan
adf.set_index('time', inplace=True)
fig, ax = plt.subplots(dpi=300)
ax2 = ax.twinx()
a = adf.plot(y='A', ax=ax, color='blue', alpha=0.3, legend=False)
b = adf.plot(y='B', ax=ax2, color='red', alpha=0.3, legend=False)
ax.legend([
a.get_lines()[0],
b.get_lines()[0]
], ['A', 'B'])
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d-%y'))
ax.xaxis.set_major_locator(mdates.MonthLocator(interval=1))
ax.tick_params(axis='x', rotation=30)
Issue Description
When there are too many hourly data points with a date-time axis, formatting the date-time tick labels to show only one for the beginning in the month results in far too many tick labels and tick labels showing the wrong year. This is a bug within pandas. I made an issue for matplotlib and they redirected me to here.
Expected Behavior
An image like below should be possible.
Installed Versions
INSTALLED VERSIONS
commit : 4bfe3d0
python : 3.10.4.final.0
python-bits : 64
OS : Darwin
OS-release : 21.4.0
Version : Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.4.2
numpy : 1.21.6
pytz : 2022.1
dateutil : 2.8.2
pip : 22.0.4
setuptools : 62.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.1
IPython : 8.2.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli :
fastparquet : None
fsspec : 2022.3.0
gcsfs : None
markupsafe : 2.1.1
matplotlib : 3.5.1
numba : 0.55.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : 2022.3.0
xlrd : None
xlwt : None
zstandard : None