简体   繁体   English

使用subplot2grid为每个子图进行不同的回调

[英]Different Callback for each subplot using subplot2grid

Hello dear Python comunity developper, I would like to know if there is a way to have different callback for every subplot2grid (matplotlib), For example: For the first subplot2grid I want to execute a function which is different from the second subplot2grid that generates executes another function. 您好亲爱的Python社区开发人员,我想知道是否有一种方法可以对每个subplot2grid(matplotlib)具有不同的回调, 例如:对于第一个subplot2grid,我想执行一个与生成执行第二个subplot2grid不同的函数另一个功能。

I specify that i'm using subplot2grid and not subplot in matplotlib. 我指定我正在使用subplot2grid而不是matplotlib中的subplot。 Thank you, 谢谢,

If your goal is to use a widget.Button for each subplot, then the situation is very easy. 如果您的目标是为每个子图使用一个widget.Button ,则情况非常简单。 To create a button you need to pass it an Axes instance and the button will occupy that space. 要创建按钮,您需要向其传递一个Axes实例,该按钮将占用该空间。 So you need to create as many new axes as you have subplots, and specify their coordinates appropriately. 因此,您需要创建与子图一样多的新轴,并适当地指定其坐标。 Then create your buttons, which can have different callback functions. 然后创建您的按钮,它可以具有不同的回调函数。

for example: 例如:

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()

在此处输入图片说明

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

example of implementation: http://matplotlib.org/examples/widgets/buttons.html 实施示例: http : //matplotlib.org/examples/widgets/buttons.html

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

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