Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

python - Plotting a curve with equidistant (arc-length) markers

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 intervals as shown in the graph below:

Graph with equal arc-length markers

I know about the markevery argument to plot, but that would bunch up the markers to the right of the plot and probably have quite few markers on the left. The solution should be independent of the scales on the x and y axes. I am open to installing additional modules, but it should be a python+matplotlib solution.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...