Put a gap/break in a line plot

后端 未结 1 407
闹比i
闹比i 2020-12-15 09:06

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 g

相关标签:
1条回答
  • 2020-12-15 09:38

    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.

    0 讨论(0)
提交回复
热议问题