You are reading an old version of the documentation (v2.0.0). For the latest version see


This Page

api example code: date_index_formatter.pyΒΆ

(Source code, png, pdf)

Custom tick formatter for time series

When plotting time series, e.g., financial time series, one often wants
to leave out days on which there is no data, i.e. weekends.  The example
below shows how to use an 'index formatter' to achieve the desired plot
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.cbook as cbook
import matplotlib.ticker as ticker

datafile = cbook.get_sample_data('aapl.csv', asfileobj=False)
print('loading %s' % datafile)
r = mlab.csv2rec(datafile)

r = r[-30:]  # get the last 30 days

# first we'll do it the default way, with gaps on weekends
fig, axes = plt.subplots(ncols=2, figsize=(8, 4))
ax = axes[0]
ax.plot(, r.adj_close, 'o-')

# next we'll write a custom formatter
N = len(r)
ind = np.arange(N)  # the evenly spaced plot indices

def format_date(x, pos=None):
    thisind = np.clip(int(x + 0.5), 0, N - 1)

ax = axes[1]
ax.plot(ind, r.adj_close, 'o-')
ax.set_title("Custom tick formatter")

Keywords: python, matplotlib, pylab, example, codex (see Search examples)