Alex
Alex

Reputation: 1379

Put a gap/break in a line plot

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()

Plot 3, the "strange jumping line"

Upvotes: 16

Views: 21106

Answers (2)

Rainald62
Rainald62

Reputation: 740

The strange lines were a bug in matplotlib 1.1.1.

There is no need to have the t component of the dummy points in chronological order, zero values will also work.

For the c component, I use np.nan instead of None, which (on conversion from a list) forces the dtype to 'float64' instead of 'O' (object).

Dummy points are best inserted at the time of filling the array with samples (or appending to a list), like so:

samples = [] # (t,c) data pairs.
# Waiting for samples in a loop.
    if samples and current_sample[0] > samples[-1][0] + GAP_TOLERANCE:
        samples.append((0, np.nan))
    samples.append(current_sample)

t, c = np.array(samples).T

Upvotes: 1

tom10
tom10

Reputation: 69242

Masked arrays work well for this. You just need to mask the first of the points you don't want to connect:

import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt

t1 = np.arange(0, 8, 0.05)
mask_start = len(t1)
t2 = np.arange(10, 14, 0.05)
t = np.concatenate([t1, t2])
c = np.cos(t)     # an aside, but it's better to use numpy ufuncs than list comps

mc = ma.array(c)
mc[mask_start] = ma.masked
plt.figure()
plt.plot(t, mc)
plt.title('Using masked arrays')

plt.show()

enter image description here

At least on my system (OSX, Python 2.7, mpl 1.1.0), I don't have any issues with panning, etc.

Upvotes: 16

Related Questions