matplotlib plot()不起作用

问题描述 投票:0回答:1

我正在使用Raspberry 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轴读数,但打开的图形不显示任何内容。它保持不变。我需要在代码中更改什么?谢谢。

python matplotlib
1个回答
1
投票

您正在尝试绘制单个值的线图。这是一样的

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

没有出现,因为一条线至少需要两个点才能成为一条线。

您可以使用标记显示单个点,以防这是您所追求的

plt.plot([1],[5], marker="o")
© www.soinside.com 2019 - 2024. All rights reserved.