Code to 50 temperature and humidity values,
import csv
import random
import time
# File name for saving the data
csv_filename = 'temperature_humidity_log.csv'
# Create and open the CSV file for writing
with open(csv_filename, mode='w', newline='') as file:
writer = [Link](file)
# Write the header
[Link](["Timestamp", "Temperature (°C)", "Humidity (%)"])
# Generate and write 50 readings
for i in range(50):
# Simulate random sensor readings
temperature = round([Link](20.0, 35.0), 2) # Simulate 20.0°C to 35.0°C
humidity = round([Link](30.0, 70.0), 2) # Simulate 30% to 70% humidity
timestamp = [Link]("%Y-%m-%d %H:%M:%S")
# Write the data row to the CSV file
[Link]([timestamp, temperature, humidity])
print(f"[{timestamp}] Temp: {temperature} °C | Humidity: {humidity} % recorded.")
# Wait 5 seconds before next reading
[Link](5)
print(f"\n✅ Data logging complete. 50 readings saved in '{csv_filename}'.")
Code to Visualize CSV Data with Explanations
import csv
import [Link] as plt
# File name to read data from
csv_filename = 'temperature_humidity_log.csv'
# Lists to store the CSV data
timestamps = []
temperatures = []
humidities = []
# Read the CSV file
with open(csv_filename, mode='r') as file:
reader = [Link](file)
for row in reader:
[Link](row["Timestamp"])
[Link](float(row["Temperature (°C)"]))
[Link](float(row["Humidity (%)"]))
# Plot 1: Temperature over Time (Line Plot)
[Link](figsize=(10,5))
[Link](timestamps, temperatures, marker='o', linestyle='-', color='red')
[Link]('Temperature Variation Over Time')
[Link]('Time')
[Link]('Temperature (°C)')
[Link](rotation=45)
[Link](True)
plt.tight_layout()
[Link]()
# Purpose:
# This graph shows how temperature changes over time, useful to detect heating or cooling
trends.
# Plot 2: Humidity over Time (Line Plot)
[Link](figsize=(10,5))
[Link](timestamps, humidities, marker='x', linestyle='-', color='blue')
[Link]('Humidity Variation Over Time')
[Link]('Time')
[Link]('Humidity (%)')
[Link](rotation=45)
[Link](True)
plt.tight_layout()
[Link]()
# Purpose:
# This graph tracks the environmental humidity changes, helping to monitor air moisture
conditions.
# Plot 3: Temperature vs Humidity (Scatter Plot)
[Link](figsize=(8,6))
[Link](temperatures, humidities, color='green')
[Link]('Temperature vs Humidity')
[Link]('Temperature (°C)')
[Link]('Humidity (%)')
[Link](True)
plt.tight_layout()
[Link]()
# Purpose:
# This scatter plot explores the relationship between temperature and humidity,
# helping to find if higher temperatures correlate with higher or lower humidity levels.
# Plot 4: Dual Axis Plot (Temperature and Humidity Together)
fig, ax1 = [Link](figsize=(10,5))
color = 'tab:red'
ax1.set_xlabel('Time')
ax1.set_ylabel('Temperature (°C)', color=color)
[Link](timestamps, temperatures, color=color, label='Temperature')
ax1.tick_params(axis='y', labelcolor=color)
[Link](rotation=45)
ax2 = [Link]() # instantiate a second axis sharing the same x-axis
color = 'tab:blue'
ax2.set_ylabel('Humidity (%)', color=color)
[Link](timestamps, humidities, color=color, linestyle='--', label='Humidity')
ax2.tick_params(axis='y', labelcolor=color)
[Link]('Temperature and Humidity Over Time')
fig.tight_layout()
[Link](True)
[Link]()
# Purpose:
# This dual-axis plot allows us to observe how temperature and humidity behave together over
the same time period.