gpt4 book ai didi

python - 如何在图形/绘图(在 matplotlib 中)上显示对象的值?

转载 作者:行者123 更新时间:2023-11-30 23:39:48 25 4
gpt4 key购买 nike

我希望能够显示一个对象;在我的 matplotlib 图上将其称为“坡度”。例如:

import numpy as np
import matplotlib.pyplot as plt

range1 = a[(-5. <= a) & (-3. >= a)]
range2 = b[(-5. <= a) & (-3. >= a)]

'''Calculate slope value from endpoints in the data range (linear).'''

xslopeentry1 = range1[0]
xslopeentry2 = range1[-1]
yslopeentry1 = range2[0]
yslopeentry2 = range2[-1]
Slope = (yslopeentry2-yslopeentry1)/(xslopeentry2-xslopeentry1)

plt.plot(range1,range2)
plt.show()

现在,我如何能够“打印”或在绘图上显示“坡度”获得的值?

最佳答案

在 matplotlib 中添加文本有多种选项。对它们最好的解释来自documentation .

根据您的目的,有 3 个可能有意义的选项:

1.) Text relative to axes :

matplotlib.pyplot.text(Slope,x,y)

其中 x 和 y 是文本相对于轴的坐标。

2.) Text relative to figure :

matplotlib.pyplot.figtext(Slope,x,y)

其中 x 和 y 是文本相对于图形的坐标

3.) Annotation :

这将创建一个引用特定数据点的文本注释。这在这里没有多大意义,但如果您想要一个指向与斜率相关的线的箭头,它确实允许轻松创建箭头。

matplotlib.pyplot.annotate(Slope, xy=(xx, yy), xytext=(x, y),
arrowprops=dict(facecolor='black', shrink=0.05))

其中x和y是文本坐标,xx、yy是箭头所指点的坐标。

**请注意,上面的示例仅将斜率值放在绘图上。如果您想要“坡度:值”,请将上面的“坡度”替换为:

"Slope: {0}".format(Slope)

关于python - 如何在图形/绘图(在 matplotlib 中)上显示对象的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13223412/

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