简体   繁体   中英

Trying to update a 3D graphs coordinates with matplotlib

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM