简体   繁体   English

matplotlib如何调整导航和光标显示

[英]matplotlib how to adjust navigation and cursor display

When plotting two time series on two different y-scales using twinx : 使用twinx在两个不同的y尺度上绘制两个时间序列时:

ax1 = pp.subplot(111)
ax2 = ax1.twinx()

the default behavior for navigation seems to be: 导航的默认行为似乎是:

  1. zoom/pan will control both axes simultaneously zoom / pan将同时控制两个轴
  2. the lower-right corner displays the mouse cursor coordinates with y-value of ax2 右下角显示鼠标光标坐标,其中y值为ax2

I often find it useful to turn off zoom/pan for one of the axes with set_navigate (thanks to this tip). 我经常发现用set_navigate关闭其中一个轴的缩放/平移很有用(感谢这个提示)。 However when navigation is turned off for ax2, the coordinates display is also turned off. 但是,当ax2的导航关闭时,坐标显示也会关闭。

Is there anyway to disable zoom/pan without turning off the coordinates display? 无论如何在不关闭坐标显示的情况下禁用缩放/平移?

Even better, is there a way to specify which one of the two axes to take the y-value from when displaying the cursor coordinates? 更好的是,有没有办法在显示光标坐标时指定两个轴中的哪一个取y值?

[version info: python 3.4.3 + matplotlib 1.5.1] [版本信息:python 3.4.3 + matplotlib 1.5.1]

It seems that there is no simple way (ala set_navigate ) to do this. 似乎没有简单的方法(ala set_navigate )来做到这一点。 I managed to find this question and modified it a bit to accomplish what I need: 我设法找到了这个问题并对其进行了一些修改以完成我的需要:

class Cursor(object):
    def __init__(self, fig, ax, ax2=None, xfmt=None, yfmt=None):
        self.fig = fig
        self.ax = ax
        self.ax2 = ax2
        self.xfmt = xfmt
        self.yfmt = yfmt
        plt.connect('button_press_event', self)

    def __call__(self, event):
         if event.button == 3 and self.ax2 is not None: # right click
            ax = self.ax2
        elif event.button:
            ax = self.ax
        inv = ax.transData.inverted()
        x, y = inv.transform((event.x, event.y))
        self.fig.canvas.toolbar.set_message('x={} y={}'.format(x if self.xfmt is None else self.xfmt(x), y if self.yfmt is None else self.yfmt(y)))

I can then instantiate a Cursor like this: 然后我可以像这样实例化一个Cursor

fig,ax = plt.subplots(1,1)
ax2 = ax.twinx()
Cursor(fig, ax, ax2)

and regardless of the navigate status of either axis, left/right click on the plot will display the coordinates of left/right axis in the lower right corner of the navigation toolbar. 无论两个轴的导航状态如何,左/右单击图将在导航工具栏的右下角显示左/右轴的坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM