44import matplotlib as mpl
55from scipy .interpolate import make_interp_spline , BSpline
66
7+ from logic .logic import get_advanced_hourly_forecast , get_day_of_week_capitalized , get_today
8+
79INTERPOLATION_COEFFICIENT = 20
810
911mpl .rcParams ['axes.labelsize' ] = 18
@@ -16,84 +18,85 @@ def interpolate_line(x, y, k_coeff):
1618 x_arr = np .array (x ).astype (float )
1719 y_arr = np .array (y ).astype (float )
1820
19- new_x = np .linspace (x_arr .min (), x_arr .max (), INTERPOLATION_COEFFICIENT * x_arr .size )
21+ x_range = np .arange (len (x ))
22+
23+ spline = make_interp_spline (x_range , y_arr , k = k_coeff )
24+
25+ x_smooth = np .linspace (0 , len (x_arr ) - 1 , INTERPOLATION_COEFFICIENT * x_arr .size )
2026
21- spline = make_interp_spline (x_arr , y_arr , k = k_coeff )
22- y_smooth = spline (new_x )
27+ y_smooth = spline (x_smooth )
2328
24- return new_x , y_smooth
29+ interpolated_x_in_x = np . interp ( x_smooth , x_range , x_arr )
2530
31+ return interpolated_x_in_x , y_smooth
2632
27- def graph_temperature (canvas , hours ):
28- temperatures = [- 4.5 , - 2. , 0. , 5. , 2. , 1. , - 0.3 , 1.7 , 1.9 , 6.9 , 7.8 , 3.0 ]
29- temperatures_felt = [- 5.3 , - 3.2 , - 1.0 , 2.0 , 1.0 , - 0.8 , 1.3 , - 1.5 , 0.1 , 4.2 , 5.0 , 3.8 ]
3033
31- _ , temperatures_interp = interpolate_line (hours , temperatures , 3 )
32- hours_extended , temperatures_felt_interp = interpolate_line (hours , temperatures_felt , 3 )
34+ def graph_temperature (canvas , hours , temperatures , temperatures_feel ):
3335
34- canvas .plot (hours_extended , temperatures_interp , 'r' , label = "Temperatura rzeczywista" , linewidth = 4.0 )
35- canvas .plot (hours_extended , temperatures_felt_interp , 'b' , label = "Temperatura odczuwalna" , linewidth = 4.0 )
36- canvas .plot (np .array (hours ).astype (float ), temperatures , 'r.' , markersize = 15 )
37- canvas .plot (np .array (hours ).astype (float ), temperatures_felt , 'b.' , markersize = 15 )
36+ canvas .plot (hours , temperatures , 'r' , label = "Temperatura rzeczywista" , linewidth = 4.0 )
37+ canvas .plot (hours , temperatures_feel , 'b' , label = "Temperatura odczuwalna" , linewidth = 4.0 )
3838 canvas .legend (fontsize = 14 )
3939
40- canvas .set_title ('Poniedziałek, 24.06' )
4140 canvas .set_ylabel ('Temperatura [°C]' )
4241
43- canvas .set_xlim ( float ( hours [ 0 ]), float ( hours [ - 1 ]) )
42+ canvas .margins ( x = 0.0 )
4443
4544 canvas .grid (True )
4645
4746
48- def graph_rainfall (canvas , hours ):
49-
50- rainfall = [11 , 12 , 10 , 15 , 20 , 10 , 7 , 0 , 1 , 3 , 3 , 6 ]
51- humidity = [0 , 0 , 0 , 0 , 15 , 20 , 32 , 11 , 8 , 4 , 0 , 1 ]
52-
53- hours_interp , humidity_interp = interpolate_line (np .array (hours ).astype (float ), np .array (humidity ).astype (float ), 3 )
47+ def graph_rainfall (canvas , hours , rainfall , humidity ):
5448
5549 secondary_canvas = canvas .twinx ()
56- secondary_canvas .plot (hours_interp , humidity_interp , color = '#ff9914' , linewidth = 4.0 )
57- canvas .bar (np .array (hours ).astype (float ), rainfall , width = 0.7 , color = '#29bf12' , edgecolor = '#006400' , linewidth = 2.0 )
50+ secondary_canvas .plot (hours , humidity , color = '#ff9914' , linewidth = 4.0 )
51+ canvas .bar (hours [:- 1 ], rainfall [:- 1 ], width = 0.8 , color = '#29bf12' , edgecolor = '#006400' , linewidth = 4.0 , align = 'edge' )
52+ canvas .set_ylim (bottom = 0 )
5853
59- canvas .set_xlim ( float ( hours [ 0 ]), float ( hours [ - 1 ]) )
54+ canvas .margins ( x = 0.0 )
6055
6156 canvas .set_ylabel ('Opady [mm/h]' )
6257 secondary_canvas .set_ylabel ('Wilgotność [%]' )
6358 canvas .grid (True )
6459
6560
66- def graph_wind (canvas , hours ):
61+ def graph_wind (canvas , hours , wind_speed , wind_gust ):
6762
68- wind_speed = [ 0 , 5 , 6 , 18 , 3 , 20 , 25 , 41 , 20 , 11 , 6 , 2 ]
69- wind_gust = [ 5 , 11 , 21 , 28 , 27 , 29 , 45 , 47 , 26 , 17 , 10 , 5 ]
63+ canvas . plot ( hours , wind_speed , linewidth = 3.0 , color = '#4189e8' )
64+ canvas . hlines ( wind_gust , xmin = [ i - 0.2 for i , x in enumerate ( hours )], xmax = [ i + 0.2 for i , x in enumerate ( hours )], colors = 'red' , label = 'Podmuchy wiatru' )
7065
71- hours_interp , wind_interp = interpolate_line (hours , wind_speed , 3 )
72-
73- canvas .plot (hours_interp , wind_interp , linewidth = 3.0 , color = '#4189e8' )
74- canvas .plot (np .array (hours ).astype (float ), wind_speed , '.' , markersize = 15 , color = '#4189e8' )
75- canvas .errorbar (np .array (hours ).astype (float ), wind_gust , xerr = 0.45 , fmt = 'none' , color = '#ed3a37' )
76-
77- canvas .set_xlim (float (hours [0 ]), float (hours [- 1 ]))
66+ canvas .margins (x = 0.0 )
7867
7968 canvas .set_ylabel ('Prędkość wiatru [km/h]' )
8069
8170 secondary_canvas = canvas .twinx ()
82- secondary_canvas .plot (hours_interp , [x * 1000 / 3600 for x in wind_interp ], linewidth = 3.0 , color = '#4189e8' )
71+ secondary_canvas .plot (hours , [x * 1000 / 3600 for x in wind_speed ], linewidth = 3.0 , color = '#4189e8' )
8372 secondary_canvas .set_ylim (canvas .get_ylim ()[0 ] * 1000 / 3600 , canvas .get_ylim ()[1 ] * 1000 / 3600 )
8473 secondary_canvas .set_ylabel ('Prędkość wiatru [m/s]' )
8574
8675 canvas .grid (True )
8776
8877
8978def generate_graphs_image ():
90- figure , axes = plt . subplots ( 3 , 1 , figsize = ( 10 , 9 ) )
79+ forecast = get_advanced_hourly_forecast ( )
9180
92- hours = [ '10' , '11' , '12' , '13' , '14' , '15' , '16' , '17' , '18' , '19' , '20' , '21' ]
81+ figure , axes = plt . subplots ( 3 , 1 , figsize = ( 10 , 9 ))
9382
94- graph_temperature (axes [0 ], hours )
95- graph_rainfall (axes [1 ], hours )
96- graph_wind (axes [2 ], hours )
83+ hours = forecast ['hours' ]
84+
85+ axes [0 ].set_title (f'{ get_day_of_week_capitalized ()} , { get_today ()} ' )
86+
87+ graph_temperature (
88+ axes [0 ], hours ,
89+ temperatures = forecast ['temperature' ],
90+ temperatures_feel = forecast ['temperature_feel' ])
91+ graph_rainfall (
92+ axes [1 ], hours ,
93+ rainfall = forecast ['rainfall' ],
94+ humidity = forecast ['humidity' ])
95+ graph_wind (
96+ axes [2 ], hours ,
97+ wind_speed = forecast ['wind_speed' ],
98+ wind_gust = forecast ['wind_gust' ]
99+ )
97100
98101 plt .tight_layout ()
99102 plt .savefig ('./assets/generated_images/graphs.png' , pil_kwargs = {'compress_level' : 7 })
0 commit comments