我希望我的函数返回一个< class 'matplotlib.image.AxesImage' >
显示后两类型的对象< class 'matplotlib.image.AxesImage' >
有imshow
:
def merge_class(array1,array2):
plt.imshow(array1)
plt.imshow(array2)
return ?
我怎么做 ?
我听说可以保存整个图形,然后在程序中调用该文件,但我想避免这种情况。
我还看到可以合并这些对象,但不了解它们的作用:Python:如何“合并”两个类
编辑1:我想知道,因为我正在制作动画,一帧有两层:一张图片和一个数组上方。
movie.append([plt.imshow(merge_class(array1,array2), animated=True, interpolation='none', origin='lower')])
电影列表中的对象必须为< class 'matplotlib.image.AxesImage' >
,如果我仅plt.show()
在函数中返回,它将返回None
。
编辑2:我正在模拟一场森林大火。
在数组forest
和forest_fire
:
该set_on_fire
函数返回一个新的森林,其中坐标(i,j)处的树着火。
check_fire
如果至少一棵不燃烧的树可以燃烧,则该函数返回True。
该spreading_fire
函数返回一个新的森林,其中可以燃烧的树木着火了。
这是代码的一部分:
# maido is the name of a mountain and a forest
def img_maido(img_file,forest):
fig, axes = plt.subplots()
# 1) Opening the picture as an array
img_array = plt.imread(img_file)
img = np.copy(img_array[::-1,:,:]) # I flip it because it is upside down
# 2) Hiding all the 'no tree' values (0.0)
forest = np.ma.masked_where(forest == 0.0, forest) # The array is transparent at each one of the 'no tree' values position (0.0)
# 3) The 'non-burning tree' values (1.0) are green and the 'burning tree' values (2.0) are red
cmap = ListedColormap(['green','red'], 'indexed')
# 4) Displaying the array 'img' of the mountain and the array 'forest' of the forest above it
plt.imshow(forest,zorder=1, cmap=cmap, origin='lower')
plt.imshow(img,zorder=0, origin='lower')
return ? # Here is my issue
def fire_animation(img_file,forest,i,j,wind):
fig, axes = plt.subplots()
movie = []
# 1) Initialization
forest_fire = set_on_fire(forest,i,j) # I put a 'burning tree' value (2.0) in the array 'forest' at the coordinates (i,j)
movie.append([plt.imshow(img_maido(img_file,forest_fire), animated=True, cmap=cmap, interpolation='none', origin='lower')])
plt.draw()
# 2) Spread of fire
while check_fire(foret,wind):
forest_fire = spreading_fire(forest_fire,wind)
movie.append([plt.imshow(img_maido(img_file,forest_fire), animated=True, interpolation='none', origin='lower')])
plt.draw()
# 3) Animation
ani = animation.ArtistAnimation(fig, movie, interval=100, blit=True, repeat_delay=100)
plt.draw()
plt.show()
在这种情况下,似乎没有理由使用数组来附加艺术家。您可以简单地使用一个为每次迭代更改数组的函数。
在下面的示例中,我们创建了一个图形和一个轴,并将两个数组绘制到它们上。每个图像都保存在一个变量中。
然后,我们创建一个动画,该动画反复调用一个函数burn
。在此函数内部,我们操纵一个数组并将新数据设置为其中一个图像,而另一个保持不变。然后,更改后的图像就是该函数的返回值。注意,,
之后的返回值。该逗号使返回值成为一个序列,这意味着,如果需要,我们也可以更改两个图像并返回两个图像。但是,这不是必须的,因为在我们的情况下背景不会改变。
import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np
x = np.linspace(0,40)
X,Y = np.meshgrid(x,x)
static_array = (X/40.)**2+(Y/30.)**2
dynamic_array = np.floor(np.random.random(X.shape)*1.1)
masked_dynamic_array = np.ma.masked_where(dynamic_array <=0.7 , dynamic_array)
fig, ax = plt.subplots()
static_image = ax.imshow(static_array, cmap="terrain")
dynamic_image = ax.imshow(dynamic_array, cmap="magma")
def burn(i):
rand = (np.random.random(X.shape)-0.3)
new = dynamic_array + 0.1*rand
dynamic_array[:,:] = new/new.max()
masked_dynamic_array = np.ma.masked_where(dynamic_array <=0.7 , dynamic_array)
dynamic_image.set_data(masked_dynamic_array)
return dynamic_image,
ani = matplotlib.animation.FuncAnimation(fig, burn, interval=100, blit=True)
plt.show()
import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np
x = np.linspace(0,40)
X,Y = np.meshgrid(x,x)
static_array = (X/40.)**2+(Y/30.)**2
dynamic_array = np.floor(np.random.random(X.shape)*1.1)
fig, ax = plt.subplots(figsize=(4,4))
static_image = ax.imshow(static_array, cmap="terrain")
dynamic_image = ax.imshow(dynamic_array, cmap="magma", vmin=0., vmax=4.)
ims = []
while dynamic_array.mean() < 1.5:
rand = (np.random.random(X.shape)-0.4)
new = dynamic_array + 0.085*rand
new[new > 4] = 4.
dynamic_array[:,:] = new
masked_dynamic_array = np.ma.masked_where(dynamic_array <=0.7 , dynamic_array)
ims.append(masked_dynamic_array)
def burn(i):
dynamic_image.set_data(ims[i])
return dynamic_image,
plt.tight_layout()
ani = matplotlib.animation.FuncAnimation(fig, burn, frames=len(ims), interval=100, blit=True)
ani.save(__file__+'.gif', writer='imagemagick', fps=10)
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句