使用matplotlib.pyplot或numpy切断右手点

时间:2018-06-19 22:04:54

标签: python numpy matplotlib

我有一些要用Python脚本绘制的数据。在x值约为2000之后,数据基本上是白噪声,需要从图中删除。我可以手动从文件中删除,但是从长远来看,通过自动化它会容易得多。我更喜欢使用numpy或matplotlib进行此操作。快速文档扫描后,我找不到任何简单的解决方案。

3 个答案:

答案 0 :(得分:1)

您可以使用xlim将限制设置为x轴上显示的值。在这种情况下:

plt.xlim(xmax=2000)

the docs中有更多信息。

答案 1 :(得分:1)

如果您想在x之后硬截取数据本身,但x并非总是2000,则可以使用最初在此处找到的最接近的代码:Find nearest value in numpy array

然后将数据x和y分配给新变量,例如:

import numpy as np
def find_nearest(array, value):
    array = np.asarray(array)
    idx = (np.abs(array - value)).argmin()
    return [idx]

Cutoff_idx = find_nearest(x, 2000.)

Xnew = x[:Cutoff_idx]
Ynew = y[:Cutoff_idx]

答案 2 :(得分:0)

如果您的x值是连续的,则可以执行以下操作:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(1, 3001)
y = np.sin(x/250)

plt.plot(x, y)
plt.show()

plt.plot(x[0:2000], y[0:2000])
plt.show()

请注意,当x大于2000时,第二个图将截断值。如果x数组不连续,则可能需要使用逻辑索引。