plt图右上角显示的值变化

Changing values displayed in top right corner of plt diagram

如何更改matplot图形右上角显示的值?默认情况下,它显示当前光标位置的坐标,但我更喜欢它显示当前 x 光标坐标的显示数据值。我在附图中标记了这些值。 diagram

编辑:这是一个简单的代码。请告诉我如何解决这个例子的上述问题:

import numpy as np
from matplotlib import pyplot as plt

x = np.sin(np.arange(0,100,0.1))

fig, ax = plt.subplots()
ax = plt.plot(x)
plt.show()

您可以使用 format_coord:

在 NavigationToolbar 中定义这些坐标的格式
import numpy as np
from matplotlib import pyplot as plt

def f(x):
    return np.sin(x)

x = np.arange(0, 100, 0.1)
y = f(x)

fig, ax = plt.subplots()
ax.plot(x, y)
#this can be defined for each axis object either using a def function
#or in simple cases a lambda function
ax.format_coord = lambda x, y: f"x: {x:.2f}, f(x): {f(x):.4f}"
plt.show()