Python 如何使用子地块中的图像生成动画(matplotlib)
可以在jupyter笔记本中设置成对图像的动画吗 有两个图像列表:Python 如何使用子地块中的图像生成动画(matplotlib),python,matplotlib,jupyter,subplot,Python,Matplotlib,Jupyter,Subplot,可以在jupyter笔记本中设置成对图像的动画吗 有两个图像列表: greys = io.imread_collection(path_greys) grdTru= io.imread_collection(path_grdTru) 以下幼稚代码无法生成动画: for idx in range(1,900): plt.subplot(121) plt.imshow(greys[idx], interpolation='nearest', cmap=plt.cm.gray)
greys = io.imread_collection(path_greys)
grdTru= io.imread_collection(path_grdTru)
以下幼稚代码无法生成动画:
for idx in range(1,900):
plt.subplot(121)
plt.imshow(greys[idx], interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(122)
plt.imshow(grdTru[idx], interpolation='nearest', cmap=plt.cm.,vmin=0,vmax=3)
plt.show()
(它生成子批次列表)
顺便说一下,如果将文档粘贴到笔记本中,文档将失败。为了在jupyter笔记本中工作,您需要包括
%matplotlib notebook
魔法命令
import numpy as np
%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
def f(x, y):
return np.sin(x) + np.cos(y)
x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
im = plt.imshow(f(x, y), animated=True)
def updatefig(*args):
global x, y
x += np.pi / 15.
y += np.pi / 20.
im.set_array(f(x, y))
return im,
ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
plt.show()
然后,您可以轻松地将其调整为图像列表
从matplotlib 2.1版开始,您还可以选择创建JavaScript动画内联
from IPython.display import HTML
HTML(ani.to_jshtml())
完整示例:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def f(x, y):
return np.sin(x) + np.cos(y)
x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
im = plt.imshow(f(x, y), animated=True);
def updatefig(*args):
global x, y
x += np.pi / 15.
y += np.pi / 20.
im.set_array(f(x, y))
return im,
ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
from IPython.display import HTML
HTML(ani.to_jshtml())
为了在jupyter笔记本中工作,您需要包括
%matplotlib notebook
魔法命令
import numpy as np
%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
def f(x, y):
return np.sin(x) + np.cos(y)
x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
im = plt.imshow(f(x, y), animated=True)
def updatefig(*args):
global x, y
x += np.pi / 15.
y += np.pi / 20.
im.set_array(f(x, y))
return im,
ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
plt.show()
然后,您可以轻松地将其调整为图像列表
从matplotlib 2.1版开始,您还可以选择创建JavaScript动画内联
from IPython.display import HTML
HTML(ani.to_jshtml())
完整示例:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def f(x, y):
return np.sin(x) + np.cos(y)
x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
im = plt.imshow(f(x, y), animated=True);
def updatefig(*args):
global x, y
x += np.pi / 15.
y += np.pi / 20.
im.set_array(f(x, y))
return im,
ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
from IPython.display import HTML
HTML(ani.to_jshtml())
OP询问如何使用多个子批次进行此操作。OP询问如何使用多个子批次进行此操作。