import matplotlib
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#坐标轴显示成中文
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
#提取你想要的文本数据
filename = 'disp_6.out'
X, Y = [], []
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
#选取你想要的文档列数,代码中0表示文本里的第一列
X.append(value[0])
Y.append(value[1])
这是我读取我想要文档数据的代码段落
FuncAnimation只能画已经固定好了的方程动态图
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation #导入负责绘制动画的接口
fig, ax = plt.subplots()
x, y= [], []
line, = plt.plot([], [], '.-',color='red')
nums = 100 #需要的帧数
filename = 'disp_6.out'
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
# 选取你想要的文档列数,代码中0表示文本里的第一列
x.append(value[0])
y.append(value[1])
# 用matplotlib绘制一个图形
def init():
ax.set_xlim(-5, 50)
ax.set_ylim(-10, 10)
return line
def update(i):
if len(x)>=nums: #通过控制帧数来避免不断的绘图
return line
line.set_data(x[i],y[i])
return line
ani = FuncAnimation(fig, update, frames=nums, #nums输入到frames后会使用range(nums)得到一系列step输入到update中去
init_func=init)
plt.show()
这样没办法运行....