.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/ticks/date_demo_convert.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_ticks_date_demo_convert.py: ================= Date Demo Convert ================= .. GENERATED FROM PYTHON SOURCE LINES 7-40 .. image-sg:: /gallery/ticks/images/sphx_glr_date_demo_convert_001.png :alt: date demo convert :srcset: /gallery/ticks/images/sphx_glr_date_demo_convert_001.png, /gallery/ticks/images/sphx_glr_date_demo_convert_001_2_00x.png 2.00x :class: sphx-glr-single-img .. code-block:: Python import datetime import matplotlib.pyplot as plt import numpy as np from matplotlib.dates import DateFormatter, DayLocator, HourLocator, drange date1 = datetime.datetime(2000, 3, 2) date2 = datetime.datetime(2000, 3, 6) delta = datetime.timedelta(hours=6) dates = drange(date1, date2, delta) y = np.arange(len(dates)) fig, ax = plt.subplots() ax.plot(dates, y**2, 'o') # this is superfluous, since the autoscaler should get it right, but # use date2num and num2date to convert between dates and floats if # you want; both date2num and num2date convert an instance or sequence ax.set_xlim(dates[0], dates[-1]) # The hour locator takes the hour or sequence of hours you want to # tick, not the base multiple ax.xaxis.set_major_locator(DayLocator()) ax.xaxis.set_minor_locator(HourLocator(range(0, 25, 6))) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d')) ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S') fig.autofmt_xdate() plt.show() .. _sphx_glr_download_gallery_ticks_date_demo_convert.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: date_demo_convert.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: date_demo_convert.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_