2D的简单动画坐标使用matplotlib和pyplot [英] Simple animation of 2D coordinates using matplotlib and pyplot
问题描述
我是新来matplotlib。我有我的蟒蛇更新和想用matplotlib的pyplot动画x-y坐标列表。我想预先指定的X范围和Y范围。下面是我目前的code:
I am new to matplotlib. I have a list of x-y coordinates that I update in python and want to animate using matplotlib's pyplot. I want to specify the x-range and y-range in advance. Below is my current code:
import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[5,6,7,8]
for t in range(100):
#lists x and y get updated here
#...
plt.plot(x, y, marker='o', linestyle='None')
plt.show()
正如你所看到的,我用 plt.plot()
和 plt.show()
的在我的迭代循环的结束的仅绘制最终坐标。但是我想把这个步骤的 的内循环和情节在每次迭代与指定的暂停时间,让我有一个动画作为循环运行。
As you can see, I use plt.plot()
and plt.show()
at the end of my iteration loop to plot only the final coordinates. But I want to put this step inside the loop and plot at every iteration with a specified pause time so that I have an animation as the loop runs.
只是移动该声明内循环或调整其周围不工作。
我想保持它很简单,虽然,不希望使用 matplotlib.animation
。有没有使用更多的模块和库的一些简单的方法(只喜欢的东西plt.pause()
,更或许只是一个位),这将让我做我想要什么?
Just moving that statement inside the loop or tweaks thereabout aren't working.
I want to keep it very simple though, and don't want to use matplotlib.animation
. Is there some simple method without using many more modules and libraries (only stuff like plt.pause()
and perhaps only a bit more) that will let me do what I want?
我看了看网上很多地方,我面对大多数方法问题存在,我在Windows上使用python(X,Y)(这是Python版本2.7)这一点,并使用过于复杂的模块和库动画在这里崩溃。
I looked at many places online, and the problem I face with most methods there is that I am using python(x,y) (that's python version 2.7) on Windows for this, and animations using too complicated modules and libraries are crashing here.
不过,我能够运行像这个例子上的matplotlib网站,这是接近我想要的,但并不完全。因此,也许最好的事情将本实施例的变形例,对于我的二维数据的情况下(即实施例为一维行)的工作原理。但是,任何其他建议是值得欢迎的。
However, I am able to run simple stuff like this example on the matplotlib site, which is close to what I want, but not quite. So perhaps the best thing will be a modification of this example that works for my case of 2D data (that example is for a 1D row). But any other suggestion is welcome.
推荐答案
这是从演示动画改编一>:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
for t in range(10):
if t == 0:
points, = ax.plot(x, y, marker='o', linestyle='None')
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
else:
new_x = np.random.randint(10, size=5)
new_y = np.random.randint(10, size=5)
points.set_data(new_x, new_y)
plt.pause(0.5)
虽然这是简单的文档字符串说,它是缓慢的。
While this is simple the docstring say that it is slow.
这篇关于2D的简单动画坐标使用matplotlib和pyplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!