matplotlib 如何在线图上显示单个点[重复]

wxclj1h5  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(112)

此问题已在此处有答案

How to plot a single point in matplotlib(2个答案)
16天前关闭
代码在下面。我在绘制压力传感器感受到的压力时遇到了问题。如果我将图表的格式更改为:plt.plot(t, LBS_DATA_SENSOR1, 'c.')它可以很好地用点绘制数据。然而,当我把它改为‘c-‘来制作一条线段的线时,它不会画出任何东西。此时将显示绘图并且轴移动(x和y),就像在不可见墨水中绘图一样。我知道数据正在被接收,因为它用点绘制得很好。我就是不能把它变成台词!

#TE FX29 load cell sensor
#set up digital io
import matplotlib.pyplot as plt
import RPi.GPIO as IO          #calling header file which helps us use GPIO’s of PI
IO.setwarnings(False)           #do not show any warnings
IO.setmode (IO.BCM)         #we are programming the GPIO by BCM pin numbers. (PIN35 as    ‘GPIO19’)
IO.setup(19,IO.OUT)           # initialize GPIO19 as an output, not important for the pressure     sensor or load cell
#set up i2c
import time
import smbus
from time import sleep
bus = smbus.SMBus(1) #I2C channel 1 is connected to the GPIO pins 2 (SDA) and 4 (SCL)
channel = 1          #select channel
t=0
plt.figure()
#Condition sensor for continuous measurements
LOAD_SENSOR_ADDRESS=0x28
dummy_command=0x00
offset=1000
#offset=int((input("Enter offset value, default 1000:") or 1000))                                            #subtracts zero offset per data sheet, should be 1000
LOAD_SENSOR_DATA=bus.read_byte(LOAD_SENSOR_ADDRESS)#This apparently turns the load sensor on,    only need it once
running_max = 0
#take continuous measurements and report
while 1:
    bus.write_byte(LOAD_SENSOR_ADDRESS,dummy_command)                                              #without this command, the status bytes go high on every other read
    LOAD_SENSOR_DATA=bus.read_i2c_block_data(LOAD_SENSOR_ADDRESS,dummy_command,2)      
    LBS_DATA_SENSOR1=((LOAD_SENSOR_DATA[0]&63)*2**8 + LOAD_SENSOR_DATA[1] - offset)*100/14000                                                                                  #It does return the correct two bytes after the initial read byte command    
    if LBS_DATA_SENSOR1 > running_max:
        running_max = LBS_DATA_SENSOR1
    plt.plot(t, LBS_DATA_SENSOR1, 'c-')
    plt.xlim(0, t+1)
    plt.ylim(0, running_max + 10)
    plt.pause(0.01)
    t += .01

我已经看了文档,并试图改变线的样式,以许多东西,但它总是结束了什么也没有绘制或绘制修改的点在任何大小和颜色,我想要的线。我已经问了聊天GPT,我已经添加了一个等待时间的程序赶上,我已经搜索了互联网的解决方案,但我空手而归。

ryoqjall

ryoqjall1#

问题是您试图使用线绘制单个点,因此不会绘制任何内容。你需要两个x点和两个y点来定义一条直线。您可以做的是将数据保存到列表中并不断更新图。这可以通过两种方式完成。第一种方法是手动的,您创建列表来存储数据,并在每个循环中更新它们。第二个方法访问存储在行对象中的数据,并在每个循环中追加更多的数据。我认为第一种方法可读性更强(而且由于某种原因,动画似乎不那么出问题)。

import numpy as np
import matplotlib.pyplot as plt

plt.close("all")

x = np.linspace(0, 1, 100)
y = x**3

# option 1
fig, ax = plt.subplots()
line, = ax.plot([],[], "c-")

# you will store the data in lists and append new data to those lists
xdata = []
ydata = []
for _x, _y in zip(x, y):
    xdata.append(_x)
    ydata.append(_y)
    line.set_data(xdata, ydata)
    ax.relim()
    ax.autoscale_view()
    fig.canvas.draw()
    plt.pause(0.01)
    
# option 2
fig, ax = plt.subplots()
line, = ax.plot([],[], "c-")
for _x, _y in zip(x, y):
    line.set_xdata(np.append(line.get_xdata(), _x))
    line.set_ydata(np.append(line.get_ydata(), _y))
    ax.relim()
    ax.autoscale_view()
    fig.canvas.draw()
    plt.pause(0.01)

相关问题