ax.plot_date((dates, dates), (highs, lows), '-')
I'm currently using this command to plot financial highs and lows using Matplotlib. It works great, but how do I remove the blank spaces in the x-axis left by days without market data, such as weekends and holidays?
I have lists of dates, highs, lows, closes and opens. I can't find any examples of creating a graph with an x-axis that show dates but doesn't enforce a constant scale.
I ran into this problem again and was able to create a decent function to handle this issue, especially concerning intraday datetimes. Credit to @Primer for this answer.
Up to date answer (2018) with Matplotlib 2.1.2, Python 2.7.12
The function
equidate_ax
handles everything you need for a simple date x-axis with equidistant spacing of data points. Realised withticker.FuncFormatter
based on this example.I will typically use NumPy's NaN (not a number) for values that are invalid or not present. They are represented by Matplotlib as gaps in the plot and NumPy is part of pylab/Matplotlib.
One of the advertised features of scikits.timeseries is "Create time series plots with intelligently spaced axis labels".
You can see some example plots here. In the first example (shown below) the 'business' frequency is used for the data, which automatically excludes holidays and weekends and the like. It also masks missing data points, which you see as gaps in this plot, rather than linearly interpolating them.
There's an example of how to do this on the Matplotlib site:
https://matplotlib.org/gallery/ticks_and_spines/date_index_formatter.html
scikits.timeseries functionality has largely been moved to pandas, so you can now resample a dataframe to only include the values on weekdays.
and then to plot the dataframe as normal