.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python_packages\matplotlib\demo_dateaxis.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_packages_matplotlib_demo_dateaxis.py: Date axis ============ Load a numpy record array from yahoo csv data with fields date, open, close, volume, adj_close from the mpl-data/example directory. The record array stores the date as an np.datetime64 with a day unit ('D') in the date column. .. GENERATED FROM PYTHON SOURCE LINES 8-56 .. image-sg:: /11_demos/python_packages/matplotlib/images/sphx_glr_demo_dateaxis_001.png :alt: demo dateaxis :srcset: /11_demos/python_packages/matplotlib/images/sphx_glr_demo_dateaxis_001.png :class: sphx-glr-single-img .. code-block:: default import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates import matplotlib.cbook as cbook import matplotlib years = mdates.YearLocator() # every year months = mdates.MonthLocator() # every month yearsFmt = mdates.DateFormatter("%Y") if matplotlib.__version__ < "3.3.0": with cbook.get_sample_data("goog.npz") as datafile: r = np.load(datafile) else: r = cbook.get_sample_data("goog.npz", np_load=True) r = r["price_data"].view(np.recarray) fig, ax = plt.subplots() ax.plot(r.date, r.adj_close) # format the ticks ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) ax.xaxis.set_minor_locator(months) # round to nearest years... datemin = np.datetime64(r.date[0], "Y") datemax = np.datetime64(r.date[-1], "Y") + np.timedelta64(1, "Y") ax.set_xlim(datemin, datemax) # format the coords message box def price(x): return "$%1.2f" % x ax.format_xdata = mdates.DateFormatter("%Y-%m-%d") ax.format_ydata = price ax.grid(True) # rotates and right aligns the x labels, and moves the bottom of the # axes up to make room for them fig.autofmt_xdate() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.193 seconds) .. _sphx_glr_download_11_demos_python_packages_matplotlib_demo_dateaxis.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_dateaxis.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_dateaxis.ipynb `