私は自分のラズベリーパイに数多くのセンサーを接続しています。私はTCPを使って自分のPCにデータを2秒間送っています。 matplotlibを使ってこれらの値をグラフにしたいと思います。matplotlibのバープロットを動的に更新します
私が現在使用しているメソッドは非効率的です(私はサブプロットをクリアして毎回それを再描画しています)、いくつかの望ましくない欠点があります(スケールは毎回再調整されます。私はクリアして再描画せずにこれを行う方法があることを知っているが、それを把握するように見えることはできません。以下は、私の現在のコードです:
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()
多くのありがとう、私はこれをアップグレードとして実装しようとします。しかし、私はまだクリアしないでmatplotlibのグラフを更新することで私の元の問題に単純な修正があるかどうか知りたいですか? – hfaran