When plotting daily data, e.g., financial time series, one often wants
to leave out days on which there is no data, for instance weekends, so that
the data are plotted at regular intervals without extra spaces for the days
with no data.
The example shows how to use an 'index formatter' to achieve the desired plot.
Instead of passing a function into Axis.set_major_formatter you can use
any other callable, e.g. an instance of a class that implements __call__:
classMyFormatter(Formatter):def__init__(self,dates,fmt='%a'):self.dates=datesself.fmt=fmtdef__call__(self,x,pos=0):"""Return the label for time x at position pos."""try:returnself.dates[round(x)].item().strftime(self.fmt)exceptIndexError:passax2.xaxis.set_major_formatter(MyFormatter(r["date"],'%a'))plt.show()
Total running time of the script: (0 minutes 1.238 seconds)