Python 3.x 具有多个绘图和for循环的matplotlib动画

Python 3.x 具有多个绘图和for循环的matplotlib动画,python-3.x,matplotlib,Python 3.x,Matplotlib,嘿,我试图让matplotlib.animation在一个图中绘制n个图,就像下面的第一个代码块一样,但是当我运行脚本时,除了没有显示任何图外,所有的图都在运行 import matplotlib.pyplot as plt # Data to be ploted x = [] y = [] x2 = [] y2 = [] for i in range(-9,9): x.append(i) y.append(i**2) x2.append(i) y2.appen

嘿,我试图让matplotlib.animation在一个图中绘制n个图,就像下面的第一个代码块一样,但是当我运行脚本时,除了没有显示任何图外,所有的图都在运行

import matplotlib.pyplot as plt

# Data to be ploted
x = []
y = []
x2 = []
y2 = []
for i in range(-9,9):
    x.append(i)
    y.append(i**2)
    x2.append(i)
    y2.append(i**3)
# plot the data
plt.plot(x,y, label = 'first line')
# plot other data points
plt.plot(x2,y2, label = 'second line')
# add this before plt.show() to add labels to graph
plt.xlabel('X value')
plt.ylabel('Y value')
# add a title to graph
plt.title('interesting graph\nsubtitle')
plt.legend()
plt.show()
以下是使用动画的代码:

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

# better face
style.use('fivethirtyeight')

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def anima(i):
    graph_data = open('a.txt').read()
    lines = graph_data.split('\n')
    dataPoints = []
    for i in lines:
        # ignor empty lines

        if len(i) > 1:
            line = i.split('|') # delimiter is |

            for a in range(len(line)):
                try:
                    dataPoints[a].append(int(line[a]))
                # if there is no dataPoint[a] it gets created
                except:
                    dataPoints.append(int(line[a]))
    # modify axis
    ax1.clear()
    # plot
    for i in range(len(dataPoints)-1):
        ax1.plot(dataPoints[1],dataPoints[i+1])

#where to animate, what to animate, how often to update
ani = animation.FuncAnimation(fig, anima, interval = 1000)
plt.show()
在a.txt中,我有以下内容:

1|56|80|62
2|123|135|55
12|41|12|23
60|12|45|23
12|43|56|54
25|123|23|31
2|213|31|84
61|1|68|54
62|2|87|31
63|4|31|53
64|8|13|13
65|16|51|65
66|32|43|84
80|62|42|15
更新: 我放弃了读取文件,让一个线程函数为我生成值,取而代之的是让所有的东西都在一个绘图中,我让所有的东西都在子绘图中(这个数字很快就会被编辑)。当我使用普通绘图运行代码时,它工作正常,但当我尝试使用动画时。。。它再次显示了图形,但没有绘图。我的问题是显示动画情节

#检查操作系统是否为linux
导入平台
如果str(platform.system()).lower()==str('linux').lower():
#必须在导入任何其他matplotlib之前设置
导入matplotlib
matplotlib.use('TkAgg')
将matplotlib.pyplot作为plt导入
将matplotlib.animation导入为动画
从matplotlib导入样式
从线程导入线程
#改变风格
style.use('fivethirtyeight'))
图=plt.图()
#列出所有数据点,例如:[时间列表],[图表列表]。。。。
数据点=[]
#列表中每个图形的“名称”
图0=[0]
def create_plots():
xs=[]
ys=[]
对于范围(-10,11)内的i:
x=i
y=i**3
追加(x)
y.追加(y)
数据点追加(xs)
数据点追加(ys)
t=线程(目标=创建图)
t、 开始()
def anima(一):
对于范围内的i(len(图_uz)):
图[i]=图添加子图(211+i)
图[i].clear()
图[i]。绘图(0,i+1)
而len(数据点)==0:
打印('.'))
ani=动画。FuncAnimation(图,动画,间隔=1000)
plt.show()
1)您确定调用了
anima(i)
函数吗

2) 为什么要在
anima(i)
中重写变量i,然后在第行中再次重写

    for i in lines:
        # ignor empty lines
1) 您确定调用了
anima(i)
函数吗

2) 为什么要在
anima(i)
中重写变量i,然后在第行中再次重写

    for i in lines:
        # ignor empty lines

我不明白为什么有两种不同的代码。哪一个是你遇到问题的?如果是第二个,显示第一个(反之亦然)的目的是什么?除了,您不应该有一个泛型的
——它应该指定由该代码处理的错误,例如
ValueError
等。这使得调试其他奇怪的错误变得困难。我会使用
进行内联:
int(a)
直接遍历值,而不是使用数字索引(或使用比
a
更具描述性的名称)…我显示第一个代码只是为了显示我希望使用animate获得的结果。感谢您的建议,我将添加ValueError作为添加更具描述性的名称,现在我只是想让一些基本的东西开始工作,当它开始工作时,我将编辑它以便于阅读。我想做的事情(在任何一种情况下)可能吗?我不明白为什么有两种不同的代码。哪一个是你遇到问题的?如果是第二个,显示第一个(反之亦然)的目的是什么?除了
,您不应该有一个泛型的
——它应该指定由该代码处理的错误,例如
ValueError
等。这使得调试其他奇怪的错误变得困难。我会使用
进行内联:
int(a)
直接遍历值,而不是使用数字索引(或使用比
a
更具描述性的名称)…我显示第一个代码只是为了显示我希望使用animate获得的结果。感谢您的建议,我将添加ValueError作为添加更具描述性的名称。现在,我只是想让一些基本的东西开始工作。当它开始工作时,我将编辑它以便于阅读。我想做的(在任何情况下)是可能的吗?是的,我确信它被调用了。我从工作脚本中更改的部分是:对于范围内(len(line))循环和范围内(len(dataPoints)-1)循环,我确信它被调用了。我从工作脚本中更改的部分是:对于范围内(len(line))循环和范围内(len(dataPoints)-1)循环