bokeh.models.BooleanFilter

Here are the examples of the python api bokeh.models.BooleanFilter taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

4 Examples 7

0 Source : visualization.py
with MIT License
from pedromartins4

def plot_stock_price(stock):
    p = figure(x_axis_type="datetime", plot_width=WIDTH_PLOT, plot_height=400,
               title="Stock price + Bollinger Bands (2 std)",
               tools=TOOLS, toolbar_location='above')

    inc = stock.data['close'] > stock.data['open']
    dec = stock.data['open'] > stock.data['close']
    view_inc = CDSView(source=stock, filters=[BooleanFilter(inc)])
    view_dec = CDSView(source=stock, filters=[BooleanFilter(dec)])

    width = 35000000

    p.segment(x0='date', x1='date', y0='low', y1='high', color=RED, source=stock, view=view_inc)
    p.segment(x0='date', x1='date', y0='low', y1='high', color=GREEN, source=stock, view=view_dec)

    p.vbar(x='date', width=width, top='open', bottom='close', fill_color=RED, line_color=RED,
           source=stock,
           view=view_inc)
    p.vbar(x='date', width=width, top='open', bottom='close', fill_color=GREEN, line_color=GREEN,
           source=stock,
           view=view_dec)

    # p.line(x='date', y='close_line', line_width=1, color=BLUE, line_alpha=0.7, souce=stock)

    band = Band(base='date', lower='bolling_lower', upper='bolling_upper', source=stock, level='underlay',
                fill_alpha=0.5, line_width=1, line_color='black', fill_color=BLUE_LIGHT)
    p.add_layout(band)

    code = """
    def ticker():
        return "{:.0f} + {:.2f}".format(tick, tick % 1)
    """
    p.yaxis.formatter = NumeralTickFormatter(format='$ 0,0[.]000')

    return p


# Simple Moving Average
def plot_sma(stock):

0 Source : visualization.py
with MIT License
from pedromartins4

def plot_macd(stock):
    p = figure(x_axis_type="datetime", plot_width=WIDTH_PLOT, plot_height=200, title="MACD (line + histogram)",
               tools=TOOLS, toolbar_location='above')

    up = [True if val > 0 else False for val in stock.data['macd_histogram']]
    down = [True if val   <   0 else False for val in stock.data['macd_histogram']]

    view_upper = CDSView(source=stock, filters=[BooleanFilter(up)])
    view_lower = CDSView(source=stock, filters=[BooleanFilter(down)])
    p.vbar(x='date', top='macd_histogram', bottom='zeros', width=30000000, color=GREEN, source=stock, view=view_upper)
    p.vbar(x='date', top='zeros', bottom='macd_histogram', width=30000000, color=RED, source=stock, view=view_lower)

    # Adding an extra range for the MACD lines, because using the same axis as the histogram
    # sometimes flattens them too much
    p.extra_y_ranges = {'macd': DataRange1d()}
    p.add_layout(LinearAxis(y_range_name='macd'), 'right')

    p.line(x='date', y='macd', line_width=2, color=BLUE, source=stock, legend='MACD', muted_color=BLUE,
           muted_alpha=0, y_range_name='macd')
    p.line(x='date', y='macd_signal', line_width=2, color=BLUE_LIGHT, source=stock, legend='Signal',
           muted_color=BLUE_LIGHT, muted_alpha=0, y_range_name='macd')

    p.legend.location = "bottom_left"
    p.legend.border_line_alpha = 0
    p.legend.background_fill_alpha = 0
    p.legend.click_policy = "mute"

    p.yaxis.ticker = []
    p.yaxis.axis_line_alpha = 0

    return p


# RSI
def plot_rsi(stock):

0 Source : bokeh_figures.py
with MIT License
from SolarArbiter

def timeseries(timeseries_value_cds, timeseries_meta_cds,
               start, end, units, timezone='UTC'):
    """
    Timeseries plot of one or more forecasts and observations.

    Parameters
    ----------
    timeseries_value_cds: bokeh.models.ColumnDataSource
        ColumnDataSource of timeseries data. See :py:func:`solarforecastarbiter.reports.reoports.figures.construct_timeseries_cds` for format.
    timeseries_meta_cds: bokeh.models.ColumnDataSource
        ColumnDataSource of metadata for each Observation Forecast pair. See :py:func:`solarforecastarbiter.reports.reoports.figures.construct_timeseries_cds` for format.
    start : pandas.Timestamp
        Report start time
    end : pandas.Timestamp
        Report end time
    timezone : str
        Timezone consistent with the data in the obs_fx_cds.

    Returns
    -------
    fig : bokeh.plotting.figure
    """  # NOQA
    palette = cycle(PALETTE)

    fig = figure(
        sizing_mode='scale_width', plot_width=900, plot_height=300,
        x_range=(start, end), x_axis_type='datetime',
        tools='pan,xwheel_zoom,box_zoom,box_select,lasso_select,reset,save',
        name='timeseries')

    plotted_objects = 0
    for obs_hash in np.unique(timeseries_meta_cds.data['observation_hash']):
        metadata = _extract_metadata_from_cds(
            timeseries_meta_cds, obs_hash, 'observation_hash')
        pair_indices = _boolean_filter_indices_by_pair(
            timeseries_value_cds, metadata['pair_index'])
        view = CDSView(source=timeseries_value_cds, filters=[
            BooleanFilter(pair_indices)
        ])
        plot_method, plot_kwargs, hover_kwargs = line_or_step(
            metadata['interval_label'])
        legend_label = metadata['observation_name']
        color = metadata['observation_color']
        getattr(fig, plot_method)(
            x='timestamp', y='observation_values', source=timeseries_value_cds,
            view=view, color=color, legend_label=legend_label,
            **plot_kwargs)
        plotted_objects += 1

    for fx_hash in np.unique(timeseries_meta_cds.data['forecast_hash']):
        metadata = _extract_metadata_from_cds(
            timeseries_meta_cds, fx_hash, 'forecast_hash')
        pair_indices = _boolean_filter_indices_by_pair(
            timeseries_value_cds, metadata['pair_index'])
        view = CDSView(source=timeseries_value_cds,
                       filters=[BooleanFilter(pair_indices)])
        plot_method, plot_kwargs, hover_kwargs = line_or_step(
            metadata['interval_label'])
        legend_label = metadata['forecast_name']
        color = next(palette)
        getattr(fig, plot_method)(
            x='timestamp', y='forecast_values', source=timeseries_value_cds,
            view=view, color=color, legend_label=legend_label,
            **plot_kwargs)
        plotted_objects += 1

    fig.legend.location = "top_left"
    fig.legend.click_policy = "hide"
    if plotted_objects > 10:
        fig.legend.label_height = 10
        fig.legend.label_text_font_size = '8px'
        fig.legend.glyph_height = 10
        fig.legend.spacing = 1
        fig.legend.margin = 0
    fig.xaxis.axis_label = f'Time ({timezone})'
    fig.yaxis.axis_label = f'Data ({units})'
    return fig


def _get_scatter_limits(cds):

0 Source : bokeh_figures.py
with MIT License
from SolarArbiter

def scatter(timeseries_value_cds, timeseries_meta_cds, units):
    """
    Scatter plot of one or more forecasts and observations.

    Parameters
    ----------
    timeseries_value_cds: bokeh.models.ColumnDataSource
        ColumnDataSource of timeseries data. See
        :py:func:`solarforecastarbiter.reports.reoports.figures.construct_timeseries_cds`
        for format.
    timeseries_meta_cds: bokeh.models.ColumnDataSource
        ColumnDataSource of metadata for each Observation Forecast pair. See
        :py:func:`solarforecastarbiter.reports.reoports.figures.construct_timeseries_cds`
        for format.

    Returns
    -------
    fig : bokeh.plotting.figure
    """  # NOQA
    xy_min, xy_max = _get_scatter_limits(timeseries_value_cds)

    # match_aspect=True does not work well, so these need to be close
    plot_height = 400
    # width will be updated later based on label length
    plot_width = plot_height + 50
    fig = figure(
        plot_width=plot_width, plot_height=plot_height, match_aspect=True,
        x_range=Range1d(xy_min, xy_max), y_range=Range1d(xy_min, xy_max),
        tools='pan,wheel_zoom,box_zoom,box_select,lasso_select,reset,save',
        name='scatter')

    kwargs = dict(size=6, line_color=None)

    palette = cycle(PALETTE)

    # accumulate labels and plot objects for manual legend
    scatters_labels = []
    for fxhash in np.unique(timeseries_meta_cds.data['forecast_hash']):
        metadata = _extract_metadata_from_cds(
            timeseries_meta_cds, fxhash, 'forecast_hash')
        pair_indices = _boolean_filter_indices_by_pair(
            timeseries_value_cds, metadata['pair_index'])
        view = CDSView(source=timeseries_value_cds,
                       filters=[BooleanFilter(pair_indices)])
        label = metadata['forecast_name']
        r = fig.scatter(
            x='observation_values', y='forecast_values',
            source=timeseries_value_cds, view=view,
            fill_color=next(palette), **kwargs)
        scatters_labels.append((label, [r]))

    # manual legend so it can be placed outside the plot area
    legend = Legend(items=scatters_labels, location='top_center',
                    click_policy='hide')
    fig.add_layout(legend, 'right')

    # compute new plot width accounting for legend label text width.
    # also considered using second figure for legend so it doesn't
    # distort the first when text length/size changes. unfortunately,
    # that doesn't work due to bokeh's inability to communicate legend
    # information across figures.
    # widest part of the legend
    max_legend_length = max((len(label) for label, _ in scatters_labels))
    px_per_length = 7.75  # found through trial and error
    fig.plot_width = int(fig.plot_width + max_legend_length * px_per_length)

    label = f'({units})'
    fig.xaxis.axis_label = 'Observed ' + label
    fig.yaxis.axis_label = 'Forecast ' + label
    return fig


def construct_metrics_cds(metrics, rename=None):