我试图通过具有多个子图并进行遍历来在散点图中绘制一些不同的事物。这里是一个结果实际上是什么样的示例:
('x out: ', ' -511', ' y out: ', ' 1')
('Magnitudo = ', 4.778809128414475)
[0.]
('x out: ', ' -511', ' y out: ', ' -255')
('Magnitudo = ', 5.9840357600793475)
[1.]
('x out: ', ' -511', ' y out: ', ' 1')
('Magnitudo = ', 5.474086008639472)
[0.]
('x out: ', ' 513', ' y out: ', ' -511')
('Magnitudo = ', 5.182103409440737)
[0.]
('x out: ', ' -511', ' y out: ', ' 513')
('Magnitudo = ', 5.1769691160028835)
[0.]
('x out: ', ' -255', ' y out: ', ' -511')
('Magnitudo = ', 6.559643742815329)
[1.]
这是我生成的matplot:
如您所见,matplot仅显示结果的最后一次迭代。如何将所有迭代呈现到该图中?谢谢。这是我的代码。我哪里出错了?
print ("Gyroskop")
print ("--------")
i="TRUE"
j = 0
mag = []
with open("/home/pi/TA/accelero.csv") as csvFile:
while i == "TRUE":
gyroskop_xout1 = read_word_2c(0x43)
gyroskop_yout1 = read_word_2c(0x45)
acc1 = math.sqrt((gyroskop_xout1*gyroskop_xout1)+(gyroskop_yout1*gyroskop_yout1))
mag.append(acc1)
print ("x out: ", ("%5d" % gyroskop_xout1), " y out: ",("%5d" % gyroskop_yout1))
j=j+1
time.sleep(1)
#---------------------
gyroskop_xout2 = read_word_2c(0x43)
gyroskop_yout2 = read_word_2c(0x45)
acc2 = math.sqrt((gyroskop_xout2*gyroskop_xout2)+(gyroskop_yout2*gyroskop_yout2))
mag.append(acc2)
time.sleep(1)
#--------------------
mag_max = max(mag)
mag_min = min(mag)
amplitude = mag_max - mag_min
# print "A : ",amplitude
h = amplitude/0.001
# print h
# print abs(h)
M_richter = 0
if(h!=0):
M_richter = math.log10(abs(h))
print ("Magnitudo = ",(M_richter))
#mag.clear()
del mag [:]
#accel = deltaM /2
hasil=clf.predict([[1, M_richter]])
print(hasil)
if (M_richter >= 6):
while aaa<5:
GPIO.output(buzzer,GPIO.HIGH)
print ("Beep")
sleep(0.5) # Delay in seconds
GPIO.output(buzzer,GPIO.LOW)
#print ("No Beep")
sleep(0.5)
aaa=aaa+1
csvFile.close()
i = "False"
if j == 10:
i = "False"
X0, X1 = 1, M_richter
plt.scatter(X0,X1, color = 'G')
plt.title('linear SVC')
plt.ylabel('Magnitude')
plt.xlabel('Location')
plt.show()
默认情况下Y为1,我想展示我所有的magnitudo,谢谢。
答案 0 :(得分:0)
当前,您仅将单个点值(X0,X1)传递给plt.scatter()。您需要向其传递值列表,例如:
plt.scatter([x1,x2,x3,x4], [y1,y2,y3,y4], color = 'G')
但是,我不确定您要在此处绘制的内容吗?如果要绘制幅度(M_richter
)与索引j
的关系,则必须在每次迭代中将计算出的M_richter
附加到列表中,然后将该列表传递给plt.scatter( ),并对j
的值进行相同的操作。您的脚本将如下所示:
print ("Gyroskop")
print ("--------")
i="TRUE"
j = 0
mag = []
M_richter_values = []
j_values = []
with open("/home/pi/TA/accelero.csv") as csvFile:
while i == "TRUE":
gyroskop_xout1 = read_word_2c(0x43)
gyroskop_yout1 = read_word_2c(0x45)
acc1 = math.sqrt((gyroskop_xout1*gyroskop_xout1)+(gyroskop_yout1*gyroskop_yout1))
mag.append(acc1)
print ("x out: ", ("%5d" % gyroskop_xout1), " y out: ",("%5d" % gyroskop_yout1))
j_values.append(j) # Append value of j to list
j=j+1
time.sleep(1)
#---------------------
gyroskop_xout2 = read_word_2c(0x43)
gyroskop_yout2 = read_word_2c(0x45)
acc2 = math.sqrt((gyroskop_xout2*gyroskop_xout2)+(gyroskop_yout2*gyroskop_yout2))
mag.append(acc2)
time.sleep(1)
#--------------------
mag_max = max(mag)
mag_min = min(mag)
amplitude = mag_max - mag_min
# print "A : ",amplitude
h = amplitude/0.001
# print h
# print abs(h)
M_richter = 0
if(h!=0):
M_richter = math.log10(abs(h))
print ("Magnitudo = ",(M_richter))
M_richter_values.append(M_richter) # Append to list
#mag.clear()
del mag [:]
#accel = deltaM /2
hasil=clf.predict([[1, M_richter]])
print(hasil)
if (M_richter >= 6):
while aaa<5:
GPIO.output(buzzer,GPIO.HIGH)
print ("Beep")
sleep(0.5) # Delay in seconds
GPIO.output(buzzer,GPIO.LOW)
#print ("No Beep")
sleep(0.5)
aaa=aaa+1
csvFile.close()
i = "False"
if j == 10:
i = "False"
X0, X1 = 1, M_richter
plt.scatter(j_values, M_richter_values, color = 'G') # Passing lists instead of scalars
plt.title('linear SVC')
plt.ylabel('Magnitude')
plt.xlabel('Location')
plt.show()