Plotting a curve with equidistant (arc-length) markers

后端 未结 2 564
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 07:59

I would like to plot a graph of some experimental data which is sampled at a relatively high rate, but approximates a smooth curve using markers spaced at equal arc-length i

2条回答
  •  再見小時候
    2020-12-10 08:47

    I think I have put together a relatively good solution. The only problem is taking the data ratio into account in a way that also uses information about the aspect ratio of the final plot. I've not found a reliable way to do this, although this function will accept a data ratio so you can play until the output looks right:

    def spacedmarks(x, y, Nmarks, data_ratio=None):
        import scipy.integrate
    
        if data_ratio is None:
            data_ratio = plt.gca().get_data_ratio()
    
        dydx = gradient(y, x[1])
        dxdx = gradient(x, x[1])*data_ratio
        arclength = scipy.integrate.cumtrapz(sqrt(dydx**2 + dxdx**2), x, initial=0)
        marks = linspace(0, max(arclength), Nmarks)
        markx = interp(marks, arclength, x)
        marky = interp(markx, x, y)
        return markx, marky
    

    Example of use (this is suitable for pylab mode in iPython):

    x = linspace(0, 10*pi, 1000)
    y = sin(x*2) + sin(x+1)
    
    plot(x, y)
    markx, marky = spacedmarks(x, y, 80)
    plot(markx, marky, 'o', color='blue')
    

    Result:

    Sample output showing equally spaced markers

提交回复
热议问题