在matplotlib中动态更新条形图 [英] Dynamically updating a bar plot in matplotlib

查看:309
本文介绍了在matplotlib中动态更新条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Raspberry Pi上连接了许多传感器;我使用TCP每秒两次将其数据发送到我的PC.我想使用matplotlib连续绘制这些值.

I have a number of sensors attached to my Raspberry Pi; I'm sending their data to my PC twice a second using TCP. I would like to continuously graph these values using matplotlib.

我当前使用的方法似乎效率低下(我每次都在清除子图并重新绘制),并且有一些不良的缺点(每次都会重新调整比例;我希望它保持在0.0-5.0之间).我知道有一种方法可以不必清除并重新绘制,但似乎无法弄清楚.以下是我当前的代码:

The method I'm currently using seems inefficient (I'm clearing the subplot and redrawing it every time) and has some undesirable drawbacks (the scale gets readjusted every time; I would like it stay from 0.0 - 5.0). I know there's a way of doing this without having to clear and redraw but can't seem to figure it out. The following is my current code:

import socket
import sys
import time
from matplotlib import pyplot as plt

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('192.168.0.10', 10000)
print >>sys.stderr, 'connecting to %s port %s' % server_address
sock.connect(server_address)

# Initial setup for the bar plot
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x = [1,2,3]
labels = ['FSR', 'Tilt', 'IR']
ax.set_xticklabels(labels)
y = [5.0,5.0,5.0]
ax.bar(x,y)
fig.autofmt_xdate()
plt.draw()

#Grab and continuously plot sensor values
try:
    for i in range(300):
        amount_received = 0
        amount_expected = len("0.00,0.00,0.00")

        # Receive data from RasPi
        while amount_received < amount_expected:
            data = sock.recv(14)
            amount_received += len(data)
            print >>sys.stderr, 'received "%s"' % data

        # Plot received data
        y = [float(datum) for datum in data.split(',')]
        ax.clear()
        ax.bar(x,y)
        plt.draw()
        time.sleep(0.5)

#Close the socket       
finally:
    print >>sys.stderr, 'closing socket'
    sock.close()

推荐答案

您可以使用 animation.FuncAnimation . 绘制一次条形图并保存返回值,该值是Rects的集合:

You could use animation.FuncAnimation. Plot the bar plot once and save the return value, which is a collection of Rects:

rects = plt.bar(range(N), x, align='center')

然后,要更改条形的高度,请调用rect.set_height:

Then, to change the height of a bar, call rect.set_height:

    for rect, h in zip(rects, x):
        rect.set_height(h)


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def animate(frameno):
    x = mu + sigma * np.random.randn(N)
    n, _ = np.histogram(x, bins, normed=True)
    for rect, h in zip(patches, n):
        rect.set_height(h)
    return patches

N, mu, sigma = 10000, 100, 15
fig, ax = plt.subplots()
x = mu + sigma * np.random.randn(N)
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green', alpha=0.75)

frames = 100
ani = animation.FuncAnimation(fig, animate, blit=True, interval=0,
                              frames=frames,
                              repeat=False)
plt.show()

这篇关于在matplotlib中动态更新条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆