实时可视化数据-流式数据可视化 python

ion版

import matplotlib.pyplot as plt
plt.ion()
fig = plt.figure()
ax1 = fig.add_subplot(111)
t = []
v = []
line, = ax1.plot(t, v, linestyle="-", color="r")
import numpy as np
ys = np.random.normal(100, 10, 1000)

for i in range(2000):
    t.append(i)
    v.append(ys[i])
    ax1.set_xlim(min(t), max(t) + 1)
    ax1.set_ylim(min(v), max(v) + 1)
    line.set_data(t, v)
    plt.pause(0.001)
    ax1.figure.canvas.draw()

animation版

# _*_ coding:utf-8 _*_

import time
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

import numpy as np
x1 = np.arange(0, 20, 1)
y1 = np.random.normal(100, 10, 20)

print(x1.shape)
print(y1.shape)
# format the time style 2016-12-01 00:00:00

x = []
y = []
# initial the size of the figure
fig = plt.figure(figsize=(18, 8), facecolor="white")
# fig.subplots_adjust(left=0.06, right=0.70)
ax1 = fig.add_subplot(111)

# initial plot
p1, = ax1.plot(x, y, linestyle="dashed", color="red")

ax1.set_ylabel("value")

def stream(i):
    x.append(x1[i])
    y.append(y1[i])
    time.sleep(3)#可以执行其他程序,比如等待,删除这个操作也可以
    # update the axis
    ax1.set_xlim(min(x1), max(x1)+1)
    ax1.set_ylim(min(y1), max(y1)+1)

    p1.set_data(x, y)
    ax1.figure.canvas.draw()
    return p1


# main animated function
# for i in range(25):#可以将下面实现放在循环里面
anim = FuncAnimation(fig, stream, frames=len(x1))
plt.show()

参考:可视化篇:流式数据监控(python)
最后再放两个例子

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
'''
batch_samples -> N,T,K,2
gt_traj  -> N,T,2
'''
#为啥出现首尾相连问题
def gen_colorsk(color_map):
    k,_ = color_map.shape
    colors = []
    for i in range(k):
        colors.append("#%02x%02x%02x"% tuple(color_map[i]))  # 250 250 250 ,g值越小越靠近0红色
    return colors

def gen_colors(color_map):
    l,w,h = color_map.shape
    colors = []
    for r in range(l):
        for g in range(w):
               colors.append("#%02x%02x%02x"% tuple(color_map[r,g]))  # 250 250 250 ,g值越小越靠近0红色
    return colors

def visual_pred_traj(batch_samples, gt_traj):
    N,T,K,_ = batch_samples.shape

    r = np.random.randint(0, 255, K)
    g = np.random.randint(0, 255, K)
    b = np.random.randint(0, 255, K)

    # r = np.random.randint(0, 255, (N, K))
    # g = np.random.randint(0, 255, (N, K))
    # b = np.random.randint(0, 255, (N, K))
    color = np.stack((r,g,b),axis=-1)
    print("color",color.shape)
    colors = gen_colorsk(color)
    print("colors", len(colors))
    colors = ['purple','blue','green']
    for i in range(N):
        print("i",i)
        fig = plt.figure(figsize=(18, 8), facecolor="white")
        ax1 = fig.add_subplot(111)
        ax1.set_xlabel("pixel_x")
        ax1.set_ylabel("pixel_y")
        ax1.set_xlim(0, 1920)
        ax1.set_ylim(0, 1080)
        lines = []
        px = []
        py = []
        for j in range(K):
            px.append([])
            py.append([])
            line1, = ax1.plot(px[j], py[j], linestyle="dashed", color=colors[int(j)])
            lines.append(line1)
        px.append([])
        py.append([])
        line2, = ax1.plot(px[-1], py[-1], linestyle="solid", color='red')
        lines.append(line2)

        def stream_p(t):

            print(t)
            for u in range(K):
                px[u].append(batch_samples[i,t,u,0])
                py[u].append(batch_samples[i,t,u,1])
                plt.pause(0.2)
                lines[u].set_data(px[u], py[u])
                ax1.figure.canvas.draw()  # 重绘子图画布,使改变生效

            px[-1].append(gt_traj[i, t, 0])
            py[-1].append(gt_traj[i, t, 1])
            plt.pause(0.2)
            lines[-1].set_data(px[-1], py[-1])
            ax1.figure.canvas.draw()#重绘子图画布,使改变生效

            return lines,

        anim_p = FuncAnimation(fig, stream_p, frames=T, interval=10, repeat=False,blit=False)
        # anim_p.save('sample_{}.gif'.format(str(i)),fps=2, writer='pillow')#当加入该行代码时,stream_p会被调用两次,显示得图像会出现重复绘制的现象,但是保存的gif不会出现,因此当保存图时,加入该行代码;只显示图时,注释掉改行代码
        plt.show()



if __name__ == '__main__':

    batch_samples = np.random.randint(0,1080,(5,4,3,2))
    gt = np.random.randint(0,1000,(5,4,2))
    visual_pred_traj(batch_samples,gt)


ion版

import matplotlib.pyplot as plt
import numpy as np
'''
batch_samples -> N,T,K,2
gt_traj  -> N,T,2
'''

def gen_colors(color_map):
    l,w,h = color_map.shape
    colors = []
    for r in range(l):
        for g in range(w):
               colors.append("#%02x%02x%02x"% tuple(color_map[r,g]))  # 250 250 250 ,g值越小越靠近0红色
    return colors

def visual_pred_traj(batch_samples, gt_traj):
    N,T,K,_ = batch_samples.shape
    plt.ion()
    r = np.random.randint(0, 255, (N, K))
    g = np.random.randint(0, 255, (N, K))
    b = np.random.randint(0, 255, (N, K))
    color = np.stack((r,g,b),axis=-1)
    print("color",color.shape)
    colors = gen_colors(color)
    print("colors", len(colors))
    for i in range(N):
        fig = plt.figure(figsize=(18, 8), facecolor="white")
        ax1 = fig.add_subplot(111)
        ax1.set_xlabel("pixel_x")
        ax1.set_ylabel("pixel_y")
        ax1.set_xlim(0, 1920)
        ax1.set_ylim(0, 1080)
        for j in range(K):
            pred_traj = batch_samples[i,:,j,:]
            print("pred_traj",pred_traj.shape)
            px = []
            py = []
            line1, = ax1.plot(px, py, linestyle="dashed", color=colors[int(i * K + j)])
            for t in range(T):
                print(t)
                px.append(pred_traj[t,0])
                py.append(pred_traj[t,1])
                line1.set_data(px,py)
                plt.pause(0.1)
                ax1.figure.canvas.draw()

        gx = []
        gy = []
        line2, = ax1.plot(gx, gy, linestyle="solid", color='red')
        each_gt = gt_traj[i]
        for t in range(T):
            print("t",t)
            gx.append(each_gt[t,0])
            gy.append(each_gt[t,1])
            line2.set_data(gx,gy)
            plt.pause(2)
            ax1.figure.canvas.draw()

        plt.ioff()
    plt.show()

if __name__ == '__main__':

    batch_samples = np.random.randint(0,1080,(2,3,1,2))
    gt = np.random.randint(0,1080,(2,3,2))
    visual_pred_traj(batch_samples,gt)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值