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

Changes to major/minor locator on a DateTime index leads to messed up axis #26293

Open
niklassemmler opened this issue May 6, 2019 · 8 comments

Comments

@niklassemmler
Copy link

niklassemmler commented May 6, 2019

Code Sample

import matplotlib
matplotlib.use('Agg')
import matplotlib.ticker as ticker
import matplotlib.dates as mdates
from matplotlib import pyplot as plt

from datetime import datetime
import pandas

# Creates problems
hours = range(24)

# Works fine
# hours = list(range(12)) + list(range(13, 24))

times = []
for hour in hours:
     times.append(datetime(year=2019, month=5, day=6, hour=hour))
df_example = pandas.DataFrame({'column':range(len(times))}, index=times)

f, ax = plt.subplots()

# Works fine
# ax.plot(df_example.index, df_example['column'])

# Creates problems
df_example.plot(ax=ax)

hour_locator = mdates.HourLocator(byhour=range(0, 24, 6))
ax.xaxis.set_minor_formatter(mdates.DateFormatter('%H:%M'))
ax.xaxis.set_minor_locator(hour_locator)
ax.xaxis.set_major_locator(ticker.NullLocator())
ax.xaxis.set_major_formatter(ticker.NullFormatter())
f.savefig('example.png')

Problem description

I was plotting a DataFrame with a DateTime index and tried to change the major and minor ticks afterwards. I expected an x-axis with ticks every 6-hours. Instead the x-axis became filled with ticks at what I assume to be hourly intervals.

This behavior does not occur, when either matplotlib is used to plot the columns ax.plot(...) or when the DateTime index contains gaps. Both cases are shown in the comments above.

The issue appeared both with pandas versions 0.24.1 and 0.24.2.

Expected Output

Expected

Output

Output

@gfyoung
Copy link
Member

gfyoung commented May 6, 2019

cc @TomAugspurger @mroeschke

@TomAugspurger
Copy link
Contributor

Pandas tick formatting is quite a bit more complex that that. I would recommend just using matplotlib entirely, rather than trying to customize a pandas-produced axes.

@TomAugspurger
Copy link
Contributor

If you can identify a specific issue, then we can leave this open. Otherwise I worry that it's just going to sit around in an unclear state.

@niklassemmler
Copy link
Author

Hi Tom, in the end I used matplotlib directly. I think it would help to at least a sentence somewhere that states your recommendation. That would have at least helped me. Maybe a comment in a documentation would be good or maybe this issue is enough.

@TomAugspurger
Copy link
Contributor

TomAugspurger commented May 17, 2019 via email

@niklassemmler
Copy link
Author

Well, the wording could be either "Don't use pandas for changing ticks in a timeseries plot." to "While ticks in a timeseries plot can be changed in pandas, it is still an experimental feature. Use matplotlib when you experience troubles." Or if you have more insight, in when the problem occurs, then it would help to mention that.

@TomAugspurger
Copy link
Contributor

TomAugspurger commented May 22, 2019 via email

@niklassemmler
Copy link
Author

Sorry for the delay, above you find a pull request to add a line to the docs.

@mroeschke mroeschke added the Docs label Jul 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants