1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# pip install pandas matplotlib
# python analyze_air_quality.py
# The script will create a directory named with today’s date (e.g., 2024-10-11) and save the graphs as PNG images inside that directory.
import os
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
def load_data(file_path):
"""Load the air quality CSV data."""
return pd.read_csv(file_path)
def preprocess_data(data):
"""Preprocess the data by converting the 'Time' column to datetime."""
data['Time'] = pd.to_datetime(data['Time'], format='%d/%m/%Y %I:%M:%S %p')
return data
def create_output_directory():
"""Create a directory with today's date to store the plots."""
today = datetime.today().strftime('%Y-%m-%d')
if not os.path.exists(today):
os.makedirs(today)
return today
def plot_data(data, output_dir):
"""Generate and save the time series plots to the output directory."""
# Plot CO2 levels over time
plt.figure(figsize=(10, 6))
plt.plot(data['Time'], data['CO2_ppm'], label='CO2 (ppm)', color='green')
plt.xlabel('Time')
plt.ylabel('CO2 (ppm)')
plt.title('CO2 Levels Over Time')
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig(f"{output_dir}/CO2_levels_over_time.png")
plt.close()
# Plot Temperature over time
plt.figure(figsize=(10, 6))
plt.plot(data['Time'], data['Temperature_F'], label='Temperature (°F)', color='red')
plt.xlabel('Time')
plt.ylabel('Temperature (°F)')
plt.title('Temperature Over Time')
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig(f"{output_dir}/Temperature_over_time.png")
plt.close()
# Plot Relative Humidity over time
plt.figure(figsize=(10, 6))
plt.plot(data['Time'], data['Humidity_percent'], label='Relative Humidity (%)', color='blue')
plt.xlabel('Time')
plt.ylabel('Relative Humidity (%)')
plt.title('Relative Humidity Over Time')
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig(f"{output_dir}/Relative_humidity_over_time.png")
plt.close()
# Plot Atmospheric Pressure over time
plt.figure(figsize=(10, 6))
plt.plot(data['Time'], data['Pressure_hPa'], label='Pressure (hPa)', color='purple')
plt.xlabel('Time')
plt.ylabel('Pressure (hPa)')
plt.title('Atmospheric Pressure Over Time')
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig(f"{output_dir}/Pressure_over_time.png")
plt.close()
def main():
"""Main function to load, process, and plot the air quality data."""
file_path = "export.csv"
# Load and preprocess the data
data = load_data(file_path)
data = preprocess_data(data)
output_dir = create_output_directory()
plot_data(data, output_dir)
print(f"Graphs have been saved in the directory: {output_dir}")
if __name__ == "__main__":
main()
|