gpt4 book ai didi

python - 在图中的 x 刻度中显示值和索引

转载 作者:太空宇宙 更新时间:2023-11-03 16:18:44 25 4
gpt4 key购买 nike

我想使用 plot(x,y) 绘制一些内容,并能够在绘图上看到 x 中值的索引

例如。

x = [10,30,100,120]
y = [16,17,19,3]
plot(x,y)

将显示

enter image description here

查看该图,很难知道每个点的原始索引是什么。例如,我希望在查看点 (100,19) 时能够知道它是索引 2,因为 x[2]=100y[2]=19。我该如何在 matplotlib 中做到这一点?我查看了 twiny() 函数,但这似乎只是添加了另一个轴,而忽略了 x 中点之间的距离。

最佳答案

这是我的解决方案:

import matplotlib.pyplot as plt
x = [10,30,100,120]
y = [16,17,19,3]
plt.plot(x,y);
for i, (a, b) in enumerate(zip(x, y)):
plt.annotate(str(i), xy=(a, b), textcoords="offset points", xytext=(0, 12),
horizontalalignment='center', verticalalignment='center')
plt.xlim(0, 130)
plt.ylim(0, 22)

它的作用:它枚举您的 yx数组,将索引存储在变量 i 中以及 x 的相应值和y在变量a中和b 。然后它注释索引 i在坐标 (a, b) ,在 y 轴上将文本偏移 12 个像素,以避免注释覆盖曲线。

结果:

enter image description here

关于python - 在图中的 x 刻度中显示值和索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38678085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com