Skip to content

Commit

Permalink
Add size to Scatter (bokeh#13958)
Browse files Browse the repository at this point in the history
* use clone() in graphs.py

* update more graphs

* fix customjs_for_tools example

* Update glyph_non_visual.py

* fix advanced example parallel_plot.py

* fix linter issues
  • Loading branch information
mosc9575 authored Sep 11, 2024
1 parent df57080 commit 360e38e
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 88 deletions.
33 changes: 13 additions & 20 deletions examples/advanced/extensions/parallel_plot/parallel_plot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import numpy as np

from bokeh.layouts import column
from bokeh.models import (BasicTickFormatter, ColumnDataSource,
CustomJSTickFormatter, Div, FixedTicker,
LinearAxis, LinearColorMapper, MultiLine, Range1d)
from bokeh.models import (BasicTickFormatter, ColumnDataSource, CustomJSTickFormatter,
Div, FixedTicker, LinearAxis, LinearColorMapper, Range1d)
from bokeh.plotting import figure
from bokeh.sampledata.autompg import autompg_clean as df

Expand Down Expand Up @@ -34,9 +33,9 @@ def parallel_plot(df, color=None, palette=None):
p = figure(x_range=(-1, ndims),
y_range=(0, 1),
width=1000,
tools="pan, box_zoom")
tools="pan,box_zoom,tap")

# Create x axis ticks from columns contained in dataframe
# Create x-axis ticks from columns contained in dataframe
fixed_x_ticks = FixedTicker(
ticks=np.arange(ndims), minor_ticks=[])
formatter_x_ticks = CustomJSTickFormatter(
Expand All @@ -47,7 +46,7 @@ def parallel_plot(df, color=None, palette=None):
p.yaxis.visible = False
p.y_range.start = 0
p.y_range.end = 1
p.y_range.bounds = (-0.1, 1.1) # add a little padding around y axis
p.y_range.bounds = (-0.1, 1.1) # add a little padding around y-axis
p.xgrid.visible = False
p.ygrid.visible = False

Expand All @@ -67,23 +66,16 @@ def parallel_plot(df, color=None, palette=None):
p.add_layout(LinearAxis(fixed_location=index, y_range_name=col,
ticker=fixedticks, formatter=tickformatter), 'right')

# create the data renderer ( MultiLine )
# specify selected and non selected style
# create the data renderer (MultiLine)
# specify selected and non-selected style
non_selected_line_style = dict(line_color='grey', line_width=0.1, line_alpha=0.5)

selected_line_style = dict(line_color={'field': 'color', 'transform': cmap}, line_width=1)

parallel_renderer = p.multi_line(
xs="xs", ys="ys", source=data_source, **non_selected_line_style)
parallel_renderer = p.multi_line(xs="xs", ys="ys", source=data_source, **non_selected_line_style)

# Specify selection style
selected_lines = MultiLine(**selected_line_style)

# Specify non selection style
nonselected_lines = MultiLine(**non_selected_line_style)

parallel_renderer.selection_glyph = selected_lines
parallel_renderer.nonselection_glyph = nonselected_lines
parallel_renderer.selection_glyph = parallel_renderer.glyph.clone(**selected_line_style)
parallel_renderer.nonselection_glyph = parallel_renderer.glyph.clone(**non_selected_line_style)
p.y_range.start = p.y_range.bounds[0]
p.y_range.end = p.y_range.bounds[1]

Expand All @@ -106,12 +98,13 @@ def parallel_plot(df, color=None, palette=None):
p.toolbar.active_drag = selection_tool
return p


if __name__ == '__main__':
from bokeh.io import show
from bokeh.palettes import Viridis256
del df['origin']
del df['mfr']
del df['name']
p = parallel_plot(df=df, color=df[df.columns[0]], palette=Viridis256)
plot = parallel_plot(df=df, color=df[df.columns[0]], palette=Viridis256)
div = Div(text="Select up and down column grid lines to define filters. Double click a filter to reset it.")
show(column(div, p))
show(column(div, plot))
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ParallelSelectionTool(BoxSelectTool):
""" Selection tool for parallel plot
To create a selection box, drag the selection around an axe
When hovering a selection the box can be dragged upside-down
Double click on a selection to remove it
Doubleclick on a selection to remove it
Escape key remove all selections
"""

Expand Down
11 changes: 8 additions & 3 deletions examples/interaction/js_callbacks/customjs_for_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const geometry = cb_obj.geometry
const data = source.data
// quad is forgiving if left/right or top/bottom are swappeed
// quad is forgiving if left/right or top/bottom are swapped
source.data = {
left: data.left.concat([geometry.x0]),
right: data.right.concat([geometry.x1]),
Expand All @@ -21,10 +21,15 @@
tools="box_select", x_range=(0, 1), y_range=(0, 1))

# using Quad model directly to control (non)selection glyphs more carefully
quad = Quad(left='left', right='right',top='top', bottom='bottom',
quad = Quad(left='left', right='right', top='top', bottom='bottom',
fill_alpha=0.3, fill_color='#009933')

p.add_glyph(source, quad, selection_glyph=quad, nonselection_glyph=quad)
p.add_glyph(
source,
quad,
selection_glyph=quad.clone(fill_color='blue'),
nonselection_glyph=quad.clone(fill_color='gray'),
)

p.js_on_event(SelectionGeometry, callback)

Expand Down
35 changes: 21 additions & 14 deletions examples/models/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@

G = nx.karate_club_graph()


def create_graph(layout_func, inspection_policy=None, selection_policy=None, **kwargs):

plot = Plot(width=400, height=400,
x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))
plot = Plot(width=400, height=400, x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
graph_renderer = from_networkx(G, layout_func, **kwargs)

graph_renderer.node_renderer.glyph = Scatter(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.selection_glyph = Scatter(size=15, fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = Scatter(size=15, fill_color=Spectral4[1])
scatter_glyph = Scatter(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.glyph = scatter_glyph
graph_renderer.node_renderer.selection_glyph = scatter_glyph.clone(fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = scatter_glyph.clone(fill_color=Spectral4[1])

graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=5)
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=5)
ml_glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.glyph = ml_glyph
graph_renderer.edge_renderer.selection_glyph = ml_glyph.clone(line_color=Spectral4[2], line_alpha=1)
graph_renderer.edge_renderer.hover_glyph = ml_glyph.clone(line_color=Spectral4[1], line_alpha=1)

if inspection_policy is not None:
graph_renderer.inspection_policy = inspection_policy
Expand All @@ -42,27 +44,32 @@ def create_graph(layout_func, inspection_policy=None, selection_policy=None, **k

return plot

plot_1 = create_graph(nx.circular_layout, inspection_policy=NodesAndLinkedEdges(), scale=1, center=(0,0))

plot_1 = create_graph(nx.circular_layout, inspection_policy=NodesAndLinkedEdges(), scale=1, center=(0, 0))
plot_1.title.text = "Circular Layout (NodesAndLinkedEdges inspection policy)"
plot_1.add_tools(HoverTool(tooltips=None))

plot_2 = create_graph(nx.spring_layout, selection_policy=NodesAndLinkedEdges(), scale=2, center=(0,0))
plot_2 = create_graph(nx.spring_layout, selection_policy=NodesAndLinkedEdges(), scale=2, center=(0, 0))
plot_2.title.text = "Spring Layout (NodesAndLinkedEdges selection policy)"
plot_2.add_tools(TapTool(), BoxSelectTool())

plot_3 = create_graph(nx.random_layout, inspection_policy=EdgesAndLinkedNodes(), center=(0,0))
plot_3 = create_graph(nx.random_layout, inspection_policy=EdgesAndLinkedNodes(), center=(0, 0))
plot_3.title.text = "Random Layout (EdgesAndLinkedNodes inspection policy)"
plot_3.add_tools(HoverTool(tooltips=None))

plot_4 = create_graph(nx.fruchterman_reingold_layout, selection_policy=EdgesAndLinkedNodes(), scale=2, center=(0,0), dim=2)
plot_4 = create_graph(
nx.fruchterman_reingold_layout, selection_policy=EdgesAndLinkedNodes(), scale=2, center=(0, 0), dim=2,
)
plot_4.title.text = "FR Layout (EdgesAndLinkedNodes selection policy)"
plot_4.add_tools(TapTool())

plot_5 = create_graph(nx.circular_layout, inspection_policy=NodesAndAdjacentNodes(), scale=1, center=(0,0))
plot_5 = create_graph(nx.circular_layout, inspection_policy=NodesAndAdjacentNodes(), scale=1, center=(0, 0))
plot_5.title.text = "Circular Layout (NodesAndAdjacentNodes inspection policy)"
plot_5.add_tools(HoverTool(tooltips=None))

plot_6 = create_graph(nx.fruchterman_reingold_layout, selection_policy=NodesAndAdjacentNodes(), scale=2, center=(0,0), dim=2)
plot_6 = create_graph(
nx.fruchterman_reingold_layout, selection_policy=NodesAndAdjacentNodes(), scale=2, center=(0, 0), dim=2,
)
plot_6.title.text = "FR Layout (NodesAndAdjacentNodes selection policy)"
plot_6.add_tools(TapTool())

Expand Down
2 changes: 1 addition & 1 deletion examples/styling/plots/glyph_non_visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
cr = p.circle(
x, y, radius=radii,
fill_color=colors, fill_alpha=0.8, line_color=None,
hover_fill_alpha=0.5, # mix `hover_` attributes with manual setup below
hover_fill_alpha=0.5, # mix `hover_` attributes with manual setup below
)

# there is no `hover_radius` so we have set things manually
Expand Down
8 changes: 2 additions & 6 deletions examples/styling/plots/glyph_selection_models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from bokeh.models import Scatter
from bokeh.plotting import figure, show

plot = figure(width=400, height=400, tools="tap", title="Select a circle")
renderer = plot.scatter([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=50)

selected_scatter = Scatter(fill_alpha=1, fill_color="firebrick", line_color=None)
nonselected_scatter = Scatter(fill_alpha=0.2, fill_color="blue", line_color="firebrick")

renderer.selection_glyph = selected_scatter
renderer.nonselection_glyph = nonselected_scatter
renderer.selection_glyph = renderer.glyph.clone(fill_alpha=1, fill_color="firebrick", line_color=None)
renderer.nonselection_glyph = renderer.glyph.clone(fill_alpha=0.2, fill_color="blue", line_color="firebrick")

show(plot)
18 changes: 10 additions & 8 deletions examples/topics/graph/graph_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
G = nx.karate_club_graph()

plot = Plot(width=400, height=400,
x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))
x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
plot.title.text = "Graph Interaction Demonstration"

plot.add_tools(HoverTool(tooltips=None), TapTool(), BoxSelectTool())

graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0,0))
graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0, 0))

graph_renderer.node_renderer.glyph = Scatter(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.selection_glyph = Scatter(size=15, fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = Scatter(size=15, fill_color=Spectral4[1])
scatter_glyph = Scatter(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.glyph = scatter_glyph
graph_renderer.node_renderer.selection_glyph = scatter_glyph.clone(fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = scatter_glyph.clone(fill_color=Spectral4[1])

graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=5)
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=5)
ml_glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.glyph = ml_glyph
graph_renderer.edge_renderer.selection_glyph = ml_glyph.clone(line_color=Spectral4[2], line_alpha=1)
graph_renderer.edge_renderer.hover_glyph = ml_glyph.clone(line_color=Spectral4[1], line_width=1)

graph_renderer.selection_policy = NodesAndLinkedEdges()
graph_renderer.inspection_policy = EdgesAndLinkedNodes()
Expand Down
19 changes: 10 additions & 9 deletions examples/topics/graph/interaction_edgeslinkednodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@

G = nx.karate_club_graph()

plot = Plot(width=400, height=400,
x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))
plot = Plot(width=400, height=400, x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
plot.title.text = "Graph Interaction Demonstration"

plot.add_tools(HoverTool(tooltips=None), TapTool(), BoxSelectTool())

graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0,0))
graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0, 0))

graph_renderer.node_renderer.glyph = Scatter(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.selection_glyph = Scatter(size=15, fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = Scatter(size=15, fill_color=Spectral4[1])
scatter_glyph = Scatter(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.glyph = scatter_glyph
graph_renderer.node_renderer.selection_glyph = scatter_glyph.clone(fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = scatter_glyph.clone(fill_color=Spectral4[1])

graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=5)
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=5)
ml_glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.glyph = ml_glyph
graph_renderer.edge_renderer.selection_glyph = ml_glyph.clone(line_color=Spectral4[2], line_alpha=1)
graph_renderer.edge_renderer.hover_glyph = ml_glyph.clone(line_color=Spectral4[1], line_width=1)

graph_renderer.selection_policy = EdgesAndLinkedNodes()
graph_renderer.inspection_policy = EdgesAndLinkedNodes()
Expand Down
19 changes: 10 additions & 9 deletions examples/topics/graph/interaction_nodesadjacentnodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@

G = nx.karate_club_graph()

plot = Plot(width=400, height=400,
x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))
plot = Plot(width=400, height=400, x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
plot.title.text = "Graph Interaction Demonstration"

plot.add_tools(HoverTool(tooltips=None), TapTool(), BoxSelectTool())

graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0,0))
graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0, 0))

graph_renderer.node_renderer.glyph = Scatter(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.selection_glyph = Scatter(size=15, fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = Scatter(size=15, fill_color=Spectral4[1])
scatter_glyph = Scatter(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.glyph = scatter_glyph
graph_renderer.node_renderer.selection_glyph = scatter_glyph.clone(fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = scatter_glyph.clone(fill_color=Spectral4[1])

graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=5)
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=5)
ml_glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.glyph = ml_glyph
graph_renderer.edge_renderer.selection_glyph = ml_glyph.clone(line_color=Spectral4[2], line_alpha=1)
graph_renderer.edge_renderer.hover_glyph = ml_glyph.clone(line_color=Spectral4[1], line_width=1)

graph_renderer.selection_policy = NodesAndAdjacentNodes()
graph_renderer.inspection_policy = NodesAndAdjacentNodes()
Expand Down
19 changes: 10 additions & 9 deletions examples/topics/graph/interaction_nodeslinkededges.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@

G = nx.karate_club_graph()

plot = Plot(width=400, height=400,
x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))
plot = Plot(width=400, height=400, x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
plot.title.text = "Graph Interaction Demonstration"

plot.add_tools(HoverTool(tooltips=None), TapTool(), BoxSelectTool())

graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0,0))
graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0, 0))

graph_renderer.node_renderer.glyph = Scatter(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.selection_glyph = Scatter(size=15, fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = Scatter(size=15, fill_color=Spectral4[1])
scatter_glyph = Scatter(size=15, fill_color=Spectral4[0])
graph_renderer.node_renderer.glyph = scatter_glyph
graph_renderer.node_renderer.selection_glyph = scatter_glyph.clone(fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = scatter_glyph.clone(fill_color=Spectral4[1])

graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=5)
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=5)
ml_glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.glyph = ml_glyph
graph_renderer.edge_renderer.selection_glyph = ml_glyph.clone(line_color=Spectral4[2], line_alpha=1)
graph_renderer.edge_renderer.hover_glyph = ml_glyph.clone(line_color=Spectral4[1], line_width=1)

graph_renderer.selection_policy = NodesAndLinkedEdges()
graph_renderer.inspection_policy = NodesAndLinkedEdges()
Expand Down
Loading

0 comments on commit 360e38e

Please sign in to comment.