Put a gap / gap in the line chart

I have a dataset with effective "continuous" sensor readings with periodic clearance.

However, there are several periods in which data was not recorded. These gaps are significantly longer than the sampling period.

By default, pyplot connects each data point to the next (if I have a set of line styles), however I feel this is a little misleading when it connects two data points on either side of a long span.

I would rather just not have a line; that is, I would like the line to stop and start again after the break.

I tried adding an element to these markup sections with a y-value of None , but it seems to send the line back to the earlier part of the graph (although it is strange these lines are not displayed at all zoom levels).

Another option that I thought of is to simply build each fragment using a separate plot call, but that would be a little ugly and cumbersome.

Is there a more elegant way to achieve this?

Edit: The following is a minimal working example demonstrating the behavior. The first plot is a connection line that I am trying to avoid. The second graph shows that adding a None value seems to work, however, if you pan the plot, you get what is shown in the third figure, a line that jumps to an earlier part of the graph.

 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"

+8
python matplotlib
source share
1 answer

Masked arrays work well for this. You just need to mask the first of the points that you do not 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 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 have no problems with panning, etc.

+10
source share

All Articles