python Matplotlib 系列教程(九)——如何绘制动态图(类似实时股票图=走势图)

本章我们讨论的是如何绘制实时图表,用到的知识是Matplotlib的动画功能。

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)  

# 这个是style的内置风格 
style.use('fivethirtyeight')

# 多次使用figure函数可以绘制产生多个图,其中,图片号按顺序增加。
# 这里,要注意一个概念当前图和当前坐标。所有绘图操作仅对当前图和当前坐标有效.



# 你可以尝试下面这段代码,就可以理解figure的含义 

# plt.figure(1) # 第一张图  
# plt.subplot(211) # 第一张图中的第一张子图  
# plt.plot([1,2,3])  
# plt.subplot(212) # 第一张图中的第二张子图  
# plt.plot([4,5,6])  
# plt.figure(2) # 第二张图  
# plt.plot([4,5,6]) # 默认创建子图subplot(111)  
# plt.figure(1) # 切换到figure 1 ; 子图subplot(212)仍旧是当前图  
# plt.subplot(211) # 令子图subplot(211)成为figure1的当前图  
# plt.title('Easy as 1,2,3') # 添加subplot 211 的标题

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

def animate(i):
    graph_data = open('example.txt', 'r').read()
    lines = graph_data.split('\n')
    xs = []
    ys = []

    for line in lines:
        if len(line) > 1:
            x, y = line.split(',')
            xs.append(x)
            ys.append(y)

    ax1.clear()
    ax1.plot(xs, ys)

ani = animation.FuncAnimation(fig, animate, interval = 1000)

plt.show()













  • 2
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值