【问题标题】:Dynamically update graphics in matplotlib在 matplotlib 中动态更新图形
【发布时间】:2013-08-21 13:55:09
【问题描述】:
# when the code works but it's meaningless to include it
### When I can't get this part to work and I'd need your code

如何在 matplotlib 中隐藏或显示 Axes 对象(子图),以便在同一图中的不同 Axes 之间切换?

我正在使用 matplotlib 在 Tkinter GUI 中显示图形,我想使用单选按钮在同一图中的不同轴之间切换。

基本上我会有一些单选按钮链接到 IntVar():

graphic_version = tk.IntVar()
tk.Radiobutton(root, text='Option1', variable=graphic_version, value=1).pack()
tk.Radiobutton(root, text='Option2', variable=graphic_version, value=2).pack()

然后我会使用自定义方法跟踪 IntVar(),并使用请求的图形更新我的图形:

choice.trace("w", lambda choice: myGraphic.showGraphic(version))

这样每次用户单击单选按钮时,图形都会更新为不同版本的绘图。现在的问题是我不知道如何正确地进行 showGraphic。假设我使用这个类系统来获得绘制相同数据的 2 个不同版本:

class Version1():
    def __init__(self, ax, data):
        self.ax = ax #This is a Axes object
        self.data = self._formatDataV1(data)

        self._draw()
        self._setOptions()
        self.hide()

    def _formatDataV1(self, data):
        #Here I manipulate the raw data to extract the info I need for this version
        #Just a bunch of math algorithms it works fine

    def _setOptions(self):
        #Here I can overwrite or change settings specific for this version

    def _draw(self):
        self.ax.bar(self.data[0], self.data[1], width=1, color='red')
        self._setOptions()

    def hide(self):
        ###How do I remove the ax without affecting the figure?

    def show(self):
        ###If I want to see this version again I don't want the cost of redrawing   


class Version2():
    def __init__(self, ax, data):
        self.ax = ax #This is a Axes object
        self.data = self._formatDataV1(data)

        self._draw()
        self._setOptions()
        self.hide()

    def _formatDataV2(self, data):
        #The data is manipulated differently here to extract new information

    def _setOptions(self):
        #These options are specific to the version2 to get the display right

    def _draw(self): #Drawing a different version of the graphic with differently formated data
        self.ax.plot(self.data[0], self.data[1])
        self._setOptions()

    def hide(self):
        ###How do I remove the ax without affecting the figure?

    def show(self):
        ###If I want to see this version again I don't want the cost of redrawing

class MyGraphic(tk.LabelFrame):
    def __init__(self, root, data, **options):
        #I use the labelframe only as a container to make things pretty
        tk.LabelFrame.__init__(self, root, text="My 1337 graphic : ", **options)

        self.data = data
        self.fig = mpl.figure.Figure()
        self.ax = self.fig.add_subplot(111)

        self._drawCanvas() #This is just for Tkinter compatibility

        self.my_versions = {}
        self.my_versions.update({'v1' : Version1(self.ax, self.data)})
        self.my_versions.update({'v2' : Version2(self.ax, self.data)})

    def _drawCanvas(self):
        self.canvas = FigureCanvasTkAgg(self.figure, master=self)
        self.canvas.show()
        self.canvas.get_tk_widget().grid()
        self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)

    def _setOptions(self, **kwargs):
        #Here I can set options common to both versions of the graphic

    def showGraphic(self, graphic_version):
        for i, item in enumerate(self.my_versions):
            item.hide()
        if graphic_version == 1:
            self.my_versions['v1'].show()
        elif graphic_version == 2:
            self.my_versions['v2'].show()

        self._setOptions()

抱歉,这篇文章太长了,但我宁愿包含太多细节,并在解决后删除那些不必要的细节。

基本上,我希望能够根据用户的选择在同一图形上隐藏和显示不同的斧头。谜题中缺少的部分是 myGraphic.show() 和 myGraphic.hide()。

我也是一个完整的 matplotlib 新手,我尝试了这个设计,因为它看起来很清晰,很容易在需要时实现其他版本,但设计输入也非常感谢。

【问题讨论】:

  • set_visible 是我想你要找的。​​span>
  • 另外,matplotlib 对象层次结构设计得很好,没有很多充分的理由用你自己的类来包装它们。

标签: python matplotlib tkinter


【解决方案1】:

您可以使用 figure.delaxes 从图形中删除轴(并使用 add_axes 添加): http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.delaxes

【讨论】:

    【解决方案2】:

    我设法用figure.clear()figure.add_axes(ax) 解决了这个问题 明天我将尝试编辑一个干净的问题答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 2012-06-12
      • 2020-11-06
      • 2022-01-25
      • 2011-04-22
      相关资源
      最近更新 更多