matplotlib 只绘制一次

发布于 2024-08-30 20:30:51 字数 1057 浏览 2 评论 0原文

我在使用 matplotlib 时遇到问题,它在我第一次运行为其创建的 draw() 函数时仅绘制一条线。我的代码是这样的:

我有 graphPanel.py: (精简到要点)

class GraphPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self.figure = Figure()
        self.canvas = FigureCanvasWxAgg(self, -1, self.figure)

        self.draw()

    def draw(self, curves={}):       #This creates the axis and
        if not hasattr(self, "ax"):  #plots all the lines in the curve dictionary
            self.ax = self.figure.add_subplot(111)

        self.ax.lines = []

        for name, props in zip(curves.keys(), curves.values()):
            x, y, col = props
            line = self.ax.plot(x, y, color=col, lw=3)

和 Main.py 文件,其中包含嵌入 graphpanel 的整个界面,以及一个应该调用面板的 draw() 函数的函数,给出它是包含一组曲线的字典。 问题是,当我这样做时,它将完全忽略被清空的列表 self.ax.lines 和plot() 调用,并将所有内容保留为之前的样子。

由于某种原因,它仅在我第一次从类的 init 调用它时起作用,因为如果我将一个带有点的字典放在 draw() 调用中init 它将完美地绘制它们。

为什么我再次调用 draw() 时不起作用?

I'm having a problem with matplotlib where it only plots a line the first time i run the draw() function I made for it. My code is like this:

I have graphPanel.py: (stripped to the essentials)

class GraphPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self.figure = Figure()
        self.canvas = FigureCanvasWxAgg(self, -1, self.figure)

        self.draw()

    def draw(self, curves={}):       #This creates the axis and
        if not hasattr(self, "ax"):  #plots all the lines in the curve dictionary
            self.ax = self.figure.add_subplot(111)

        self.ax.lines = []

        for name, props in zip(curves.keys(), curves.values()):
            x, y, col = props
            line = self.ax.plot(x, y, color=col, lw=3)

And the Main.py file which contains the entire interface where the graphpanel is embedded, along with a function that should call the panel's draw() function, giving it a dictionary containing a set of curves.
The problem is, when I do this it will completely ignore the list self.ax.lines being emptied and the plot() call and leave everything as it was before.

For some reason it only works the first time i call it, from the init of the class, because if i put a dictionary with points in the draw() call placed in init it will plot them perfectly.

Why doesn't it work if i call draw() another time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬 2024-09-06 20:30:51

在你自己的draw()方法结束时,你应该调用

self.canvas.draw()

告诉画布它现在必须更新屏幕。否则它会在下一个抽奖事件中这样做(这是由你提到的可怕的黑客造成的)。

At the end of you own draw() method you should call

self.canvas.draw()

to tell the canvas it has to update the screen right now. Otherwise it will do so on the next draw event (which you cause by that terrible hack you mentioned).

许你一世情深 2024-09-06 20:30:51

对于有同样问题的人,

我用一点“黑客”解决了它,

我在主窗口中创建了一个函数,该函数销毁图形面板,删除其值(del graphpanel),然后创建一个新的,向其发送带有曲线数据的字典也。

然后我“摇动”窗口(我制作了另一个函数,将其大小增加 1px,然后将其恢复正常,以强制执行 evt_size 事件)以使 graphpanel 调整大小(否则它将是一个小正方形)窗口顶部。

For people with my same problem

I solved it with a little "hack"

I made a function in the main window that destroys the graphpanel, deletes its value (del graphpanel) and then creates a new one, sending it a dictionary with the curve data too.

Then I "shake" the windows (I've made another function that increases its size by 1px and then takes it back to normal, to force an evt_size event) to make the graphpanel resize (or else it would be a tiny square at the top of the window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文