10

Raspberry Pi には多数のセンサーが取り付けられています。TCP を使用して 1 秒に 2 回、自分の PC にデータを送信しています。matplotlib を使用して、これらの値を継続的にグラフ化したいと思います。

私が現在使用している方法は非効率的で (サブプロットをクリアして毎回再描画しています)、望ましくない欠点がいくつかあります (スケールは毎回再調整されます。0.0 から 5.0 にとどめたいと思います)。クリアして再描画することなくこれを行う方法があることは知っていますが、それを理解できないようです。以下は私の現在のコードです:

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()
4

2 に答える 2

24

を使用できますanimation.FuncAnimation。棒グラフを一度プロットし、Rects のコレクションである戻り値を保存します。

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

次に、バーの高さを変更するには、次のように呼び出します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()
于 2013-04-27T13:28:38.753 に答える
2

matplotlib が強制的なオプションでない場合は、サーバーに Web ソケット ベースのプッシュ システムを、クライアント側に Javascript ベースのプロットをお勧めします。最初にいくつかの利点を挙げます。

  1. クライアント (他の PC) には、最新の Web ブラウザーのみがインストールされている必要があり、任意の OS を実行でき、Python、Matplotlib をインストールする必要はありません。
  2. WebSocket はブロードキャスト方式で機能するため、任意の数のクライアントに同じフィードを使用させることができ、ユーザーにシステムのデモを提供する際に非常に役立ちます。
  3. クライアント側のコードも効率的で、最後の「x」値を保持し、リアルタイムでうまく機能するため、すべてを再描画する必要はありません

私は Raspberry Pi で非常に似たようなことをしているので、同じ詳細を共有できます。これは、このブログ投稿に触発されたものです。データをプッシュするサーバー側のコードは、こちらにありますsocket.send()依存関係をインストールした後、それはあなたのコードに非常に似ており、最終的には私のコードに偶数が見つかることがわかるでしょう。クライアント側の場合、これは HTML ファイルへのリンクであり、これはブラウザーで実行される JS であり、Flot Plottingライブラリを使用します。彼らのホームページのデモは、注目に値するほど素晴らしいものだと確信しています!

于 2013-04-27T08:11:50.997 に答える