【问题标题】:Difficulty with annotate function in Python 2Python 2 中的注释函数的困难
【发布时间】:2015-08-11 09:23:20
【问题描述】:

我正在尝试通过串行端口从arduino 在 Python 上绘制实时数据。

我发现annotate功能有一些困难:我不知道如何配置它,也不知道我放置annotate功能的地方是否正确。

import serial
import time
import numpy
import matplotlib.pyplot as plt
from drawnow import *

temperature= []
vitesse= []
charge= []
current= []
Time = []
cnt=0

# Create our serial object
arduinoData = serial.Serial('com5', 9600) 

# Turn on matplotlib interactive mode to plot live data
plt.ion() 

##fig1 = plt.figure()
STARTINGTIME = round(time.time(),2)

# A function that makes our desired plot
def makeFig(): 
    plt.subplot(2,2,1)
    plt.subplot(2,2,1).annotate(str(temperature)+','+ str(Time), 
                                textcoords='offset points')
    plt.title('Live Streaming Temperature Sensor Data')
    plt.ylabel('Temperature C')
    plt.grid(True)
    plt.plot(temperature, 'ro-')

    plt.subplot(2,2,2)
    plt.subplot(2,2,2).annotate(str(vitesse)+','+ str(Time), 
                                textcoords='offset points')
    plt.title('Live Streaming Speed Sensor Data')
    plt.ylabel('Speed KM/H')
    plt.grid(True)
    plt.plot(vitesse, 'bo-')

    plt.subplot(2,2,3)
    plt.subplot(2,2,3).annotate(str(charge)+','+ str(Time), 
                                textcoords='offset points')
    plt.title('Live Streaming SOC Sensor Data')
    plt.ylabel('Battery Charge %')
    plt.grid(True)
    plt.plot(charge, 'go-')

    plt.subplot(2,2,4)
    plt.subplot(2,2,4).annotate(str(current)+','+ str(Time), 
                                textcoords='offset points')
    plt.title('Live Streaming Current Sensor Data')
    plt.ylabel('Current A')
    plt.grid(True)
    plt.plot(current, 'yo-')

while True:

    while (arduinoData.inWaiting()==0): 
        # Wait here until there is data
        pass

    # Read the line of text from the serial port
    arduinoString = arduinoData.readline() 
    # Split it into an array
    dataArray = arduinoString.split(';')   
    temp = float(dataArray[0])
    vite = float(dataArray[1])
    char = float(dataArray[2])
    curr = float(dataArray[3])

    # Build our temperature array by appending temperature readings
    temperature.append(temp) 
    # Build our vitesse array by appending temp readings
    vitesse.append(vite)                    
    # Build our charge array by appending temp readings 
    charge.append(char)                     
    # Build our current array by appending temp readings
    current.append(curr)                     

    Time.append(round(time.time(),2) - STARTINGTIME)

    # Update our live graph
    drawnow(makeFig)                       

    plt.pause(0.00001)
    cnt += 1
    if(cnt > 50):
        temperature.pop(0)
        vitesse.pop(0)
        charge.pop(0)
        current.pop(0)

【问题讨论】:

  • 你没有说你想要什么结果。
  • give me error a = mtext.Annotation(*args, **kwargs) TypeError: __init__() 需要至少 3 个参数(3 个给定)
  • 我想在绘图上放置标签数据点
  • 您想要每个数据点的标签吗?例如,对于temperature 中的每个点,您希望将值打印在该点旁边吗?
  • 是的,我希望将值打印在该点旁边

标签: python matplotlib matplotlib-basemap matplotlib-widget


【解决方案1】:

annotate 至少需要两个参数。第一个是文本,第二个是一个元组,其中包含您希望文本所在的坐标。此外,由于您要注释每个点,因此您需要在循环中调用 annotate

这是一个例子:

import matplotlib.pyplot as plt
import numpy as np
Y = np.sin(np.linspace(0, 3, 10))
plt.plot(Y)
for i, y in enumerate(Y):
    plt.annotate(str(y), (i, y))
plt.show()

下面是循环在代码中的位置:

def makeFig(): #Create a function that makes our desired plot

    plt.subplot(2,2,1)
    plt.title('Live Streaming Temperature Sensor Data')
    plt.ylabel('Temperature C')
    plt.grid(True)
    plt.plot(temperature, 'ro-')
    for i, t in enumerate(temperature):
        plt.annotate(str(t), (i, t), textcoords='offset points')

【讨论】:

  • 非常感谢,但我没有像你那样逐点绘制的函数
  • 没关系。 Y 是一个 numpy 数组。您可以使用 for 循环遍历任何内容。
  • 我想在 enumerate() 里面放什么
  • 任何可迭代的。例如,温度或电流。
  • 温度的第一个元素是什么?
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 2016-07-20
  • 2021-11-06
  • 2021-08-08
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多