-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DateFormatter('%Y') for x-axis labels gives strange output for a specific date value #26253
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
Comments
I might be wrong, but this looks like a bug in matplotlib's DateFormatter and the way the ticks are created and formatted (https://matplotlib.org/_modules/matplotlib/dates.html#DateFormatter -> https://matplotlib.org/_modules/matplotlib/dates.html#num2date). I was able to reproduce the bug with 3, 4 and 5 data points having exactly 7 days of separation. |
I found the wrongly functioning code uses PeriodConverter. The get_datevalue function call is returning "date.asfreq(freq).ordinal", which is an integer and I'm not familiar with its purpose, but seems to be causing the format errors at a later point. The cases that work use DatetimeConverter (instead of PeriodConverter -- which applies for the evenly-separated timestamps). |
The difference in behavior likely comes down to the inferred frequency for the two datasets. The first is W-SUN
This leads to pandas using different scales for the values of the x axis.
Pandas knows how to format those (with our custom formatter) matplotlib doesn't. Improvements here would be welcome, but I'd like to see the scope of this issue be more narrowly defined. |
Hi there, I was wondering if there was ever any resolution to this. It's easy enough to directly call the matplotlib API, but it'd be more convenient if we didn't have to. :) Thanks! |
The same problem. Topic was opened at 1 May 2019 (not 1 May 0007, lol) |
With just a few hours of trial & error :-) I finally found an easy workaround thanks to https://stackoverflow.com/a/57129081/890242.
Then the plot shows as it is supposed to. The |
Problem description
When I run the above code the plot generated should have x axis tick labels showing the year e.g. '2014', '2014', '2014', etc. Instead it has the labels '0007', '0007', '0007'.
If I increment the last date from '2014-10-12' to '2014-10-13' in the 'data' variable by one day (or really change it to anything else) like this:
Then rerun it, it works as expected.
Expected Output
x label on generated plot figure should be '2014', '2014', '2014', ...
What else have you tried?
I ran the same data using only numpy and matplotlib to rule that out as a source of the unexpected output and I was given the expected x-axis label formatting with '2014', '2014', '2014', etc. Here is my code for that;
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.8.final.0
python-bits: 64
OS: Darwin
OS-release: 18.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_AU.UTF-8
LOCALE: en_AU.UTF-8
pandas: 0.24.2
pytest: None
pip: 19.0.3
setuptools: 41.0.0
Cython: None
numpy: 1.16.2
scipy: 1.2.1
pyarrow: None
xarray: 0.11.3
IPython: 7.4.0
sphinx: None
patsy: None
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: 4.3.3
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
The text was updated successfully, but these errors were encountered: