尝试使用 matplotlib 更新 3D 图形坐标

发布于 2024-10-02 14:50:29 字数 626 浏览 2 评论 0原文

我有一个函数可以在 tkinter 中使用 matplotlib 绘制 3D 球体图。然而,每次连续调用该函数时,绕球体运行的性能都会下降。此外,该图仅在我尝试绕球体运行后更新。

self.A 是一个调整球体大小的变量。

我的功能:

def draw_fig(self):

        self.ax = Axes3D(self.fig)

        u = numpy.linspace(0, 2 * numpy.pi, 100)
        v = numpy.linspace(0, numpy.pi, 100)
        x = self.A * numpy.outer(numpy.cos(u), numpy.sin(v))
        y = self.A * numpy.outer(numpy.sin(u), numpy.sin(v))
        z = self.A * numpy.outer(numpy.ones(numpy.size(u)), numpy.cos(v))

        t = self.ax.plot_surface(x, y, z,  rstride=4, cstride=4,color='lightblue',linewidth=0)

I have a function that will graph a 3D sphere with matplotlib in tkinter. However, every successive time I call the function the performance when orbiting the sphere drops. Also the graph only updates after I try to orbit the sphere.

self.A is a variable that adjusts the size of the sphere.

My function:

def draw_fig(self):

        self.ax = Axes3D(self.fig)

        u = numpy.linspace(0, 2 * numpy.pi, 100)
        v = numpy.linspace(0, numpy.pi, 100)
        x = self.A * numpy.outer(numpy.cos(u), numpy.sin(v))
        y = self.A * numpy.outer(numpy.sin(u), numpy.sin(v))
        z = self.A * numpy.outer(numpy.ones(numpy.size(u)), numpy.cos(v))

        t = self.ax.plot_surface(x, y, z,  rstride=4, cstride=4,color='lightblue',linewidth=0)

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

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

发布评论

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

评论(1

很酷又爱笑 2024-10-09 14:50:29

您不应该每次都重新生成所有数据,而只需修改现有数据即可。

编辑:只需移出调用draw_fig 轴构建代码

def __init__...
     u = numpy.linspace(0, 2 * numpy.pi, 100)
     v = numpy.linspace(0, numpy.pi, 100)
     self.x = A * numpy.outer(numpy.cos(u), numpy.sin(v))
     self.y = A * numpy.outer(numpy.sin(u), numpy.sin(v))
     self.z = A * numpy.outer(numpy.ones(numpy.size(u)), numpy.cos(v))
     self.ax = Axes3D(self.fig)

def draw_fig(self):

        t = self.ax.plot_surface(self.x, self.y, self.z,  rstride=4, cstride=4,color='lightblue',linewidth=0)

You should not regenerate each time all the data, but just modify your existing one.

Edit: Just move out of the calling draw_fig the axes building code

def __init__...
     u = numpy.linspace(0, 2 * numpy.pi, 100)
     v = numpy.linspace(0, numpy.pi, 100)
     self.x = A * numpy.outer(numpy.cos(u), numpy.sin(v))
     self.y = A * numpy.outer(numpy.sin(u), numpy.sin(v))
     self.z = A * numpy.outer(numpy.ones(numpy.size(u)), numpy.cos(v))
     self.ax = Axes3D(self.fig)

def draw_fig(self):

        t = self.ax.plot_surface(self.x, self.y, self.z,  rstride=4, cstride=4,color='lightblue',linewidth=0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文