When plotting two time series on two different y-scales using twinx
:
ax1 = pp.subplot(111)
ax2 = ax1.twinx()
the default behavior for navigation seems to be:
ax2
I often find it useful to turn off zoom/pan for one of the axes with set_navigate
(thanks to this tip). However when navigation is turned off for ax2, the coordinates display is also turned off.
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?
[version info: python 3.4.3 + matplotlib 1.5.1]
It seems that there is no simple way (ala set_navigate
) to do this. 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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.