You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Sense Hat温度传感器数据与Matplotlib绘图代码整合问题

Fixing the Integration of Sense Hat Temperature Data with Matplotlib

Hey there! Let's work through this together—combining your Sense Hat temperature collection with Matplotlib plotting is totally manageable, and the issue is likely just a small misstep in how you're moving data between the two parts.

First, let's cover the most common reasons you might be hitting errors when passing your temperature list to the y-axis:

  • Incorrect data type: If your temperature readings are stored as strings instead of numbers (floats/integers), Matplotlib can't plot them.
  • Scope/timing issues: Your temperature list might be empty or out of scope when you try to plot it, or you're trying to plot before the data is fully collected.
  • Misplaced plotting code: If you're trying to plot every single reading inside the collection loop instead of waiting to gather all data first, that can cause unexpected behavior.

Here's a complete, tested integrated example that should solve the problem:

from sense_hat import SenseHat
import matplotlib.pyplot as plt
import time

# Initialize the Sense Hat
sense = SenseHat()

# Set up empty lists to store our data
temperature_readings = []
time_points = []

# Configure data collection parameters
total_readings = 25
seconds_between_readings = 1

# Collect temperature data
for reading_num in range(total_readings):
    # Grab the temperature (ensure it's a numeric value)
    current_temp = round(sense.get_temperature(), 2)
    temperature_readings.append(current_temp)
    # Track time for the x-axis (use index * interval for simplicity)
    time_points.append(reading_num * seconds_between_readings)
    
    print(f"Reading {reading_num + 1}: {current_temp}°C")
    time.sleep(seconds_between_readings)

# Now plot the full dataset
plt.figure(figsize=(10, 6))
plt.plot(time_points, temperature_readings, marker='o', linestyle='-', color='orange')
plt.title('Sense Hat Temperature Over Time')
plt.xlabel('Time (Seconds)')
plt.ylabel('Temperature (°C)')
plt.grid(True)
plt.show()

Critical Tips to Avoid Errors:

  • Validate your data: Before plotting, add print(temperature_readings) to confirm your list has valid numbers. If it's empty or has non-numeric entries, that's your root cause.
  • Separate collection and plotting: Gather all your data first in the loop, then run the plotting code once afterward. This ensures you're working with a complete, populated list.
  • Explicitly handle data types: While sense.get_temperature() returns a float by default, rounding or converting it explicitly (like float(sense.get_temperature())) prevents any accidental type mismatches.

If you're still getting a specific error message, share it and we can dig deeper!

内容的提问来源于stack exchange,提问作者user8868975

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:04:45