| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import pandas as pd
- import matplotlib.pyplot as plt
- import numpy as np
- import matplotlib.dates as mdates
- #======================================================
- # == SCRIPT PARAMETERS ==
- #======================================================
- # === CSV Parameters ===
- CSV_FILENAME = r"c:\Users\ranny\Downloads\week_2025_41_data.csv"
- CSV_SEPARATOR = ","
- COL_DIAMETER = "Diameter1 [mm]"
- COL_VELOCITY = "Velocity [m/s]"
- COL_TEMPERATURE = "Temperature [C]"
- FIG_TITLE = f"Rain activity of week 41 of 2025"
- FIG_GRID_STYLE = ":"
- # === Limits for Velocity & Diameter Filtering ===
- LIM_VELOCITY_MIN = 0.2 # m/s
- LIM_DIAMETER_MIN = 0.16 # mm
- LIM_DIAMETER_MAX = 8.0 # mm
- # === Figure Parameters ===
- FIG_WIDTH = 9
- FIG_HEIGHT = 6
- FIG_DATA_ALPHA = 0.6
- FIG_DATA_SIZE = 35
- # === Load CSV ===
- df = pd.read_csv(CSV_FILENAME, sep=CSV_SEPARATOR)
- # === Clean column names ===
- df.columns = [col.strip() for col in df.columns]
- print(df.columns)
- # === Drop missing data & provide filtering ===
- df = df.dropna(subset=[COL_DIAMETER, COL_VELOCITY, COL_TEMPERATURE])
- df = df[df[COL_VELOCITY] >= LIM_VELOCITY_MIN]
- df = df[(df[COL_DIAMETER] >= LIM_DIAMETER_MIN) & (df[COL_DIAMETER] <= LIM_DIAMETER_MAX)]
- # Convert timestamp
- df['Timestamp'] = pd.to_datetime(df['Timestamp'])
- df = df.set_index('Timestamp')
- # === Hourly resampling ===
- hourly = (
- df
- .resample("1h")
- .agg({
- COL_TEMPERATURE: "mean",
- COL_DIAMETER: "count"
- })
- .rename(columns={COL_DIAMETER: "Particle Count"})
- )
- hourly['Temperature [C]'] = hourly['Temperature [C]'].interpolate(method='linear')
- # =====================================
- # 📈 Create the plot (temperature + particles)
- # =====================================
- fig, ax1 = plt.subplots(figsize=(9, 6))
- ax2 = ax1.twinx()
- ax1.plot(hourly.index, hourly['Temperature [C]'], color='tab:red', label='Temperature [°C]', linewidth=2)
- ax1.set_ylabel('Temperature [°C]', color='tab:red')
- ax1.tick_params(axis='y', labelcolor='tab:red')
- ax2.bar(hourly.index, hourly['Particle Count'], width=0.03, alpha=0.4, color='tab:blue', label='Particles per hour')
- ax2.set_ylabel('Particle count per hour', color='tab:blue')
- ax2.tick_params(axis='y', labelcolor='tab:blue')
- # === Formátovanie timestamp na dd.mm.yyyy ===
- ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%Y'))
- fig.autofmt_xdate() # otočí dátumy, aby sa neprekrývali
- plt.title(FIG_TITLE)
- # === ZDIEĽANÁ LEGENDA ===
- lines1, labels1 = ax1.get_legend_handles_labels()
- lines2, labels2 = ax2.get_legend_handles_labels()
- ax1.legend(lines1 + lines2, labels1 + labels2, loc='upper left', frameon=True)
- plt.grid(True, linestyle=FIG_GRID_STYLE)
- plt.tight_layout()
- plt.show()
|