动画 matplotlib imshow

2023-11-29

让我首先澄清一下,我并不是想生成像中那样的随机游走线this和许多其他问题。我正在尝试制作一个随机游走热图,它会随着点的重新访问而改变颜色,例如this.

I've been able to create still-lifes like this:results of random walk but I want to see the process.

我可以让图形显示出来,如果我在每一步打印数组,我可以看到步行正在工作。但人物本身并没有动画。我的代码:

import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import animation as anim
import numpy as np
import sys
import random

length = int(sys.argv[1])

fig = plt.figure()
ax = plt.axes(xlim=(0, length-1), ylim=(0, length-1))
arr = np.zeros((length, length), dtype = int)

cmap = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',
                                                    ['black','green','white'],
                                                    256)
bounds=[0,0,10,10]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

im=plt.imshow(arr, interpolation='nearest',
              cmap = cmap,
              origin='lower')

x = int(np.random.random_sample() * length)
y = int(np.random.random_sample() * length)

def walk():
    global x, y
        rand = np.random.random_sample()
        if rand < 0.25 :
            if x == length - 1:
                x = 0
            else: x = x + 1
        elif rand < 0.5 :
            if x == 0:
                x = length - 1
            else: x = x - 1
        elif rand < 0.75 :
            if y == length - 1:
                y = 0
            else: y = y + 1
        else:
            if y == 0:
                y = length - 1
            else: y = y - 1
    return

def stand(arr):
    global x,y
    arr[x][y] = arr[x][y] + 1
    return arr

def animate(i):
    arr=im.get_array()
    walk()
    #print(a)
    arr = stand(arr)
    im.set_array(arr)
    return [im]

anim = anim.FuncAnimation(fig, animate, frames=200, interval=20, blit=True)
plt.show()

运行 Python 3.6,如打印所示。

有很多带有这些动画网格的视频,但我找不到任何答案!必须有人知道如何去做。谢谢!


I added animated=True and vmin=0, vmax=255, in the imshow()下面的函数。我也改变了stand()线路至arr[x][y] = arr[x][y] + 10.

#!/usr/bin/env python3
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import animation as anim
import numpy as np
import sys
import random

length = int(sys.argv[1])

fig = plt.figure()
ax = plt.axes(xlim=(0, length-1), ylim=(0, length-1))
arr = np.zeros((length, length), dtype = int)

cmap = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',
                                                    ['black','green','white'],
                                                    256)
bounds=[0,0,10,10]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

im=plt.imshow(arr, interpolation='nearest',
        cmap = cmap, vmin=0, vmax=255,
              origin='lower', animated=True) # small changes here

x = int(np.random.random_sample() * length)
y = int(np.random.random_sample() * length)

def walk():
    global x, y
    rand = np.random.random_sample()
    if rand < 0.25 :
        if x == length - 1:
            x = 0
        else: x = x + 1
    elif rand < 0.5 :
        if x == 0:
            x = length - 1
        else: x = x - 1
    elif rand < 0.75 :
        if y == length - 1:
            y = 0
        else: y = y + 1
    else:
        if y == 0:
            y = length - 1
        else: y = y - 1
    return

def stand(arr):
    global x,y
    arr[x][y] = arr[x][y] + 1000
    return arr

def animate(i):
    global x,y
    arr=im.get_array()
    walk()
    #print(a)
    arr = stand(arr)
    im.set_array(arr)
    return [im]

anim = anim.FuncAnimation(fig, animate, frames=200, interval=20, blit=True)
plt.show()

我运行它length = 50我得到一个动画。看见here。因此,您可能需要稍微调整一下颜色选择。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

动画 matplotlib imshow 的相关文章

随机推荐

  • 使用 SQL 高效插入大量数据

    您好 我经常需要将大量数据插入表中 例如 我将从 Excel 或文本文件中获取以下形式的数据 1 a 3 bsdf 4 sdkfj 5 something 129 else 然后我经常在这个例子中构造6条插入语句并运行SQL脚本 我发现当我
  • 如何以编程方式设置网格行和列位置

    我在 Stackpanel 中有两个网格 第一个网格被命名为 GridX 最初 在网格内部 有一个文本框的二维数组 RowDefs ColumnDefs XAML 中的 TextBox 定义是
  • 从聚合迭代 Mongodb 游标

    这是我的 node js 后端的代码 app get getpossibleconnections auth function req res if req authenticated false res send Your session
  • java.lang.NoClassDefFoundError:无法解决

    我在android studio上安装了jrebel for android 启动时出现这个错误 这是我的配置 我的jdk版本 jdk1 8 0 91 编译SDK版本24 buildTools版本 25 0 0 类路径 com androi
  • 作为参数传递给模块函数时,Scriptblock 未获得管道变量绑定

    我想把这个功能 function Test Any CmdletBinding param EvaluateCondition Parameter ValueFromPipeline true ObjectToTest begin any
  • 如何选择shell输出的最后一行

    你好 我有一个像这样的 shell 命令 s3 awk BEGIN print S3 bucket path Executing command queryId sub queryId space q 0 s3 print 10 OFS h
  • Java 中的就地快速排序

    为了刷新一些 Java 我尝试实现一个可以对整数数组进行排序的快速排序 就地 算法 以下是我到目前为止得到的代码 你可以通过以下方式调用它sort a 0 a length 1 如果两个 指针 都存在 则此代码显然会失败 进入无限循环 i
  • 如何在 R 中创建自累积向量

    我觉得这个很简单 但是我的R功夫很弱 我正在尝试以累积的方式创建其自身的向量 这段代码可以工作 但我想要更优雅和自动化的东西 我有数百万行需要累积 a lt c 4 4 5 1 9 a lt a order a k lt a 1 lengt
  • Tessnet2 Init-Method 在某些 tessdata 路径下崩溃

    我正在使用 Tessnet2 程序集 它使用 Tesseract 来进行 OCR 不幸的是 在我调用 init 方法后 程序崩溃了 没有任何异常 tessnet2 Tesseract ocr new tessnet2 Tesseract o
  • SQL 挑战/难题:如何合并嵌套范围?

    此挑战基于涉及 IP 范围的现实生活用例 我带来的解决方案基于堆栈跟踪我之前提出过的挑战 每个范围开始都被视为PUSH操作 每个范围结束 1 被视为POP手术 挑战 我们有一个范围数据集 其中每个范围都有起点 终点和值 create tab
  • XSLT - 在输出中用转义文本替换撇号

    我正在编写一个 XSLT 模板 需要为 xml 站点地图输出有效的 xml 文件
  • WPF 图像控件中的初始图像

    我的项目中有一个从互联网加载的 WPF 图像控件 延迟加载 我想在图像控件中显示初始图像 直到主图像加载 请帮助我
  • 为什么 Python 不会因切片越界而抛出错误? [复制]

    这个问题在这里已经有答案了 MATLAB 为此抛出错误 gt gt a 2 3 4 gt gt a 3 4 index out of bounds 如果用 Python 尝试类似的事情 为什么它不是非法的 gt gt gt a 2 3 4
  • 区分“没有行受到影响”和行成功更新为相同值(MySQL 和 PHP)

    我正在从 PHP 执行 SQL MySQL 命令 每次执行有几种可能的结果 记录更新为新值 记录已更新 但值恰好相同 记录找不到要更新的行 即没有行与WHERE clause 我想知道如何区分 的 1 和 3 两种情况都返回零作为受影响的行
  • 为什么在请求中使用 Cache-Control 标头?

    这一页 on Cache Control指定以下内容 可以使用的标准缓存控制指令由客户在一个 HTTP 请求 我认为只有服务器会发回有关客户端是否应该缓存响应的信息 为什么客户端要向服务器发送缓存信息 客户端和服务器之间可以有任意数量的中间
  • TCL:Windows 中线程之间的双向通信

    我需要在 Tcl 中的线程之间进行两种方式的通信 而我所能得到的只是一种将参数作为我唯一的 master gt helper 通信通道传入的方式 这是我所拥有的 proc ExecProgram command if catch open
  • 如何将数据模板分配给文本框wpf

    文本框应该显示某些访问权限的隐藏美元金额 我创建了一个转换器类 继承自 IValueConverter 来通过实现转换方法来处理屏蔽 public object Convert object value Type targetType ob
  • 如何将 Memory Sanitizer 与 GCC 一起使用?

    我想在 gcc 中使用这种消毒剂 我怎样才能做到这一点 这样的手术可以吗 我找到了 clang 的解决方案 clang fsanitize memory fno omit frame pointer g O2 umr cc但我不知道如何在
  • 如何更新 RestTemplate 以正确映射 Java 日期?

    我有一个问题 我的RestTemplate postForEntity url restRequest RepoResponse class 调用失败 因为它无法反序列化以下形式的日期 2019 02 01T12 00 00 000 050
  • 动画 matplotlib imshow

    让我首先澄清一下 我并不是想生成像中那样的随机游走线this和许多其他问题 我正在尝试制作一个随机游走热图 它会随着点的重新访问而改变颜色 例如this I ve been able to create still lifes like t