में बार बार प्लॉट को गतिशील रूप से अपडेट करना मेरे पास रास्पबेरी पीआई से जुड़े कई सेंसर हैं; मैं टीसीपी का उपयोग कर अपने पीसी पर दो बार एक बार अपने डेटा भेज रहा हूं। मैं matplotlib का उपयोग कर इन मूल्यों को लगातार ग्राफ करना चाहता हूं।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()
इसके लिए बहुत धन्यवाद, मैं इसे अपग्रेड के रूप में कार्यान्वित करने के लिए देखता हूं। हालांकि, मैं अभी भी जानना चाहूंगा कि क्या मेरे मूल मुद्दे पर एक सरल फिक्स के रूप में matplotlib चार्ट को समाशोधन के बिना अपडेट करना है? – hfaran