62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
|
import matplotlib.pyplot as plt
|
||
|
import pandas as pd
|
||
|
import os
|
||
|
|
||
|
# Generowanie przykładowych danych
|
||
|
def generate_sample_data(num_records=100):
|
||
|
import random
|
||
|
from datetime import datetime, timedelta
|
||
|
|
||
|
station_ids = [101, 102, 103, 104]
|
||
|
pollutants = ["PM10", "WWA"]
|
||
|
start_date = datetime(2023, 1, 1)
|
||
|
end_date = datetime(2023, 12, 31)
|
||
|
|
||
|
data = []
|
||
|
for _ in range(num_records):
|
||
|
record = {
|
||
|
"station_id": random.choice(station_ids),
|
||
|
"pollutant": random.choice(pollutants),
|
||
|
"date": (start_date + timedelta(days=random.randint(0, (end_date - start_date).days))).strftime("%Y-%m-%d"),
|
||
|
"value": round(random.uniform(5, 50), 2), # Example values
|
||
|
}
|
||
|
data.append(record)
|
||
|
|
||
|
return pd.DataFrame(data)
|
||
|
|
||
|
# Generowanie danych
|
||
|
data = generate_sample_data(100)
|
||
|
|
||
|
# Tworzenie katalogu na wykresy
|
||
|
output_dir = "figs"
|
||
|
os.makedirs(output_dir, exist_ok=True)
|
||
|
|
||
|
# Generowanie wykresów
|
||
|
# Histogramy stężeń dla każdego zanieczyszczenia
|
||
|
pollutants = data["pollutant"].unique()
|
||
|
for pollutant in pollutants:
|
||
|
subset = data[data["pollutant"] == pollutant]
|
||
|
plt.figure(figsize=(8, 6))
|
||
|
plt.hist(subset["value"], bins=10, edgecolor="black", alpha=0.7)
|
||
|
plt.title(f"Histogram of {pollutant} concentrations")
|
||
|
plt.xlabel("Concentration")
|
||
|
plt.ylabel("Frequency")
|
||
|
plt.grid(True)
|
||
|
plt.savefig(os.path.join(output_dir, f"histogram_{pollutant}.png"))
|
||
|
plt.close()
|
||
|
|
||
|
# Średnie stężenia w czasie
|
||
|
data["date"] = pd.to_datetime(data["date"])
|
||
|
mean_over_time = data.groupby(["date", "pollutant"])["value"].mean().unstack()
|
||
|
mean_over_time.plot(figsize=(10, 6), marker="o")
|
||
|
plt.title("Mean Concentrations Over Time")
|
||
|
plt.xlabel("Date")
|
||
|
plt.ylabel("Mean Concentration")
|
||
|
plt.legend(title="Pollutant")
|
||
|
plt.grid(True)
|
||
|
plt.savefig(os.path.join(output_dir, "mean_concentration_over_time.png"))
|
||
|
plt.close()
|
||
|
|
||
|
print(f"Figures have been saved to the '{output_dir}' directory.")
|
||
|
|