Issue
I have some data of a particle moving in a corridor with closed boundary conditions.
Plotting the trajectory leads to a zig-zag-trajectory.
I would like to know how to hinder plot()
from connecting the points, where the particle comes back to the start. Some thing like in the upper part of the pic, but without "."
First idea I had is to find the index when the numpy
array a[:-1]-a[1:]
gets positiv. And then plot from 0 to that index. But how to get the index of the first occurrence of a positive element of a[:-1]-a[1:]
?
Maybe there are some other ideas.
Solution
I'd go a different approach. First, I'd determine the jump points not by looking at the sign of the derivative, as probably the movement might go up or down, or even have some periodicity in it. I'd look at those points with the biggest derivative.
Second, an elegant approach to have breaks in a plot line is to mask one value on each jump. Then matplotlib will make segments automatically. My code is:
import pylab as plt
import numpy as np
xs = np.linspace(0., 100., 1000.)
data = (xs*0.03 + np.sin(xs) * 0.1) % 1
plt.subplot(2,1,1)
plt.plot(xs, data, "r-")
#Make a masked array with jump points masked
abs_d_data = np.abs(np.diff(data))
mask = np.hstack([ abs_d_data > abs_d_data.mean()+3*abs_d_data.std(), [False]])
masked_data = np.ma.MaskedArray(data, mask)
plt.subplot(2,1,2)
plt.plot(xs, masked_data, "b-")
plt.show()
And gives us as result:
The disadvantage of course is that you lose one point at each break - but with the sampling rate you seem to have I guess you can trade this in for simpler code.
Answered By - Thorsten Kranz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.