matplotlib正在返回空图

发布于 2025-02-05 04:18:50 字数 659 浏览 1 评论 0原文

单击此处以获取图像

试图在python中绘制动画线图。为什么此代码返回空白的白色图?指导将不胜感激。而且,如果有一种更好的方法可以在Python绘制动画线图,请建议您。

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

x_data=[]
y_data =[]
fig,ax = plt.subplots()
ax.set_xlim(0,100)
ax.set_ylim(0,12)
line, = ax.plot(0,0)

def update(i):
    x_data.append(i*10)
    y_data.append(i)

    line.set_xdata(x_data)
    line.set_ydata(y_data)
    return line,

animation = FuncAnimation(fig,func = update, frames = np.arange(0,10,0.01), interval =200)

plt.show()

Click Here for the image

trying to plot an animated line chart in python. Why is this code returning to a blank white plot ? a guidance would be appreciated. And also if there is a better way to draw an animated line chart in Python, please suggest.Thank you.

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

x_data=[]
y_data =[]
fig,ax = plt.subplots()
ax.set_xlim(0,100)
ax.set_ylim(0,12)
line, = ax.plot(0,0)

def update(i):
    x_data.append(i*10)
    y_data.append(i)

    line.set_xdata(x_data)
    line.set_ydata(y_data)
    return line,

animation = FuncAnimation(fig,func = update, frames = np.arange(0,10,0.01), interval =200)

plt.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转瞬即逝 2025-02-12 04:18:50

该代码对我有用,但是非常慢,因为您以200ms的间隔添加了1000帧,因此完整的动画需要200秒才能完成。

您需要顶部的导入(以及安装的适当库),

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

x_data = []
y_data = []
fig, ax = plt.subplots()
ax.set_xlim(0, 100)
ax.set_ylim(0, 12)
line, = ax.plot(0, 0)

def update(i):
    x_data.append(i*10)
    y_data.append(i)

    line.set_xdata(x_data)
    line.set_ydata(y_data)
    return line,


animation = FuncAnimation(fig,func = update, frames = np.arange(0, 10, 0.01), interval = 2)

plt.show()

我将上述代码中的间隔设置为2ms,以显示更快的动画。

The code works for me, but is very slow because you have added 1000 frames at 200ms intervals, so the full animation takes 200 seconds to complete.

You need the imports at the top (and the appropriate libraries installed)

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

x_data = []
y_data = []
fig, ax = plt.subplots()
ax.set_xlim(0, 100)
ax.set_ylim(0, 12)
line, = ax.plot(0, 0)

def update(i):
    x_data.append(i*10)
    y_data.append(i)

    line.set_xdata(x_data)
    line.set_ydata(y_data)
    return line,


animation = FuncAnimation(fig,func = update, frames = np.arange(0, 10, 0.01), interval = 2)

plt.show()

I have set the interval to 2ms in the above code to show a faster animation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文