我正在繪制圖表,我想繪制使用 scipy 找到的峰值的值。現在我有 2 個 numpy 陣列,它們是每個峰值的x值和y值。當我繪制完整的 x 和峰值的放大圖時,我想遍歷這些峰值并注釋所選 x 限制 ( xlim ) 之間的所有內容。
我試圖只注釋所有值,但我無法讓它作業。
for i in x[0:-1]:
plt.annotate(str(int(y[i])),(x[i],y[i]))
給我IndexError: only integers, slices ( :
), ellipsis ( ...
), numpy.newaxis ( None
) 和 integer or boolean arrays are valid indices and if I do astype(int)我得到IndexError: index 8 is out of bounds for axis 0 with當我從 0 索引到 -1 時,我不明白尺寸 7 ?
你能幫我做我想做的事情,并添加一些限制它只運行 plt.annotate 為xlim范圍內的 x 值的東西嗎?非常感謝
資料:
x = [ 1.5293 2.9786 8.6734 11.944 13.309 13.337 14.591 ]
y = [ 3978.072 1391.799 1739.091 218146.734 29643.477 29680.311
965.577]
xlim = [11.5, 12.5]
uj5u.com熱心網友回復:
正如評論中所說,您直接迭代 and 的值x
,y
因此無需索引:
import numpy as np
import matplotlib.pyplot as plt
x = [1.5293, 2.9786, 8.6734, 11.944, 13.309, 13.337, 14.591]
y = [3978.072, 1391.799, 1739.091, 218146.734, 29643.477, 29680.311, 965.577]
xlim = [11.5, 12.5]
fig, ax = plt.subplots()
ax.plot(x, y, "-o")
for x_, y_ in zip(x, y):
if xlim[0] < x_ < xlim[1]:
ax.annotate(str(y_), (x_, y_))
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/525362.html
下一篇:如何呼叫另一個檔案夾中的視圖