首页 > 解决方案 > 提高调整大小和平移 imshow/countourf/pcolormesh 的响应能力?

问题描述

我有以下内容:

import matplotlib.pyplot as plt
import numpy as np

fig, axes = plt.subplots(2, 4)

for ax in axes.ravel():
    ax.imshow(np.random.rand(30, 30))
    
plt.show()

这很简单,但我发现在我的系统上调整图形大小非常慢(1-2 fps)。它只管理 8 个 30 x 30 阵列,所以我有点惊讶。我什至没有使用额外的功能调用和功能,如sharex=True, sharey=True, constrained_layout=True, plt.colorbar()等,但即使禁用所有这些,上面的示例平移和缩放也非常缓慢。

有没有方便的方法来改善这一点?我正在尝试将这样的图形嵌入到 tk GUI 中,并且调整两者组合的大小是完全无法管理的。我使用pcolormeshcountourf得到了类似的结果,并尝试了设置interpolation='nearest'选项。

即使是 blitting 示例在静态位置对我来说也很快刷新,所以我的问题与调整大小更一致,而不是数字的实际内容或数据量(我相信)。

import matplotlib.pyplot as plt
import numpy as np

import matplotlib.animation as animation

def gen(): return(np.random.rand(30, 30))

fig, axes = plt.subplots(2, 4, constrained_layout=True, sharex=True, sharey=True)
images = [ax.imshow(gen()) for ax in axes.ravel()]

def animate(i):
    for im in images:
        im.set_array(gen())
    return(images)

anim = animation.FuncAnimation(fig, animate, interval=10, blit=True) 
plt.show()

标签: pythonmatplotlibimshow

解决方案


推荐阅读