[英]matplotlib how to adjust navigation and cursor display
使用twinx
在兩個不同的y尺度上繪制兩個時間序列時:
ax1 = pp.subplot(111)
ax2 = ax1.twinx()
導航的默認行為似乎是:
ax2
我經常發現用set_navigate
關閉其中一個軸的縮放/平移很有用(感謝這個提示)。 但是,當ax2的導航關閉時,坐標顯示也會關閉。
無論如何在不關閉坐標顯示的情況下禁用縮放/平移?
更好的是,有沒有辦法在顯示光標坐標時指定兩個軸中的哪一個取y值?
[版本信息:python 3.4.3 + matplotlib 1.5.1]
似乎沒有簡單的方法(ala set_navigate
)來做到這一點。 我設法找到了這個問題並對其進行了一些修改以完成我的需要:
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)))
然后我可以像這樣實例化一個Cursor
:
fig,ax = plt.subplots(1,1)
ax2 = ax.twinx()
Cursor(fig, ax, ax2)
無論兩個軸的導航狀態如何,左/右單擊圖將在導航工具欄的右下角顯示左/右軸的坐標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.