Bokeh#
This notebook discusses how to use the HivePlot class with the bokeh visualization back end.
Note: the bokeh viz back end requires that Hiveplotlib be installed with extra packages, which can be done by running:
pip install hiveplotlib[bokeh]
[1]:
from bokeh.io import output_notebook
from bokeh.models import ColorBar
from bokeh.plotting import show
from bokeh.resources import INLINE
from bokeh.transform import linear_cmap
from hiveplotlib.datasets import example_hive_plot
output_notebook(resources=INLINE)
Change Plotting Kwargs For Nodes, Edges, and Axes#
By default, Hiveplotlib viz keeps all colors black, with standardized sizes that should cover most users’ needs:
[2]:
hp = example_hive_plot(backend="bokeh")
fig = hp.plot()
fig.title = "Base Bokeh Hive Plot Viz"
show(fig)
All of these defaults, however, can be modified. Below, we modify every color and size to serve as a reference for how to change these defaults with the bokeh back end:
[3]:
fig = hp.plot(
# node kwarg changes
node_kwargs={
"size": 15,
"fill_color": None, # empty node fill color
"line_color": "blue",
"line_width": 2,
"alpha": 0.4,
},
# axes label kwarg changes
axes_labels_fontsize="32px",
label_kwargs={
"text_color": "purple",
"text_font_style": "bold italic",
},
# axes kwarg changes
axes_kwargs={
"line_width": 10,
"color": "yellow",
"line_alpha": 0.9,
},
# edge kwarg changes
color="red",
line_width=3,
alpha=0.1,
)
show(fig)
Plotting Node Metadata#
Users may want to visualize nodes with respect to node metadata. For more on visualizing node metadata, see the Visualizing Node Metadata page.
Below, we demonstrate modifying nodes according to metadata for both size and color.
Note that when coloring nodes by data values with the bokeh back end, we have to first transform the chosen data variable, then pass that transformed variable through to color the nodes. We are using the bokeh.transform.linear_cmap() function for a linear transform, but we could choose other available transformations too.
[4]:
hp = example_hive_plot(
repeat_axes=True,
backend="bokeh",
)
# scale the low variable to make more useful sizes in viz
hp.nodes.data["size"] = hp.nodes.data["low"].to_numpy() * 2
# propagate extra node data changes through to data on axes
hp.update_partition_data()
# create a color mapper based on data values
color_data_field = "low"
mapper = linear_cmap(
field_name=color_data_field,
palette="Magma256",
low=0,
high=10,
)
node_kwargs = {
"line_color": "black",
"fill_color": mapper, # based on "low" var in node data, but must be created above
"size": "size", # reference to "size" var in node data
}
hp.update_node_viz_kwargs(**node_kwargs)
# when including color bar, make wider to maintain 1:1 aspect
fig = hp.plot(
fig_kwargs={"width": 650, "height": 600},
)
color_bar = ColorBar(
color_mapper=mapper["transform"],
width=8,
title=f"Node Variable '{color_data_field}'",
)
fig.add_layout(color_bar, "right")
show(fig)
Plotting Edge Metadata#
Users may want to visualize edges with respect to edge metadata. For more on visualizing edge metadata, see the Visualizing Edge Metadata page.
Below, we demonstrate modifying edges according to metadata for both line width and color.
Note that when coloring edges by data values with the bokeh back end, we have to first transform the chosen data variable, then pass that transformed variable through to color the edges. We are using the bokeh.transform.linear_cmap() function for a linear transform, but we could choose other available transformations too.
[5]:
hp = example_hive_plot(
repeat_axes=True,
backend="bokeh",
)
# scale the low variable to make more useful line widths in viz
hp.edges.data["linewidth"] = hp.edges.data["low"] / 3
# create a color mapper based on data values
color_data_field = "low"
mapper = linear_cmap(
field_name=color_data_field,
palette="Cividis256",
low=0,
high=10,
)
edge_kwargs = {
"color": mapper, # based on "low" var in edge data, but must be created above
"line_width": "linewidth", # reference to "linewidth" variable in edge data
}
hp.update_edge_plotting_keyword_arguments(**edge_kwargs)
# when including color bar, make wider to maintain 1:1 aspect
fig = hp.plot(
fig_kwargs={"width": 650, "height": 600},
)
color_bar = ColorBar(
color_mapper=mapper["transform"],
width=8,
title=f"Edge Variable '{color_data_field}'",
)
fig.add_layout(color_bar, "right")
show(fig)
Hover Information#
We encourage users to review the high-level overview on adding hover information on the Hover Information page first.
Below, we summarize the unique behavior with the bokeh back end when adding or modifying hover information.
Axis Hover Information#
With the bokeh back end, axis hover information shows up when moving your mouse along the axis:
[6]:
hp = example_hive_plot(backend="bokeh")
fig = hp.plot(hover="axes")
show(fig)
Axis-Specific Metadata#
When a subset of axes has added metadata not available on other axes, all axes will show that metadata variable, but the axes without the variable will display the value N/A.
Below, we demonstrate this by adding a new metadata variable to only axis A:
[7]:
hp = example_hive_plot(backend="bokeh")
# add metadata value to just axis A
hp.axes["A"].add_metadata({"Special Axis Data": "My Special 'A' Value"})
fig = hp.plot(hover="axes")
show(fig)