使Matplotlib运行得更快

rec*_*gle 5 python tkinter matplotlib

片段:

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()
Run Code Online (Sandbox Code Playgroud)

上面的代码片段使用matplotlib在tkinter中绘制了一个球体.我发现更高的rstridecstride值允许图表具有更好的性能.然而,它们使球体具有奇怪的罗纹形状.我想知道在上面的代码中可以调整哪些其他东西来帮助提高性能.

Jus*_*eel 13

真的,问题更多plot_surface.可以做很多事情来改进它.例如,阴影需要花费大量时间,只需更改一行:

colors = [color * (0.5 + norm(v) * 0.5) for v in shade]
Run Code Online (Sandbox Code Playgroud)

colors = np.outer(0.5+norm(shade)*0.5,color)
Run Code Online (Sandbox Code Playgroud)

在其中一个函数中plot_surface,我在整个运行时中减少了大约28%.为什么?的norm功能(以及,类种)被建立为矢量化,但在这种方式中未被使用.我知道这些功能中有很多这样的东西并不是最优的.改变两行:

for rs in np.arange(0, rows-1, rstride):
    for cs in np.arange(0, cols-1, cstride):
Run Code Online (Sandbox Code Playgroud)

for rs in xrange(0,rows-1,rstride):
    for cs in xrange(0,cols-1,cstride):
Run Code Online (Sandbox Code Playgroud)

plot_surface函数本身提供了另一个实质性的改进 - 现在我们从原始运行时下降了33%.

从我所看到的情况来看,代码并不是为了提高效率而写的,只是为了让它从我能说的方面发挥作用 - 有很多地方可以使用Numpy和aren来更好地进行矢量化.吨.我担心真正需要的是matplotlib函数的一些优化.