라즈베리 파이에 연결된 센서가 여러 개 있습니다. 나는 그들의 데이터를 TCP를 사용하여 초당 2 번 내 PC로 보냈다. matplotlib를 사용하여이 값을 지속적으로 그래프로 나타내고 싶습니다.matplotlib의 바 플롯을 동적으로 업데이트합니다.
현재 사용중인 메서드는 비효율적 인 것처럼 보입니다 (서브 Plot을 지우고 다시 그릴 때마다) 그리고 몇 가지 바람직하지 않은 단점이 있습니다 (규모가 매번 재조정 됨, 0.0 - 5.0에서 유지하고 싶습니다). 나는 명확하게하고 다시 그릴 필요없이 이것을하는 방법이 있지만 알아낼 수는 없다는 것을 알고있다. 하기 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