Skip to content

Unevenly spaced ticks in plot_date() #13183

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

Closed
chrisjbillington opened this issue Jan 14, 2019 · 7 comments
Closed

Unevenly spaced ticks in plot_date() #13183

chrisjbillington opened this issue Jan 14, 2019 · 7 comments

Comments

@chrisjbillington
Copy link

Bug report

When plotting with plot_date(), the x axis ticks can be unevenly spaced. It looks like when the tick spacing is less than a month, the start of the month is always present as a tick, which may mean the space between it and the previous tick is small. This can lead to overlapping tick labels and generally poor looking axes and grids.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

start = np.datetime64('2018-01-01', 'D')
end = np.datetime64('2018-04-01', 'D')
dates = np.arange(start, end)
data = np.random.randn(len(dates))

plt.plot_date(dates, data)
plt.grid(True)
plt.gcf().autofmt_xdate()
plt.show()

Actual outcome

image

Expected outcome

The plot should have evenly spaced gridlines and axis ticks, unless a custom locator has specified otherwise.

Matplotlib version

  • Operating system: Arch Linux
  • Matplotlib version: 3.0.2
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.7.2
  • Jupyter version (if applicable): n/a
  • Other libraries: matplotlib 1.15.4

Matplotlib, Python and numpy are installed with the OS package manager using the standard Arch Linux repositories.

@jklymak
Copy link
Member

jklymak commented Jan 14, 2019

Yes, this was an unintended consequence of #9801. Its been "fixed" by #10841 which is on master...

figure_1

Now "fixed" is still different to how it was before, which was (v2.2.3):

figure_1-3

If you want the old behaviour back, you need to do a bit of a song and dance.

locator = mdates.AutoDateLocator(interval_multiples=False)
formatter = mdates.AutoDateFormatter(locator)

plt.plot_date(dates, data)
plt.gca().xaxis.set_major_locator(locator)
plt.gca().xaxis.set_major_formatter(formatter)

plt.grid(True)
plt.gcf().autofmt_xdate()
plt.show()

I agree that the way things are in 3.0.2 are undersirable because to intervals were not good (i.e. you could get 29, 1 ticks). OTOH, I think the new behaviour (as of master) is better than the 2.2.3 behaviour.

@chrisjbillington
Copy link
Author

chrisjbillington commented Jan 14, 2019

Oh, that sounds like a great improvement! Snapping the ticks to month edges makes a lot of sense (so long as they're still evenly spaced).

I have no complaints then, and will close this issue since it's fixed already.

@jklymak
Copy link
Member

jklymak commented Jan 14, 2019

No problem - sorry for the inconvenience of the bad state right now...

@jklymak
Copy link
Member

jklymak commented Jan 14, 2019

Oh, sorry, "evenly spaced" for months won't work, of course.... Note the ticks are not evenly spaced above...

@chrisjbillington
Copy link
Author

Oh, right, since not all months have the same number of days. Well, it's close enough not to be jarring and is more understandable than exactly even spacing (I guess I didn't realise prior to now that this was a trade-off, I just though matplotlib had been choosing the ticks poorly for no reason).

@jklymak
Copy link
Member

jklymak commented Jan 14, 2019

OK, thats what "we" decided, but its good to make sure the users are happy too. These things come down to esthetic decisions, and not everyone agrees on the best defaults.

BTW there will also be a new formatter available, so if you happen to be on master, your opinions on that would be welcome. https://matplotlib.org/devdocs/gallery/ticks_and_spines/date_concise_formatter.html#sphx-glr-gallery-ticks-and-spines-date-concise-formatter-py

@chrisjbillington
Copy link
Author

The ConciseDateFormatter looks great! I tried it out and can't think of any complaints against it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants