drakx

drakxtwo

aut viam inveniam aut faciam

Lee Halls

Xmas is a pretty dead time for me, so i finally got round to programming something to export the data from the sql db and then more importantly graph it.

I have struggled for ages trying to use matplotlib and was close to giving up then i stumbled across bokeh and magic happened. Miraculous, a gift at xmas time :-) in short a few simple lines of code and i now have a scalable graph of data that works on this site as well as the dashboard monitor. Below is an image of the output and the scalable graph can be found in the projects section.

/img/data_graph.png

The code used to produce the above is very simple even an idiot like me can get it to work :-) ;

from bokeh.plotting import figure, output_file, show, save
from bokeh.models import DatetimeTickFormatter, BoxAnnotation
from bokeh.embed import json_item

# output to static HTML file
output_file("lines.html")

# create a new plot with a title and axis labels
p = figure(title='A year of data - 2019', x_axis_label='Time',x_axis_type='datetime', y_axis_label='DegC',toolbar_location="above")
p.xaxis.formatter=DatetimeTickFormatter(
    hours="%H",
    minutes="%M",
    seconds="%S")


low_box = BoxAnnotation(top=15, fill_alpha=0.2, fill_color='blue')
mid_box = BoxAnnotation(bottom=15, top=25, fill_alpha=0.2, fill_color='green')
high_box = BoxAnnotation(bottom=25, fill_alpha=0.2, fill_color='red')

p.add_layout(low_box)
p.add_layout(mid_box)
p.add_layout(high_box)

# add a line renderer with legend and line thickness
p.line(x, data_ext, legend_label='External', line_color="blue", line_width=1)
p.line(x, data_fr, legend_label='Frontroom', line_color="red", line_width=1)
p.line(x, data_bd, legend_label='Bedroom', line_color="green", line_width=1)

# plot columns
# p = figure(plot_width=800, plot_height=400)
# p.vbar(x, width=0.5, bottom=0,
#        top=data_ext, color="firebrick")


show(p)
# save(p)

Recent posts

Post

See more

Categories

About

aut viam inveniam aut faciam