绘制周期轨迹 [英] Plot periodic trajectories

查看:108
本文介绍了绘制周期轨迹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些在边界条件封闭的情况下在走廊中移动的粒子的数据. 绘制轨迹会导致曲折轨迹.

I have some data of a particle moving in a corridor with closed boundary conditions. Plotting the trajectory leads to a zig-zag-trajectory.

我想知道如何阻止plot()连接粒子回到起点的点.类似于图片的上部,但没有"."

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 "."

我的第一个想法是在numpy数组a[:-1]-a[1:]获得正值时找到索引.然后从0绘制到该索引.但是,如何获取a[:-1]-a[1:]的正元素首次出现的索引? 也许还有其他想法.

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.

推荐答案

我会采用另一种方法.首先,我不会通过查看导数的符号来确定跳跃点,因为运动可能会上升或下降,甚至可能有一定的周期性.我会用最大的导数看那些点.

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.

第二,在情节线上打断的一种优雅方法是在每次跳跃时都掩盖一个值.然后,matplotlib将自动进行细分.我的代码是:

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()

并给出结果:

当然,缺点是您每次中断都会损失1分-但是您似乎拥有采样率,因此您可以将其换成更简单的代码.

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.

这篇关于绘制周期轨迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆