-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_slope_plot.py
66 lines (52 loc) · 2.37 KB
/
create_slope_plot.py
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
import pandas as pd
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool, BoxAnnotation, LinearAxis, Range1d, DatetimeTickFormatter
from datetime import timedelta
def find_sundays_in_range(start_date, end_date):
sundays = []
current_date = start_date
while current_date <= end_date:
if current_date.day_name() == 'Sunday':
sundays.append(current_date)
current_date += timedelta(days=1)
return sundays
def create_slope_plot(df, start_date, end_date, show_sunday_marker, x_range):
# Filter dataframe based on selected date range
filtered_df = df.loc[(df['date'] >= start_date) & (df['date'] < end_date)]
# Create Bokeh plot
plot = figure(title='Orgel', x_axis_label='Zeitstempel', width=1200, height=600,
x_axis_type='datetime', tools="pan,box_zoom,reset,save", x_range=x_range)
plot.yaxis.axis_label = '∆ah / ∆h [g/m³h]'
# x-Axis format
plot.xaxis.formatter = DatetimeTickFormatter(
hours=["%d %B %Y-%H:%M"],
days=["%d %B %Y-%H:%M"],
months=["%d %B %Y-%H:%M"],
years=["%d %B %Y-%H:%M"],
)
plot.xaxis.major_label_orientation = 3.1415 / 4
plot.xaxis.ticker.desired_num_ticks = 10
# Plot temperature
temp_slope_line = plot.line('date', 'ah_slope', source=ColumnDataSource(filtered_df), legend_label="ah/h", line_width=2, color='blue')
plot.add_tools(HoverTool(renderers=[temp_slope_line], tooltips=[
('Date', '@date{%F %H:%M}'),
('Temperature', '@ah_slope{0.2f} g/m³h')
], formatters={'@date': 'datetime'}, mode='vline'))
# Add Sunday markers
if show_sunday_marker:
sundays = find_sundays_in_range(start_date, end_date)
for sunday in sundays:
plot.add_layout(
BoxAnnotation(left=sunday, right=sunday + timedelta(days=1), fill_alpha=0.1, fill_color='green'))
# Style the plot
plot.legend.location = "top_left"
plot.legend.click_policy = "hide"
# Hide x-axis ticks
# plot.xaxis.major_tick_line_color = None # Hides major ticks
# plot.xaxis.minor_tick_line_color = None # Hides minor ticks
# Hide y-axis ticks
# plot.yaxis.major_tick_line_color = None # Hides major ticks
# plot.yaxis.minor_tick_line_color = None # Hides minor ticks
plot.xgrid.grid_line_color = None
plot.ygrid.grid_line_color = None
return plot