0

I am still working on getting nice rounded datatime in xticks: rounding datetimes in xticks matplotlib

So the answer was fine, and I could use

plt.gca().xaxis.set_major_locator(mdates.HourLocator()) 

to get round hours in the xticks.

However, if I simply do

plt.gca().xaxis.set_major_locator(mdates.DateLocator()) 

it gives:

  File "/python/anaconda-2.7.11-64/lib/python2.7/site-packages    /matplotlib/ticker.py", line 1289, in __call__
    raise NotImplementedError('Derived must override')
NotImplementedError: Derived must override

Is it really impossible to change HourLocator() to DateLocator() ? I am using matplotlib.__version__'2.0.2'

00__00__00
  • 4,834
  • 9
  • 41
  • 89
  • 1
    maybe try `AutoDateLocator`? or if you know that you want months or years for example, try `MonthLocator` or `YearLocator`? – tmdavison Oct 04 '17 at 11:55

1 Answers1

3

You are not meant do use DateLocator by itself. This is the baseclass of all other locators for dates that matplotlib provides. The matplotlib dates API page lists the possible locators to be

Here are all the date tickers:

  • MinuteLocator: locate minutes
  • HourLocator: locate hours
  • DayLocator: locate specifed days of the month
  • WeekdayLocator: Locate days of the week, e.g., MO, TU
  • MonthLocator: locate months, e.g., 7 for july
  • YearLocator: locate years that are multiples of base
  • RRuleLocator: locate using a matplotlib.dates.rrulewrapper. The rrulewrapper is a simple wrapper around a dateutil.rrule (dateutil) which allow almost arbitrary date tick specifications. See rrule example.
  • AutoDateLocator: On autoscale, this class picks the best MultipleDateLocator to set the view limits and the tick locations.

Choose any one of those and you will be fine.

In addition to those mentionned, there is also a SecondLocator and a MicrosecondLocator available, which are not (well) documented.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • how do I set the maximum number of desired ticks when using the autodatelocator? – 00__00__00 Oct 04 '17 at 12:05
  • The [AutoDateLocator](https://matplotlib.org/api/dates_api.html#matplotlib.dates.AutoDateLocator) has an argument `maxticks`. Does it not work? – ImportanceOfBeingErnest Oct 04 '17 at 12:13
  • It does work if I do mdates.AutoDateLocator(minticks=total_days, maxticks=total_days) – 00__00__00 Oct 04 '17 at 12:17
  • However, when I input the same value for minticks and maxticks, I get an error UserWarning: AutoDateLocator was unable to pick an appropriate interval for this date range. It may be necessary to add an interval value to the AutoDateLocator's intervald dictionary. Defaulting to 10000. ' Defaulting to {0}.'.format(interval)) – 00__00__00 Oct 04 '17 at 12:18
  • The problem arises when plotting multiple times on the same axis. How do I get the current xlim() in datetime format? – 00__00__00 Oct 04 '17 at 12:20
  • 1
    You should give the AutoDateLocator some freedom to choose some good locations (hence the name *Auto*). Setting min and max to the same value may result in strange things. This is what the warning is telling you. Note that its not an error; a warning can be ignored if the result is otherwise fine. The limits of the axes are obtained via `ax.get_xlim()`. – ImportanceOfBeingErnest Oct 04 '17 at 12:31