import holoviews as hv
import pandas as pd
import panel as pn
import panel.widgets as pnw
hv.extension("bokeh")
df = pd.DataFrame(
{
"horsepower": [95, 110, 140, 165, 88, 120],
"mpg": [30, 27, 22, 18, 34, 25],
"weight": [2100, 2350, 2800, 3200, 1900, 2600],
"origin": ["Europe", "Japan", "USA", "USA", "Japan", "Europe"],
}
)
columns = sorted(df.columns)
discrete = [x for x in columns if df[x].dtype == object]
continuous = [x for x in columns if x not in discrete]
quantileable = continuous
x = pnw.Select(name="X-Axis", value="horsepower", options=quantileable)
y = pnw.Select(name="Y-Axis", value="mpg", options=quantileable)
size = pnw.Select(name="Size", value="weight", options=["None"] + quantileable)
color = pnw.Select(name="Color", value="origin", options=discrete)
@pn.depends(x.param.value, y.param.value, color.param.value, size.param.value)
def create_figure(x, y, color, size):
opts = dict(
cmap="Category10",
width=700,
height=450,
line_color="black",
tools=["hover"],
color=color,
)
if size != "None":
opts["size"] = hv.dim(size).norm() * 30
return hv.Points(df, [x, y], label=f"{x.title()} vs {y.title()}").opts(**opts)
widgets = pn.WidgetBox(x, y, color, size, width=200)
pn.Row(widgets, create_figure) Click Run or press shift + ENTER to run code