Matplotlib Radiobutton在clf()之后不再起作用(Matplotlib Radiobutton doesn't work anymore after clf())

编程入门 行业动态 更新时间:2024-10-26 06:35:34
Matplotlib Radiobutton在clf()之后不再起作用(Matplotlib Radiobutton doesn't work anymore after clf())

我正在绘制超过4维的数据。 为了使可视化更容易,我还想绘制不同尺寸的所有可能的2D和3D组合。

要做到这一点,我想要一个数字并使用RadioButtons浏览不同的选项。 现在,当我从2D更改为3D时,我需要不同数量的轴,所以我清除了整个图形并再次绘制了一切,但是我的RadioButton不再工作了(尽管我在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()

此示例显示了第一次单击按钮时轴的完美变化,但从那时起按钮不再响应。

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.

最满意答案

我想问题是在你的“updatePlotAdd”中,你正在重新创建RadioButtons对象,但你没有为这个新对象分配事件触发器。

如何将私有函数“click_dim”作为类成员函数并在updatePlotAdd中调用它?

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

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

当然,同样适用于init

self.radioDim.on_clicked(self.click_dim)

通过这些修改,它似乎可以在我的控制台中按预期工作。

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.

更多推荐

本文发布于:2023-07-31 05:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1341892.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Radiobutton   Matplotlib   clf   anymore   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!