如何增加显示的 x 和 y 坐标的位数?
How to increase the number of digits of x and y coordinates shown?
使用 matplotlib 中的 %matplotlib notebook
我们可以得到图
如何增加x
和y
坐标中的小数点后位数,将鼠标移到图形上后显示?
谢谢。
这是一个使用 mplcursors 的最小示例。 hover
选项已设置,因此该功能会在悬停时调用(而不是仅在单击时调用)。
显示标准黄色注释 window,您可以更新其中的文本。如果你只想在状态栏中显示一些东西,你可以禁用这个注释。
下面的代码显示了悬停在曲线附近时的光标坐标。默认光标显示已禁用。
显示了 mplcursors 如何识别局部最大值的示例。
from matplotlib import pyplot as plt
import mplcursors
import numpy as np
def show_annotation(sel):
sel.annotation.set_visible(False)
fig.canvas.toolbar.set_message(f'{sel.annotation.xy[0]:.12f};{sel.annotation.xy[1]:.12f}')
fig, ax = plt.subplots()
x = np.linspace(0, 10)
ax.plot(x, np.sin(x))
fig.canvas.mpl_connect("motion_notify_event",
lambda event: fig.canvas.toolbar.set_message(""))
cursor = mplcursors.cursor(hover=True)
cursor.connect("add", show_annotation)
PS:如果只是使用标准注解,可以这样写show_annotation
:
def show_annotation(sel):
sel.annotation.set_text(f'x:{sel.annotation.xy[0]:.12f}\ny:{sel.annotation.xy[1]:.12f}')
Mplcursors 在悬停模式下放大后似乎不显示注释或状态栏更新。设置 hover=False
(默认模式)会导致相同的行为,但只有在单击(或缩放时双击)后才会发生。
from matplotlib import pyplot as plt
import mplcursors
import numpy as np
def show_annotation(sel):
sel.annotation.set_visible(False)
fig.canvas.toolbar.set_message(f'{sel.annotation.xy[0]:.12f};{sel.annotation.xy[1]:.12f}')
fig, ax = plt.subplots()
x = np.linspace(0, 10)
ax.plot(x, np.sin(x))
cursor = mplcursors.cursor(hover=False)
cursor.connect("add", show_annotation)
plt.show()
要始终看到小数,而不是靠近曲线,您可以尝试以下操作(不使用 mplcursors):
fig.canvas.mpl_connect("motion_notify_event",
lambda event: fig.canvas.toolbar.set_message(f"{event.xdata:.12f};{event.ydata:.12f}"))
使用 matplotlib 中的 %matplotlib notebook
我们可以得到图
如何增加x
和y
坐标中的小数点后位数,将鼠标移到图形上后显示?
谢谢。
这是一个使用 mplcursors 的最小示例。 hover
选项已设置,因此该功能会在悬停时调用(而不是仅在单击时调用)。
显示标准黄色注释 window,您可以更新其中的文本。如果你只想在状态栏中显示一些东西,你可以禁用这个注释。
下面的代码显示了悬停在曲线附近时的光标坐标。默认光标显示已禁用。
from matplotlib import pyplot as plt
import mplcursors
import numpy as np
def show_annotation(sel):
sel.annotation.set_visible(False)
fig.canvas.toolbar.set_message(f'{sel.annotation.xy[0]:.12f};{sel.annotation.xy[1]:.12f}')
fig, ax = plt.subplots()
x = np.linspace(0, 10)
ax.plot(x, np.sin(x))
fig.canvas.mpl_connect("motion_notify_event",
lambda event: fig.canvas.toolbar.set_message(""))
cursor = mplcursors.cursor(hover=True)
cursor.connect("add", show_annotation)
PS:如果只是使用标准注解,可以这样写show_annotation
:
def show_annotation(sel):
sel.annotation.set_text(f'x:{sel.annotation.xy[0]:.12f}\ny:{sel.annotation.xy[1]:.12f}')
Mplcursors 在悬停模式下放大后似乎不显示注释或状态栏更新。设置 hover=False
(默认模式)会导致相同的行为,但只有在单击(或缩放时双击)后才会发生。
from matplotlib import pyplot as plt
import mplcursors
import numpy as np
def show_annotation(sel):
sel.annotation.set_visible(False)
fig.canvas.toolbar.set_message(f'{sel.annotation.xy[0]:.12f};{sel.annotation.xy[1]:.12f}')
fig, ax = plt.subplots()
x = np.linspace(0, 10)
ax.plot(x, np.sin(x))
cursor = mplcursors.cursor(hover=False)
cursor.connect("add", show_annotation)
plt.show()
要始终看到小数,而不是靠近曲线,您可以尝试以下操作(不使用 mplcursors):
fig.canvas.mpl_connect("motion_notify_event",
lambda event: fig.canvas.toolbar.set_message(f"{event.xdata:.12f};{event.ydata:.12f}"))