python - 如何提高 python 绘图的速度?

标签 python plot matplotlib

我有一个 256x256 的 numpy 数据数组,它在不断变化。在每次迭代中,我都会拍摄快照来制作电影。 snapshot 是使用 matplotlib 制作的 3d 表面图。

问题是每次迭代都花费我 >2 秒的时间,即 250 次迭代大约需要 600 秒。我在 MATLAB 中运行了相同的程序,相同的迭代次数需要 80-120 秒。

问题:是否有加快 matplotlib 3d 表面绘图的方法,或者是否有更快的 python 绘图工具?

部分代码如下:

## initializing plot

fig = plt.figure(111)
fig.clf()
ax = fig.gca(projection='3d')
X = np.arange(0, field_size, 1)
Y = np.arange(0, field_size, 1)
X, Y = np.meshgrid(X, Y)

## the loop

start_time = time.time()
for k in xrange(250):
    it_time = time.time()
    field[128,128] = maxvalue
    field = scipy.ndimage.convolve(field, kernel)
    print k, " calculation: ", time.time() - it_time, " seconds"
    it_time = time.time()
    ax.cla()
    ax.plot_surface(X, Y, field.real, rstride=4, cstride=4, cmap=cm.hot,
        linewidth=0, antialiased=False)
    ax.set_zlim3d(-50, 150)
    filename = "out_%d.png" % k
    fig.savefig(filename)
    #fig.clf()
    print k, " plotting: ", time.time() - it_time, " seconds"
print "computing: ", time.time() - start_time, " seconds"

最佳答案

对于一般的 3D 绘图,我建议 mayavi .一开始可能有点令人生畏,但这是值得的。

绘制一次 3D 数据肯定比 matplotlib 快得多。对于使用 savefig 调用多次绘图,我不确定...

关于python - 如何提高 python 绘图的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7503164/

相关文章:

python - 在 matplotlib 中创建两个完全独立的图并在它们之间来回切换

python - 向程序添加子图会改变直方图的显示方式

python - 更新 matplotlib 图

python - 如何在 Pandas 数据框中组合 AND 和 OR 运算符?

matlab - 如果 y 方向设置为 'reverse',如何修复 Matlab 中矢量注释头的错误对齐

python - 具有特定输出的 Elasticsearch DSL 查询

matlab - 在 Matlab 中绘制圆盘,颜色随角度变化

r - 如何在geom_label中使用angle?

python - 如何跳过标准输入的第一行读取?

Heroku 上的 Python 和 Node.js