Skip to content

Instantly share code, notes, and snippets.

@ivirshup
Created September 4, 2018 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivirshup/79a530e62e9c1ab413b5a8456099f859 to your computer and use it in GitHub Desktop.
Save ivirshup/79a530e62e9c1ab413b5a8456099f859 to your computer and use it in GitHub Desktop.
Switching between categorical and continuous Dynamic Map coloring (not working)
import holoviews as hv
import numpy as np
import pandas as pd
hv.extension("bokeh")
# Generate data
base_df = pd.DataFrame({
"x": np.random.randn(100), "y": np.random.randn(100),
"cat1": np.random.randint(0, 5, 100), "cat2": np.random.randint(0, 5, 100),
"con1": np.random.randn(100), "con2": np.random.randn(100)})
base_df["cat1"], base_df["cat2"] = base_df["cat1"].astype(str), base_df["cat2"].astype(str).astype("category")
# Define callback
def gen_plot(symbol):
plotdf = pd.DataFrame({"x": base_df["x"], "y": base_df["y"], "color": base_df[symbol]})
return hv.Points(plotdf, vdims=["color"]).options(color_index="color")
# Show
hv.DynamicMap(gen_plot, kdims=["symbol"]).redim.values(symbol=["cat1", "cat2", "col1", "col2"])
# Note: I think there is some state here. Sometimes nothing happens if I choose a categorical coloring, sometimes all the points disappear
# Alternative, trying to be more explicit
def gen_plot2(symbol):
plotdf = pd.DataFrame({"x": base_df["x"], "y": base_df["y"], "color": base_df[symbol]})
if (plotdf["color"].dtype.name == "category") or (plotdf["color"].apply(type) == str).all():
cmap = "Colorblind"
else:
cmap= "Viridis"
return hv.Points(plotdf, vdims=["color"]).options(color_index="color", cmap=cmap)
# Show
hv.DynamicMap(gen_plot2, kdims=["symbol"]).redim.values(symbol=["cat1", "cat2", "con1", "con2"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment