[Matplotlib-users] Autoscaling Date Locator with minor ticks?

Thomas Caswell tcaswell at gmail.com
Sat May 25 21:11:22 EDT 2019


Akanna,

Setting the minor and major locators in the correct approach, but you need
to set them with different settings (as other wise they will decide to put
the ticks at the sameplaces, hence the "doubled" labels. This behavior
changed with 3.1 (see
https://matplotlib.org/api/api_changes.html#major-minor-tick-collisions )
and going forward the overlapping minor ticks will be trimmed by default).
I suspect that by playing with `minticks` and `maxticks` you can get the
effect you want:


import numpy as np
import matplotlib.dates as mdates
import matplotlib.pyplot as plt

t = np.arange("2018-11-03", "2018-11-06", dtype="datetime64")
x = np.random.rand(len(t))

fig, ax = plt.subplots()
ax.plot(t, x)
ax.xaxis.set(
    major_locator=mdates.AutoDateLocator(minticks=1, maxticks=5),
    minor_locator=mdates.AutoDateLocator(minticks=15, maxticks=52),
)

plt.show()


You may also be interested in the ConciseDateFormatter (
https://matplotlib.org/users/whats_new.html#concisedateformatter ) that is
new in 3.1.

Tom

On Fri, May 3, 2019 at 9:16 PM Akkana Peck <akkana at shallowsky.com> wrote:

> Is there any way to get AutoDateLocator to handle minor ticks as
> well as major ones?
>
> For instance, if I have a plot spanning a few days, I'd like to be
> able to show the date as a label and major tick, but show minor
> ticks for hours, or one minor tick every six hours, whatever is
> sensible depending on the scale. Or on a plot spanning a few years,
> show labels for each year, but with 12 minor ticks showing months.
> It seems like AutoDateLocator only draws major ticks.
>
> I can write a custom date locator that handles the minor ticks
> nicely, but then I don't get the nice auto-scaling of
> AutoDateLocator, and if I plot a larger or smaller date range,
> my labels and ticks become too crowded or too sparse.
>
> I tried setting both major and minor locators to AutoDateLocators:
>     ax.xaxis.set_major_locator(AutoDateLocator())
>     ax.xaxis.set_minor_locator(AutoDateLocator())
> and similarly with set_*_formatter(), but that just gave me
> smeary-looking double-printed major labels and ticks.
>
> Maybe I need to write a custom smart locator that reproduces
> AutoDateLocator's auto-scaling while also handling ticks. But before
> I dive into that, I thought I'd ask; I can't be the only person who
> needs more precise ticks and labeling than AutoDateLocator provides.
>
> Thanks!
>
>         ...Akkana
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
>


-- 
Thomas Caswell
tcaswell at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190525/396631de/attachment.html>


More information about the Matplotlib-users mailing list