matplotlib动画看起来与序列的imshow不同

时间:2019-08-12 15:52:37

标签: python numpy matplotlib

我有两个代码试图将其转换为动画,动画代码和类显示不完整的图像(缺少颜色),底部的函数显示完整的图像,我看不出区别在哪里在代码中是。我感觉它与matplolib API有关,但是我不确定...

filter {
  name   = "name"
  values = ["server-name-*", "!'${data.aws_ami.ami_latest.name}'"]
}

说明:

  1. 按原样运行一次,然后关闭绘图窗口,新的窗口将以更清晰的图像和正确的颜色出现。
  2. 注释掉在from urllib.request import urlopen from matplotlib import animation, image, pyplot as plt import numpy as np fig, ax = plt.subplots() ax.axis("off") class SVDCompress(): """compresses an image using singular value deocomposition""" def __init__(self): self.img = image.imread( urlopen('https://www.mariowiki.com/images/thumb/e/e1/Small_Mario_Cut-in_PD-SMBE.png/110px-Small_Mario_Cut-in_PD-SMBE.png') ) #self.img = image.imread(filename) self.svd_image = np.zeros(self.img.shape) self.bytes = 0 self.total_bytes = self.img.shape[0] * self.img.shape[1] * self.img.shape[2] / 1000 self.layers = [] # svd all the layers once so we don't have to do it again later for layer in range(self.img.shape[2]): self.layers.append(np.linalg.svd(self.img[:, :, layer])) def frames(self): """yield the frame number which gets passed to layers method""" for col in range(self.img.shape[1]): yield col def layer_no_animation(self, col: "animation index"): """layers calling plt.show() instead of figure""" for i in range(self.img.shape[2]): U, S, V = self.layers[i][0], self.layers[i][1], self.layers[i][2] self.bytes += (len(U[:, col]) + len(V[col, :])) / 1000 self.svd_image[:, :, i] += np.outer(U[:, col] * S[col], V[col, :]) if col % 30 == 0: plt.imshow(self.svd_image) plt.show() def layer(self, col: "animate index"): """add the new layers to the image""" for i in range(self.img.shape[2]): U, S, V = self.layers[i][0], self.layers[i][1], self.layers[i][2] self.bytes += (len(U[:, col]) + len(V[col, :])) / 1000 self.svd_image[:, :, i] += np.outer(U[:, col] * S[col], V[col, :]) if col % 30 == 0: ax.set_title(f"SVD column: {col} kbytes: {self.bytes}/{self.total_bytes}") ax.imshow(self.svd_image) if __name__ == "__main__": #svd = SVDCompress() #anim = animation.FuncAnimation(fig, svd.layer, frames=svd.frames, interval=100) #anim.save('./mario-compression.gif', writer='imagemagick') svd = SVDCompress() for c in svd.frames(): svd.layer_no_animation(c) 中运行的部分,并注释掉我注释掉的代码。这样可以保存动画,并且即使代码看起来完全一样,颜色也会变得混乱。

我认为调用__name__ == "__main"plt.imshow()时,颜色有所不同,但我不知道它是什么

...当然可能是我想念的其他东西

正在呼叫ax.imshow() ...

enter image description here

呼叫plt.imshow()

enter image description here

0 个答案:

没有答案