# -*- coding: utf-8 -*-
import dash
import dash_html_components as html
from plotly.data import iris

import holoviews as hv
from holoviews.plotting.plotly.dash import to_dash

# Load dataset
df = iris()
dataset = hv.Dataset(df)

scatter = hv.Scatter(dataset, kdims=["sepal_length"], vdims=["sepal_width"])
hist = hv.operation.histogram(dataset, dimension="petal_width", normed=False)

app = dash.Dash(__name__)
components = to_dash(app, [scatter, hist])

app.layout = html.Div(components.children)

if __name__ == "__main__":
    app.run_server(debug=True)
示例#2
0
# Load iris dataset and replicate with noise to create large dataset
from plotly.data import iris
import numpy as np
import pandas as pd
df_original = iris()[[
    "sepal_length", "sepal_width", "petal_length", "petal_width"
]]
df = pd.concat([
    df_original + np.random.randn(*df_original.shape) * 0.1
    for i in range(10000)
])

# Build HoloViews Dataset from large Pandas DataFrame
import holoviews as hv
from holoviews.plotting.plotly.dash import to_dash
dataset = hv.Dataset(df)

# Build Datashaded Scatter and Histogram HoloViews Elements, and side-by-side layout
from holoviews.operation.datashader import datashade
scatter = datashade(
    hv.Scatter(dataset, kdims=["sepal_length"], vdims=["sepal_width"]))
hist = hv.operation.histogram(dataset, dimension="petal_width", normed=False)
row = scatter + hist

# Link selections across subplots
linked_row = hv.selection.link_selections(row)

# Set plot title
linked_row.opts(title="Datashader with %d points" % len(dataset))

# Build Dash app
def _plotly_hooks(plot, element):
    """Used by HoloViews to give plots plotly plots special treatment"""
    fig = plot.state
    # Use plot hook to set the default drag mode to box selection
    fig["layout"]["dragmode"] = "select"

    fig["layout"]["autosize"] = True
    fig["config"]["responsive"] = True
    fig["config"]["displayModeBar"] = True
    if isinstance(element, hv.Histogram):
        # Constrain histogram selection direction to horizontal
        fig["layout"]["selectdirection"] = "h"


IRIS_DATASET = iris()
ACCENT_COLOR = "#E1477E"
OPTS: Dict[str, Dict[str, Any]] = {
    "all": {
        "scatter": {
            "color": ACCENT_COLOR,
            "responsive": True,
            "size": 10
        },
        "hist": {
            "color": ACCENT_COLOR,
            "responsive": True
        },
    },
    "bokeh": {
        "scatter": {