예제 #1
0
def make_choropleth(data_source):
    # converts data to right format
    geo_source = GeoJSONDataSource(geojson=data_source)
    # plots countries with colors
    TOOLS = "pan,wheel_zoom,reset,hover,save"
    p = figure(
        title="Grain prices 2015 in USD",
        x_axis_location=None,
        y_axis_location=None,
        width=1200,
        height=700,
        tools=TOOLS,
    )
    p.grid.grid_line_color = None
    p.title.align = "center"
    p.title.text_color = "#084594"
    p.title.text_font_size = "25px"
    p.background_fill_color = '#f7fbff'
    p.background_fill_alpha = 0.5

    p.patches(xs='xs',
              ys='ys',
              fill_color="fill",
              line_color='black',
              line_width=0.5,
              source=geo_source)
    palette = Blues8
    palette.reverse()
    mapper = LinearColorMapper(palette=palette, low=0, high=8 / 6)
    ticker = FixedTicker()
    ticker.ticks = [x / 6 for x in range(1, 8)]
    color_bar = ColorBar(color_mapper=mapper,
                         ticker=ticker,
                         label_standoff=12,
                         border_line_color=None,
                         location=(0, 0),
                         orientation="horizontal")

    p.add_layout(color_bar, 'below')

    # sets properties of hovertool
    hover = p.select_one(HoverTool)
    hover.point_policy = "follow_mouse"
    hover.tooltips = [("Country:", "@name"), ("Grain price:", "@price"),
                      ("Population:", "@pop"), ("GDP (USD):", "@bbp")]

    output_file("plots/choropleth_grain2015.html", title="World map Bokeh")
    show(p)
예제 #2
0
def timeline_days_hours(interval_frequency, all_co2_dataframe,
                        sensors_with_anomalies, data, upper_bound,
                        destination_path):
    times = pd.date_range(start='00:00:00',
                          end='23:55:00',
                          freq=str(interval_frequency) +
                          'Min').strftime('%H:%M:%S')
    days = all_co2_dataframe['timestamp'].dt.strftime('%Y-%m-%d').to_list()
    days = list(dict.fromkeys(days))
    timestamp = list(times)
    for sensor_name in sensors_with_anomalies:
        all_durations = []
        for k in data['anomalies']['anomaly_co2_values'][sensor_name][0]:
            k = data['anomalies']['anomaly_co2_values'][sensor_name][0].index(
                k)
            all_durations.append(
                int(data['anomalies']['anomaly_co2_values'][sensor_name][0][k]
                    ['duration']))
        data['anomalies']['anomaly_co2_values'][sensor_name][0]
        p = figure(plot_height=500,
                   plot_width=2000,
                   x_range=timestamp,
                   y_range=days,
                   title='Timeline of periods with CO2 levels higher than ' +
                   str(upper_bound) + ' ppm in ' + sensor_name +
                   '\n Based on data for last ' + str(len(days)) + ' days',
                   active_drag=None,
                   toolbar_location=None)
        p.x_range.range_padding = 0
        p.y_range.range_padding = 0
        p.title.text_font_size = '15pt'
        p.xaxis.axis_label_text_font_size = "15pt"
        p.yaxis.axis_label_text_font_size = "15pt"
        p.yaxis.major_label_text_font_size = '9pt'
        p.xaxis.major_label_text_font_size = '5pt'
        # set x axis to invisible
        p.xaxis.visible = False
        # Add custom axis with tickers labels only every 1 hour
        labels = np.arange(0, 288, 12).tolist()
        ticker = FixedTicker()
        ticker.ticks = labels
        xaxis = LinearAxis(ticker=ticker)
        xaxis.major_label_orientation = math.pi / 3
        p.add_layout(xaxis, 'below')

        xaxis.major_label_overrides = {
            0: '00:00',
            12: '01:00',
            24: '02:00',
            36: '3:00',
            48: '04:00',
            60: '05:00',
            72: '06:00',
            84: '07:00',
            96: '08:00',
            108: '09:00',
            120: '10:00',
            132: '11:00',
            144: '12:00',
            156: '13:00',
            168: '14:00',
            180: '15:00',
            192: '16:00',
            204: '17:00',
            216: '18:00',
            228: '19:00',
            240: '20:00',
            252: '21:00',
            264: '22:00',
            276: '23:00'
        }
        #add anomalies recorded for each day in considered period
        for i in data['anomalies']['anomaly_co2_values'][sensor_name][0]:
            i = data['anomalies']['anomaly_co2_values'][sensor_name][0].index(
                i)
            x = []
            y = []
            x.append(data['anomalies']['anomaly_co2_values'][sensor_name][0][i]
                     ['anomalies_details'][0][0][11:])
            x.append(data['anomalies']['anomaly_co2_values'][sensor_name][0][i]
                     ['anomalies_details'][-1][0][11:])
            y.append(data['anomalies']['anomaly_co2_values'][sensor_name][0][i]
                     ['anomalies_details'][0][0][0:10])
            y.append(data['anomalies']['anomaly_co2_values'][sensor_name][0][i]
                     ['anomalies_details'][-1][0][0:10])
            if data['anomalies']['anomaly_co2_values'][sensor_name][0][i][
                    'anomalies_details'][0][0][0:10] == data['anomalies'][
                        'anomaly_co2_values'][sensor_name][0][i][
                            'anomalies_details'][-1][0][0:10]:
                # print('yes')
                p.line(x,
                       y,
                       line_width=2,
                       color='blue',
                       legend_label='CO2 above critical value')
                p.circle(x, y, fill_color="blue", line_color='blue', size=5)
            else:
                x1 = x.copy()
                y1 = y.copy()
                x2 = x.copy()
                y2 = y.copy()
                x1[-1] = times[-1]
                y1[-1] = y1[0]
                p.line(x1, y1, line_width=2, color='blue')
                x2[0] = times[0]
                y2[0] = y2[-1]
                p.line(x2, y2, line_width=2, color='blue')
                x3 = [x1[0], x2[-1]]
                y3 = [y1[0], y2[-1]]
                p.circle(x3, y3, fill_color="blue", line_color='blue', size=5)

        #save graph in output location
        output_file(destination_path + '/anomalies_timeline_' + sensor_name +
                    '.html')
        save(p)
    return p