import numpy as np
import matplotlib.pyplot as plt
# some sample data
x = np.linspace(-np.pi, np.pi, 201)
y = np.sin(x)
# define lower and upper border (range of interest)
lower_border = -1
upper_border = 0
# get indices of elements in desired range
idx = np.argwhere((lower_border < x) & (x < upper_border))
# get y-values in desired range
selection = y[idx]
# generate plot for illustration
plt.axvline(lower_border, ls='--', color='grey')
plt.axvline(upper_border, ls='--', color='grey')
plt.plot(x, y)
plt.plot(x[idx], selection)
plt.show()
2条答案
按热度按时间qnakjoqk1#
你可以试试这个:答案=数据[2,7:11]
或者直接在数组中访问它:
答案=数据2[7:11]
我只是假设x坐标是9和11
0h4hbjxa2#
正如您在评论中所阐明的,您使用的是一个二维数组作为数据。然而,作为你的一行
意味着,您将基于两个1d数组(一个数组表示x值,另一个数组表示y值)打印数据。
因此,您可以使用给定的x范围来标识所需范围内x值的所需索引。然后,可以使用这些索引来相应地选择y值,并用所选数据生成绘图。
基本方法可能是:
将生成以下绘图: