python - matplotlib - what kind of set_major_locator should I use -


i have question regarding plot feature when x-axis date.

setting

i have 2 time-series dt , x, dt list of datetime.datetime values , x numpy array object.

dt =  [datetime.datetime(1996, 12, 3, 0, 0),   datetime.datetime(1996, 12, 4, 0, 0),   ...,  datetime.datetime(2015, 6, 5, 0, 0),   datetime.datetime(2015, 6, 8, 0, 0)]  x =  array([29.06262, 29.26933, ..., 208.41999, 208.44532]) 

and both have same length.

when plot them :

ax.plot(dt, x, color='black', lw=2) 

i chart has x-tick labels years (e.g. 1994, 1998, 2002, 2006, 2010, 2014).

after zooming, x-tick labels nicely start showing months (e.g. aug 2009, oct 2009, dec 2009, feb 2010, apr 2010, jun 2010).

after further zooming, x-tick labels again nicely start showing days (e.g. jan 14 2010, jan 21 2010, jan 28 2010, feb 04 2010, feb 11 2010, feb 18 2010).

question

now, trying draw custom chart (e.g. using linecollection) instead of using default plot.

i suspect need use ax.xaxis.set_major_locator(???) , ax.xaxis.set_major_formatter(???) same feature of x-axis in default plot function.

what need provide (i.e. ???) ax.xaxis.set_major_locator(???) , ax.xaxis.set_major_formatter(???) ?

yes, assumption right: have set locator , formatter manually. "regular" ones available in matplotlib.ticker , ones related dates in matplotlib.dates. there quite variety of fixed ones (e.g. weekdaylocator, microsecondlocator, etc.), automatic ones when using plot.

so should it:

import matplotlib.pyplot plt import matplotlib.dates mdates  # fancy plotting code  ax.xaxis.set_major_locator(mdates.autodatelocator()) ax.xaxis.set_major_formatter(mdates.autodateformatter())  plt.show() 

further reading: http://matplotlib.org/api/dates_api.html

extra:

be careful list of datetimes: matplotlib has internal date/time representation (number of days floats) , datetime objects need converted before plotting. in plot conversion happens automatically, custom graphs might have matplotlib.dates.date2num.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -