I'm plotting data with more than 4 dimensions. To make visualizations easier, I also want to plot all possible 2D and 3D combinations of the different dimensions.

To do this, I want one figure and navigate through the different options with RadioButtons. Now, as I change from 2D to 3D, I need a different amount of axes, so I clear my entire figure and plot everything again, but then my RadioButton doesn't work anymore (though I plot it again after clf()):

    class TEST(object):


def __init__(self):      
    self.num2d = 6
    self.rows2d = 3
    self.cols2d = 2

    self.num3d = 4
    self.rows3d = 2
    self.cols3d = 2

    self.fig2 = plt.figure()
    self.add2dAxes = []

    for i in range(self.num2d):
        ii = 100*self.rows2d + 10*self.cols2d + i+1
        self.add2dAxes.append(plt.subplot(ii))
    self.add_dim = 2

    self.updatePlotAdd()

    def click_dim(dim):
        print dim
        if dim == "2D":
            self.updatePlotAdd2d()
        elif dim == "3D":
            self.updatePlotAdd3d()
        else:
            self.updatePlotAdd4d()
    self.radioDim.on_clicked(click_dim)
    self.fig2._my_btn = self.radioDim
    plt.show()

def updatePlotAdd2d(self):
    print "2D"
    self.add_dim = 2
    self.fig2.clf()
    self.add2dAxes = []
    for i in range(self.num2d):
        ii = 100*self.rows2d + 10*self.cols2d + i+1
        self.add2dAxes.append(plt.subplot(ii))   
    self.updatePlotAdd()
def updatePlotAdd3d(self):
    print "3D"
    self.add_dim = 3
    self.fig2.clf()
    self.add3dAxes = []
    for i in range(self.num3d):
        ii = 100*self.rows3d + 10*self.cols3d + i+1
        self.add3dAxes.append(plt.subplot(ii)) 
    self.updatePlotAdd()

def updatePlotAdd4d(self):
    print "4D"
    self.add_dim = 4
    self.fig2.clf()
    self.updatePlotAdd()

def updatePlotAdd(self):        
    print "updatePlotAdd"
    rax = plt.axes([0.01, 0.7, 0.08, 0.1], axisbg='grey')
    self.radioDim = RadioButtons(rax, ("2D", "3D","> 3D"), active=self.add_dim-2)
    self.fig2.canvas.draw()

This example shows how the axes change perfectly the first time you click the button, but from then on the button doesn't respond anymore.

有帮助吗?

解决方案

I guess the problem is that in your "updatePlotAdd", you are re-creating RadioButtons object but you did not assign event trigger to this new object.

How about making the private function "click_dim" as class member function and call that in updatePlotAdd?

def click_dim(self, dim):
    print dim
    if dim == "2D":
        self.updatePlotAdd2d()
    elif dim == "3D":
        self.updatePlotAdd3d()
    else:
        self.updatePlotAdd4d()

And adding one line to updatePlotAdd:

def updatePlotAdd(self):        
    print "updatePlotAdd"
    rax = plt.axes([0.01, 0.7, 0.08, 0.1], axisbg='grey')
    self.radioDim = RadioButtons(rax, ("2D", "3D","> 3D"), active=self.add_dim-2)
    self.radioDim.on_clicked(self.click_dim)
    self.fig2.canvas.draw()

Of course the same goes to init too

    self.radioDim.on_clicked(self.click_dim)

With these modifications, it seems to work as you expected in my console.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top