我在 matplotlib 工具栏中添加了一个名为“选择点”的小部件,它允许我在图形窗口中选择一组点并将它们写入文件。这很好用。但是,如果我以前必须在单击工具栏上的小部件按钮之前缩放或平移到我感兴趣的区域,则缩放或平移模式将保持活动状态,并且我无法使用小部件按钮选择我的点,除非我手动单击缩放/再次单击工具栏上的平移按钮即可停用这些模式。单击小部件按钮时,有没有办法以编程方式退出缩放或平移模式?这是一个小小的痛苦,但却是我想要拥有的一个很好的功能。
我尝试查看所有 plt.gcf().canvas 方法,但似乎找不到可以从代码中选择/取消选择工具栏模式的位置。
---将工具栏小部件按钮“选择点”链接到 getPoints 类的部分代码---
# Get figure handles
figNum = plt.gcf()
ax = plt.gca()
pts = ax.collections[-1]
# Route 'Select Points' button to matplotlib figure window
fig1.canvas.manager.toolmanager.add_tool('Select Points', getPoints, figNum = figNum, ax = ax, pts = pts, outPath = outPath, origTitle = origTitle)
fig1.canvas.manager.toolbar.add_tool('Select Points', 'navigation', 3)
---获取积分类---
class getPoints(ToolToggleBase):
default_toggled = False
def __init__(self, *args, figNum, ax, pts, outPath, origTitle, **kwargs):
# Initialize base variables
self.figNum = figNum
self.ax = ax
self.pts = pts
self.outPath = outPath
self.origTitle = origTitle
# Call super-initialized variables
super().__init__(*args, **kwargs)
def enable(self, *args, **kwargs):
# Get inputs for getPlotPts function
figNum = self.figNum
ax = self.ax
pts = self.pts
outPath = self.outPath
origTitle = self.origTitle
onState = True
# Call getPlotPts function
getPlotPts(self, figNum, ax, pts, outPath, origTitle, onState)
def disable(self, *args, **kwargs):
# Get inputs for getPlotPts function
figNum = self.figNum
ax = self.ax
pts = self.pts
outPath = self.outPath
origTitle = self.origTitle
onState = False
# Call getPlotPts function
getPlotPts(self, figNum, ax, pts, outPath, origTitle, onState)
我认为您在这里需要的是工具栏的 pan() 和 Zoom() 方法,当您单击这些按钮时会调用这些方法。如果您有这样的工具栏参考:
fig,ax = plt.plot(some data)
tbar = fig.canvas.toolbarof the toolbar in your code,