matplotlib plot()不工作

2024-09-30 05:23:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用树莓Pi创建一个简单的图形,显示通过GPIO引脚从电位计模拟读数。我创建了一个小电路,可以克服RPi无法读取模拟信号。绘图本身有一个小问题。我使用的代码如下所示。你知道吗

# include RPi libraries in to Python code
import RPi.GPIO as GPIO
import time
import matplotlib.pyplot as plt
from drawnow import drawnow

# instantiate GPIO as an object
GPIO.setmode(GPIO.BCM)

# define GPIO pins with variables a_pin and b_pin
a_pin = 18
b_pin = 23
gainF = []
gainString = 0
plt.ion()
x_axis = 0

def makeFig():
    plt.ylim(200,210)
    plt.xlim(0,100)
    plt.title('Readings')
    plt.grid(True)
    plt.ylabel('Gain')
    print(gainString)
    print(x_axis)
    plt.plot(gainString, x_axis)
    plt.show()
    #plt.plot(gainString, 'ro-', label='Gain dBm')


# create discharge function for reading capacitor data
def discharge():
    GPIO.setup(a_pin, GPIO.IN)
    GPIO.setup(b_pin, GPIO.OUT)
    GPIO.output(b_pin, False)
    time.sleep(0.005)

# create time function for capturing analog count value
def charge_time():
    GPIO.setup(b_pin, GPIO.IN)
    GPIO.setup(a_pin, GPIO.OUT)
    count = 0
    GPIO.output(a_pin, True)
    while not GPIO.input(b_pin):
        count = count +1
    return count

# create analog read function for reading charging and discharging data
def analog_read():
    discharge()
    return charge_time()

# provide a loop to display analog data count value on the screen
while True:
    print(analog_read())
    gainString = analog_read()
    x_axis = x_axis + 1
    #dataArray = gainString.split(',')
    #gain = float(dataArray[0])
    #gainF.append(gain)
    makeFig()
    plt.pause(.000001)
    time.sleep(1)

    #GPIO.cleanup() 

此代码显示makeFig()函数中不断增加的x轴和y轴读数,但打开的图形不显示任何内容。它保持不变。我需要修改密码吗?谢谢。你知道吗


Tags: importreadgpiotimedefascountsetup
1条回答
网友
1楼 · 发布于 2024-09-30 05:23:10

您正在尝试绘制单个值的折线图。这和

plt.plot([1],[5])

因为一条线至少需要两个点才能成为一条线。你知道吗

你可以用一个标记来显示单个点,以防这是你要找的

plt.plot([1],[5], marker="o")

相关问题 更多 >

    热门问题