【问题标题】:Different Callback for each subplot using subplot2grid使用 subplot2grid 为每个子图提供不同的回调
【发布时间】:2017-05-07 23:09:13
【问题描述】:

亲爱的 Python 社区开发者,您好, 我想知道是否有办法为每个 subplot2grid (matplotlib) 设置 不同的回调, 例如:对于第一个 subplot2grid,我想执行一个不同于生成执行另一个函数的第二个 subplot2grid 的函数。

我指定我使用的是 subplot2grid 而不是 matplotlib 中的 subplot。 谢谢,

【问题讨论】:

  • 您也许可以包装您的函数,然后计算您已经执行该函数的次数。有了这些信息,你可以调用另一个回调?
  • 谢谢回答,你有例子吗?
  • 我不认为这是可能的,但你可以分两步完成:回调函数从 Ax (subplot) 中找出事件已生成,并在此基础上调用第二个函数,对于每个子图都不同。这完全取决于您对处理哪种事件感兴趣
  • @DizietAsahi 我在每个 subplot2grid 中定义了一个新的回调函数,但它不起作用:-( 是否可以在每个子图旁边包含一个按钮,而不使用 Tk()?跨度>

标签: python matplotlib callback subplot


【解决方案1】:

如果您的目标是为每个子图使用widget.Button,那么情况就很简单了。要创建一个按钮,您需要向它传递一个 Axes 实例,并且该按钮将占据该空间。因此,您需要创建与子图一样多的新轴,并适当地指定它们的坐标。然后创建你的按钮,它可以有不同的回调函数。

例如:

from matplotlib.widgets import Button

def callback1(event):
    print "you've clicked button 1"

def callback2(event):
    print "you've clicked button 2"

fig = plt.figure()
ax1 = plt.subplot2grid((2,2),(0, 0))
ax2 = plt.subplot2grid((2,2),(1,1))

# create axes to receive the buttons
# adjust the coordinates to suit your needs
# coordinates are [left, bottom, width, height]
b1ax = plt.axes([0.5, 0.8, 0.2, 0.1])
b1 = Button(b1ax, 'Button 1')
b1.on_clicked(callback1)
b2ax = plt.axes([0.7, 0.5, 0.2, 0.1])
b2 = Button(b2ax, 'Button 2')
b2.on_clicked(callback2)
plt.show()

widget.Button 的文档:http://matplotlib.org/api/widgets_api.html#matplotlib.widgets.Button

实现示例:http://matplotlib.org/examples/widgets/buttons.html

【讨论】:

  • 啊 thnx 很多,这就是我一直在寻找的,另一个问题^^:有没有办法在被调用的函数中使用事件参数设置其他参数。 ^^
  • 我找到了上一个问题的解决方案:b2ax_outgoing = plt.axes([0.4, 0.4, 0.01, 0.02]) bouttonni2 = buttonn(b2ax_outgoing, '', color='red') bouttonni2。 on_clicked(lambda event: taux_outgoing2(reader, val1, val2, val3, val4, val5, tech, event)) THNX A LOT
  • 不客气。如果答案对您有用,请考虑通过单击左侧的复选标记来接受它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-18
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
相关资源
最近更新 更多