I have some data that I'm plotting with a Python script. After a x-value of ~2000, the data is basically white noise, and needs to be cut out of the graph. I could manually delete from the file, but this would be much easier in the long run by being automated. I prefer to do this using numpy or matplotlib. After a quick documentation scan, I couldn't find any easy solution.
Asked
Active
Viewed 2,174 times
3 Answers
3
You can set limits to the values shown on the x axis with xlim
. In this case:
plt.xlim(xmax=2000)
There is more information in the docs.

mostlyoxygen
- 981
- 5
- 14
1
If instead you want to hard chop the data itself after x but x is not always exactly 2000 you can use a find nearest code originally found here : Find nearest value in numpy array
Then assign your data x and y to new variables like:
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]

Ratchet
- 11
- 4
0
If your x value is continuous then you could do this:
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()
Note that the second plot cuts off values when x is greater than 2000. If your x array is not continuous then you might need to use logical indexing.

pythonweb
- 1,024
- 2
- 11
- 26