Matplotlib remove interpolation for missing data

Nipun Batra picture Nipun Batra · May 9, 2013 · Viewed 8.5k times · Source

I am plotting timeseries data using Matplotlib and some of the data is missing in the sequence. Matplotlib implicitly joins the last contiguous data point to the next one. But in case data is missing, the plot looks ugly. The following is the plot obtained. enter image description here

It can be seen that near the April 30th marker, data is missing and Matplotlib joins the points. Also the following image is the scatter plot of the data. Scatter plot covers up this fault, but then contiguous data points won't be joint in this case. Moreover, scatter plot is very slow given the huge number of data points involved. enter image description here

What is the recommended solution for such problems.

Answer

tacaswell picture tacaswell · May 9, 2013

If you can identify where the break points should be you can either:

  1. break the data and plot each 'section' by hand
  2. insert np.nan in the data in the gaps

See for example Plot periodic trajectories.

You can get the same effect of scatter (if you don't want to scale the size or color of each point independently) with

ax.plot(x, y, linestyle='none', marker='o')