python imshow:如何将两个<class'matplotlib.image.AxesImage'>合并为一个?

LoïcPoncin

我希望我的函数返回一个< 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:我正在模拟一场森林大火。

在数组forestforest_fire

  • 无树= 0.0
  • 不燃烧的树= 1.0
  • 燃烧的树= 2.0

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在 Jupyter 笔记本中并排绘制两个 matplotlib.image.AxesImage 对象

来自分类Dev

python在matplotlib.image.AxesImage对象上绘制峰

来自分类Dev

pandas和matplotlib:将两个地块合并为一个图例项

来自分类Dev

python将两个文本文件合并为一个文件

来自分类Dev

当净值不为正时,将两个python字典合并为一个

来自分类Dev

Python依赖项:将两个软件包合并为一个

来自分类Dev

将两个排序的链表合并为python中的一个链表

来自分类Dev

将python的两个while循环合并为一个

来自分类Dev

将python中的两个列表合并为一个

来自分类Dev

Python - 将两个单列列表合并为一个双列列表并打印

来自分类Dev

Python:如何将4个小矩阵合并为一个大矩阵

来自分类Dev

python:将两个数组合并为一个数组

来自分类Dev

将python线程结果合并为一个列表

来自分类Dev

Python将多列合并为一个

来自分类Dev

将多个json合并为一个json python

来自分类Dev

将多个 JSON 合并为一个 (Python)

来自分类Dev

将一个列表中的两个子列表合并为一个子列表python

来自分类Dev

[Python]将两个文本文件合并为一个(一行一行)

来自分类Dev

如何将Python和Kivy合并为一个项目

来自分类Dev

Python:如何将for循环的结果合并为一个输出?

来自分类Dev

如何将多个视频合并为一个视频并使用 Python 设置它们的位置

来自分类Dev

如何将多个 Python 脚本合并为一个脚本?

来自分类Dev

在Python中将多个列表合并为一个列表

来自分类Dev

在Python中将两个文本合并为一个

来自分类Dev

在Python中将两个日期列合并为一个

来自分类Dev

在Python中将两个列表元素合并为一个?

来自分类Dev

Python将一个变量内的多个列表合并为一个列表

来自分类Dev

如何将两个列表合并为python中的列序列?

来自分类Dev

matplotlib - 如何将两个标签添加到一个散点图

Related 相关文章

  1. 1

    在 Jupyter 笔记本中并排绘制两个 matplotlib.image.AxesImage 对象

  2. 2

    python在matplotlib.image.AxesImage对象上绘制峰

  3. 3

    pandas和matplotlib:将两个地块合并为一个图例项

  4. 4

    python将两个文本文件合并为一个文件

  5. 5

    当净值不为正时,将两个python字典合并为一个

  6. 6

    Python依赖项:将两个软件包合并为一个

  7. 7

    将两个排序的链表合并为python中的一个链表

  8. 8

    将python的两个while循环合并为一个

  9. 9

    将python中的两个列表合并为一个

  10. 10

    Python - 将两个单列列表合并为一个双列列表并打印

  11. 11

    Python:如何将4个小矩阵合并为一个大矩阵

  12. 12

    python:将两个数组合并为一个数组

  13. 13

    将python线程结果合并为一个列表

  14. 14

    Python将多列合并为一个

  15. 15

    将多个json合并为一个json python

  16. 16

    将多个 JSON 合并为一个 (Python)

  17. 17

    将一个列表中的两个子列表合并为一个子列表python

  18. 18

    [Python]将两个文本文件合并为一个(一行一行)

  19. 19

    如何将Python和Kivy合并为一个项目

  20. 20

    Python:如何将for循环的结果合并为一个输出?

  21. 21

    如何将多个视频合并为一个视频并使用 Python 设置它们的位置

  22. 22

    如何将多个 Python 脚本合并为一个脚本?

  23. 23

    在Python中将多个列表合并为一个列表

  24. 24

    在Python中将两个文本合并为一个

  25. 25

    在Python中将两个日期列合并为一个

  26. 26

    在Python中将两个列表元素合并为一个?

  27. 27

    Python将一个变量内的多个列表合并为一个列表

  28. 28

    如何将两个列表合并为python中的列序列?

  29. 29

    matplotlib - 如何将两个标签添加到一个散点图

热门标签

归档