Snippet:
ax = Axes3D(self.fig)
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = self.prop * np.outer(np.cos(u), np.sin(v))
y = self.prop * np.outer(np.sin(u), np.sin(v))
z = self.prop * np.outer(np.ones(np.size(u)), np.cos(v))
t = ax.plot_surface(x, y, z, rstride=6, cstride=6,color='lightgreen',linewidth=0)
self.canvas.draw()
The above snippet graphs a sphere in tkinter using matplotlib. I’ve found that higher rstride and cstride values allow the graph to have a bit better performance. However, they give the sphere a weird ribbed shape. I was wondering what other things could be adjusted in the above code to help improve performance.
Really, the problem is more within
plot_surface. There are a lot of things that can be done to improve it. For instance, the shading takes a lot of time and just by changing one line:to
within one of the functions used by
plot_surface, I got a reduction of about 28% in the overall runtime. Why? Thenormfunction (well, class kind of) is set-up for vectorization, but wasn’t being used in that manner. I know that there are many such things within these functions that aren’t very optimal. Changing the two lines:to
in the
plot_surfacefunction itself gives another substantial improvement – now we’re down 33% from the original runtime.From what I’ve seen, the code isn’t really written for efficiency so much as to just get it working from what I can tell – there are plenty of places where things which could be made to be more vectorized using Numpy and aren’t. I’m afraid that what really is needed is some optimization of the matplotlib functions.