【问题标题】:setting tickslabel in matplotlib disables mouse cursor position在 matplotlib 中设置 tickslabel 会禁用鼠标光标位置
【发布时间】:2012-09-26 15:51:08
【问题描述】:

每当我在 matplotlib 中绘制某些东西时,将鼠标移到绘图上会在底部显示光标的 x 和 y 位置。这在探索数据时非常方便。

现在,如果我将刻度标签设置为其他值,则绘图底部光标的 x 位置为空,而 gui 会跟踪 y 位置。有没有办法继续获得 x 位置?

这是一个简单的例子:

import numpy as np
fig, ax = plt.subplots()
x=np.linspace(0,np.pi)
y=np.sin(x)
ax.plot(x,y)
ax.set_xticks([0,np.pi/2,np.pi])
ax.set_xticklabels(['0','pi/2','pi'])
plt.show()

【问题讨论】:

  • 有趣。附带说明一下,如果您使用 ['0',r'$\pi/2$',r'$\pi$'],您可以获得呈现为 TeX 的标签。

标签: python numpy matplotlib


【解决方案1】:

不,没有简单的方法可以做到这一点。

当您使用字符串列表设置标签时,您将 xaxis.major_formatter 设置为 FixedFormatter (doc),这具有使其工作的一些特殊行为(它总是返回 '',除非您给它第三个当它标记 x 轴时给出的参数,但不是其他的)。光标所在位置的标记是使用major_formatter 生成的,当您调用FixedFormatter 时,它会返回'',即显示的内容。

如果你真的想要这个,你必须写你自己的回调,请参阅Color values in imshow for matplotlib?作为起点。 (或子类 axes 并重新实现 format_coord (在当前 matplotlib 主分支中的 axes.py 中的第 2903 行附近)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多