标签: matplotlib-animation

将动画 Matplotlib 图形保存为 GIF 文件会产生不同的绘图

我使用FuncAnimationMatplotlib Animation 类创建了一个动画绘图,我想将其保存为 .gif 文件。当我运行脚本时,输出看起来正常,如下所示(动画工作正常):

在此输入图像描述

但是,当我尝试使用 ImageMagick 或 PillowWriter 将动画图另存为 .gif 文件时,该图如下图所示:

在此输入图像描述

线条明显粗了很多,总的来说,看起来很糟糕。该问题归因于点(紫色和红色圆圈)。因此,似乎情节是在每一帧上书写的(我认为是这样)。我可以通过将它们全部删除来避免这种情况。但我不想这样做,因为很难看清线条。

这是代码:

line, = ax.plot([], [], color = 'blue', lw=1)
line2, = ax.plot([], [], color = 'red', lw=1)
line3, = ax.plot([], [], color = 'purple', lw=1)
def animate(i):
    line.set_data(x1[:i], y1[:i])
    line2.set_data(x2[:i], y2[:i])
    line3.set_data(x3[:i], y3[:i])
    point1, = ax.plot(x1[i], y1[i], marker='.', color='blue')
    point2, = ax.plot(x2[i], y2[i], marker='.', color='red')
    point3, = ax.plot(x3[i], y3[i], marker='.', color='purple')
    return line, line2, line3, point1, point2, point3,
        
ani = animation.FuncAnimation(fig, animate, interval=20, blit=True, repeat=False, …
Run Code Online (Sandbox Code Playgroud)

python matplotlib animated-gif matplotlib-animation

12
推荐指数
1
解决办法
2万
查看次数

在jupyter中禁止显示动画的最后一帧

我正在开发一个项目,涉及生成用于帧的matplotlib动画。pyplot.imshow我正在jupyter笔记本上做这个。我已经设法让它工作,但还剩下一个恼人的错误(或功能?)。创建动画后,Jupyter在输出单元中显示动画的最后一帧。我希望输出包含捕获为 html 的动画,但不包含最终帧。这是一个简单的例子:

import numpy as np
from matplotlib import animation
from IPython.display import HTML

grid = np.zeros((10,10),dtype=int)
fig1 = plt.figure(figsize=(8,8))

ax1 = fig1.add_subplot(1,1,1)
def animate(i):
    grid[i,i]=1
    ax1.imshow(grid)
    return 
ani = animation.FuncAnimation(fig1, animate,frames=10);

html = HTML(ani.to_jshtml())
display(html)
Run Code Online (Sandbox Code Playgroud)

我可以使用capture magic,但这会抑制一切。这没问题,但我的最终目标是通过活页夹将其公开,并使其尽可能简单地供学生使用。

我在网上看到过matplotlib动画似乎没有这个问题,但那些使用的是情节,而不是imshow,这可能是一个问题。

python matplotlib jupyter-notebook matplotlib-animation

3
推荐指数
1
解决办法
1069
查看次数

表面动画并使用 matplotlib 保存

我尝试做一个非常简单的动画并用 matplotlib 保存它,但没有成功。例如,我想看到一些振荡的东西:这是我能做的最好的事情

import numpy as np
import matplotlib.pyplot as plt    
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation

#Define x,y vectors and meshgrid with function u on it

x = np.arange(0,10,0.1)
y = np.arange(0,10,0.1)
X,Y = np.meshgrid(x,y)
u = np.sin(X + Y)

#Create a figure and an axis object for the surface
#(which by the way is not initialized, because I don't know were to)

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')

#Define a kind-of animation function, imitating what I …
Run Code Online (Sandbox Code Playgroud)

python math animation matplotlib matplotlib-animation

3
推荐指数
2
解决办法
1853
查看次数

如何对 matplotlib 中的二维数组数据使用 line.set_data ?

我正在尝试在 matplotlib 中同时为多行设置动画。为此,我遵循 matplotlib.animation 文档中的教程:

https://matplotlib.org/stable/api/animation_api.html

本教程的想法是创建一条线ln, = plt.plot([], [])并更新该线的数据ln.set_data以生成动画。虽然当线数据是 n 个数据点的一维数组 (shape = (n,)) 时,这一切都工作正常,但当线数据是 n 个数据点的 2 维数组 (shape = (n,k)) 时,我遇到了麻烦要绘制的 k 条线。

更准确地说,plt.plot接受数组作为输入,每列对应于要绘制的新线。下面是一个简单的示例,通过一次plt.plot调用绘制了 3 条线:

import matplotlib.pyplot as plt
import numpy as np


x = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
x = np.concatenate([x] * 3, axis=1)

# generate 3 curves
y = np.copy(x)
y[:, 0] = np.cos(y[:, 0])
y[:, 1] = np.sin(y[:, 1] )
y[:, 2] = …
Run Code Online (Sandbox Code Playgroud)

python numpy matplotlib matplotlib-animation

2
推荐指数
2
解决办法
2万
查看次数

如何更新散点图动画

我尝试编写一个简单的脚本,它更新每个时间步的散点图t。我想做得尽可能简单。但它所做的只是打开一扇我什么也看不见的窗户。窗户就结冰了。这可能只是一个小错误,但我找不到它。

data.dat格式为

                x      y
Timestep 1      1      2
                3      1
Timestep 2      6      3
                2      1
Run Code Online (Sandbox Code Playgroud)

(该文件仅包含数字)

import numpy as np
import matplotlib.pyplot as plt
import time

# Load particle positioins
with open('//home//user//data.dat', 'r') as fp:
    particles = []
    for line in fp:
        line = line.split() 
        if line:
            line = [float(i) for i in line]
            particles.append(line)

T = 100
numbParticles = 2

x, y = np.array([]), np.array([])

plt.ion()
plt.figure()
plt.scatter(x,y)
for t in range(T):
    plt.clf()
    for …
Run Code Online (Sandbox Code Playgroud)

python matplotlib scatter-plot matplotlib-animation

1
推荐指数
1
解决办法
9324
查看次数

如何从 for 循环内对 matplotlib 绘图进行动画处理

我想用 for 循环的每次迭代中计算的值更新我的 matplotlibplot 。我的想法是,我可以实时查看计算了哪些值,并在脚本运行时逐次观察进度迭代。我不想首先迭代循环,存储值,然后执行绘图。

一些示例代码在这里:

from itertools import count
import random

from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt


def animate(i, x_vals, y_vals):
    plt.cla()
    plt.plot(x_vals, y_vals)

if __name__ == "__main__":
    x_vals = []
    y_vals = []
    fig = plt.figure()
    index = count()

    for i in range(10):
        print(i)
        x_vals.append(next(index))
        y_vals.append(random.randint(0, 10))
        ani = FuncAnimation(fig, animate, fargs=(x_vals, y_vals))
        plt.show()
Run Code Online (Sandbox Code Playgroud)

我在网上看到的大多数示例都处理动画的所有内容都是全局变量的情况,我想避免这种情况。当我使用调试器逐行单步执行代码时,该图形确实出现并且是动画的。当我在没有调试器的情况下运行脚本时,会显示图形,但没有绘制任何内容,并且我可以看到我的循环没有超过第一次迭代,首先等待图形窗口关闭,然后继续。

python matplotlib matplotlib-animation

0
推荐指数
1
解决办法
8868
查看次数