I have a data set with effectively "continuous" sensor readings, with the occasional gap.
However there are several periods in which no data was recorded. These gaps are significantly longer than the sample period.
By default, pyplot connects each data point to the next (if I have a line style set), however I feel that this is slightly misleading when it connects the two data points either side of a long gap.
I would prefer to simply have no line there; that is, I would like the line to stop and to start again after the gap.
I have tried adding in an element in these gap sections with the y-value None
, but seems to send the line back to an earlier part of the plot (though strangely these lines don't appear at all zoom levels).
The other option I have thought of is to simply plot each piece with a separate call to plot
, but this would be a bit ugly and cumbersome.
Is there a more elegant way of achieving this?
Edit: Below is a minimal working example demonstrating the behaviour. The first plot is the joining line I am trying to avoid. The second plot shows that adding a None
value appears to work, however if you pan the view of the plot, you get what is shown in the third figure, a line jumping to an earlier part of the plot.
import numpy as np
import matplotlib.pyplot as plt
t1 = np.arange(0, 8, 0.05)
t2 = np.arange(10, 14, 0.05)
t = np.concatenate([t1, t2])
c = np.cos(t)
fig = plt.figure()
ax = fig.gca()
ax.plot(t, c)
ax.set_title('Undesirable joining line')
t1 = np.arange(0, 8, 0.05)
t2 = np.arange(10, 14, 0.05)
c1 = np.cos(t1)
c2 = np.cos(t2)
t = np.concatenate([t1, t1[-1:], t2])
c = np.concatenate([c1, [None,], c2])
fig = plt.figure()
ax = fig.gca()
ax.plot(t, c)
ax.set_title('Ok if you don't pan the plot')
fig = plt.figure()
ax = fig.gca()
ax.plot(t, c)
ax.axis([-1, 12, -0.5, 1.25])
ax.set_title('Strange jumping line')
plt.show()
See Question&Answers more detail:
os