python数据动态可视化

python数据动态可视化

高频实时数据可视化,做一个简单记录:

  • 参考资料
  • 代码块实例

参考资料

real-time plotting in while loop with matplotlib
`

代码示例

参考资料例子为主题,根据需求进行修改。

实例1

代码块语法遵循标准markdown代码,例如:

import numpy as np
import time
import matplotlib
matplotlib.use('GTKAgg')
from matplotlib import pyplot as plt
import random

def randomwalk(dims=(256, 256), n=20, sigma=5, alpha=0.95, seed=1):
    """ A simple random walk with memory """

    r, c = dims
    gen = np.random.RandomState(seed)
    #pos = gen.rand(2, n) * ((r,), (c,))
    old_delta = gen.randn(2, n) * sigma
    pos=[[]]
    while True:
        #delta = (1. - alpha) * gen.randn(2, n) * sigma + alpha * old_delta
        #pos += delta
        #for ii in xrange(n):
        #    if not (0. <= pos[0, ii] < r):
        #        pos[0, ii] = abs(pos[0, ii] % r)
        #    if not (0. <= pos[1, ii] < c):
        #        pos[1, ii] = abs(pos[1, ii] % c)
        #old_delta = delta
        a=random.randint(1,1000)
        b=random.randint(1,1000)

        pos.append(a)
        yield (a ,b)#pos
        #yield pos


def run(niter=10000, doblit=True):
    """
    Display the simulation using matplotlib, optionally using blit for speed
    """

    fig, ax = plt.subplots(1, 1)
    ax.set_aspect('equal')
    ax.set_xlim(0, 255)
    ax.set_ylim(0, 255)
    ax.hold(True)
    #rw = randomwalk()
    #x, y = randomwalk()#rw.next()
    a=random.randint(1,1000)
    b=random.randint(1,1000)
    x=[]
    y=[]
    x.append(a)#random.randint(1,1000)
    y.append(b)#random.randint(1,1000)

    plt.show(False)
    plt.draw()

    if doblit:
        # cache the background
        background = fig.canvas.copy_from_bbox(ax.bbox)

    points = ax.plot(x, y, 'o')[0]
    #ax.add_artist(points)
    fig.canvas.draw()

    tic = time.time()

    for ii in xrange(niter):

        # update the xy data
        #x, y = rw.next()
        a=random.randint(1,1000)
        b=random.randint(1,1000)
        x.append(a)
        y.append(b)
        points.set_data(x, y)

        if doblit:
            # restore background
            fig.canvas.restore_region(background)

            # redraw just the points

            ax.draw_artist(points)

            # fill in the axes rectangle
            fig.canvas.blit(ax.bbox)

        else:
            # redraw everything
            fig.canvas.draw()

    plt.close(fig)
    print "Blit = %s, average FPS: %.2f" % (
        str(doblit), niter / (time.time() - tic))

if __name__ == '__main__':
    run(doblit=False)
    run(doblit=True)

说明

保留背景绘制数据,相比绘制整幅图提高10倍左右,随着数据增加提高效率降低。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值