def make_basic_timeseries(source, object_name, variable, interval_label,
                          plot_width):
    """
    Make a basic timeseries plot (with either a step or line)
    and add a hover tool.

    Parameters
    ----------
    source : bokeh.models.ColumnDataSource
        The datasource with 'timestamp' and 'value' columns that will be
        plotted.
    object_name : str
        Name of the object to be plotted so an appropriate title can be made.
    variable : str
        Variable of the plotted object
    interval_label : str
        Interval label of the object to determine whether a line or step
        is most appropriate.
    plot_width : int
        The width of the output figure

    Returns
    -------
    bokeh.models.Figure
        The figure with the timeseries

    Raises
    ------
    KeyError
       If timestamp is not a column in source
    IndexError
       If the timestamp column is empty
    """
    timestamps = source.data['timestamp']
    first = pd.Timestamp(timestamps[0], tz='UTC')
    last = pd.Timestamp(timestamps[-1], tz='UTC')
    plot_method, plot_kwargs, hover_kwargs = plot_utils.line_or_step(
        interval_label)
    figure_title = build_figure_title(object_name, first, last)
    fig = figure(title=figure_title,
                 sizing_mode='scale_width',
                 plot_width=plot_width,
                 plot_height=300,
                 x_range=(first, last),
                 x_axis_type='datetime',
                 tools='pan,wheel_zoom,box_zoom,zoom_in,zoom_out,reset,save',
                 toolbar_location='above',
                 min_border_bottom=50)
    getattr(fig, plot_method)(x='timestamp',
                              y='value',
                              source=source,
                              **plot_kwargs)
    fig.yaxis.axis_label = plot_utils.format_variable_name(variable)
    fig.xaxis.axis_label = 'Time (UTC)'
    if variable == 'event':
        fig.yaxis.ticker = [0, 1]
        fig.yaxis.major_label_overrides = {1: 'True', 0: 'False'}
    add_hover_tool(fig, source, **hover_kwargs)
    return fig
def generate_probabilistic_forecast_figure(forecast,
                                           data,
                                           limit=pd.Timedelta('3d')):
    """
    Creates a plotly figure spec from api response for a probabilistic forecast
    group.

    Parameters
    ----------
    forecast : datamodel.ProbabilisticForecast
    data : pandas.DataFrame
        DataFrame with forecast values in each column, column names as the
        constant values and a datetime index.
    limit : pandas.Timedelta or None

    Returns
    -------
    None
        When the data is empty.
    figure: Plotly.graph_objects.Figure
        Plotly json specification for the plot.
    """
    logger.info('Starting probabilistic forecast figure generation...')
    if len(data.index) == 0:
        return None

    fig = go.Figure()
    if 'x' in forecast.axis:
        ylabel = 'Probability (%)'
        _plot_probabilsitic_distribution_axis_x(fig, forecast, data)
    else:
        ylabel = plot_utils.format_variable_name(forecast.variable)
        _plot_probabilsitic_distribution_axis_y(fig, forecast, data)
    fig.update_xaxes(title_text=f'Time (UTC)',
                     showgrid=True,
                     gridwidth=1,
                     gridcolor='#CCC',
                     showline=True,
                     linewidth=1,
                     linecolor='black',
                     ticks='outside')
    fig.update_yaxes(title_text=ylabel,
                     showgrid=True,
                     gridwidth=1,
                     gridcolor='#CCC',
                     showline=True,
                     linewidth=1,
                     linecolor='black',
                     ticks='outside',
                     fixedrange=True)
    first = data.index[0]
    last = data.index[-1]
    fig.update_layout(
        title=build_figure_title(forecast.name, first, last),
        legend=dict(font=dict(size=10)),
        **PLOTLY_LAYOUT_DEFAULTS,
    )
    return fig
Ejemplo n.º 3
0
def test_format_variable_name(var, exp):
    out = utils.format_variable_name(var)
    assert out == exp