#!/usr/bin/python # -*- coding: utf-8 -*- import matplotlib.pyplot as plt import numpy as np import matplotlib.dates as mdates from datetime import datetime, timedelta from astral import LocationInfo from astral.sun import sun def main(): # Set your location city = LocationInfo("Rome", "Italy", "Europe/Rome", 44.448066, 11.270639) # Choose the date # date = datetime(2023, 3, 29) # dawn: 2023-03-29 06:32:45.166761+02:00 # sunrise: 2023-03-29 07:02:31.106176+02:00 # noon: 2023-03-29 13:19:52+02:00 # sunset: 2023-03-29 19:37:45.931604+02:00 # dusk: 2023-03-29 20:07:37.090448+02:00 date = datetime(2023, 3, 29) # Get solar times s = sun(city.observer, date=date, tzinfo=city.timezone) for k, v in s.items(): print(f'{k}: {v}') dawn = s['dawn'] sunrise = s['sunrise'] sunset = s['sunset'] dusk = s['dusk'] # Generate time series data for one day times = [date + timedelta(minutes=15 * i) for i in range(96 * 1)] values = np.sin(np.linspace(0, 4 * np.pi, len(times))) # Some mock data # Create the plot fig, ax = plt.subplots(figsize=(10, 5)) ax.plot(times, values, label="Sensor Data", color='tab:blue') # Plot twilight periods as vertical bands ax.axvspan(dawn, sunrise, color='lightskyblue', alpha=0.3, label='Morning Twilight') ax.axvspan(sunset, dusk, color='lightskyblue', alpha=0.3, label='Evening Twilight') # Mark sunrise and sunset ax.axvline(sunrise, color='orange', linestyle='--', label='Sunrise') ax.axvline(sunset, color='red', linestyle='--', label='Sunset') # Formatting ax.set_title(f"Daylight and Twilight - {city.name} on {date.strftime('%Y-%m-%d')}") ax.set_xlabel("Time") ax.set_ylabel("Sensor Value") ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) plt.xticks(rotation=45) plt.legend() plt.tight_layout() plt.show() if __name__ == '__main__': main()