[英]Plot bokeh MultiLine using different width
I would like to use MultiLine with a list that provides the LineWidth, so that each edge in the graph is plotted with a different width. 我想将MultiLine与提供LineWidth的列表一起使用,以便以不同的宽度绘制图形中的每个边。 Below is a simplified example taken from the documentation which throws an error.
下面是摘自文档的简化示例,该示例引发错误。 Is there a possibility to use MultiLine with different width?
是否可以使用不同宽度的MultiLine? Or do you know a different way to plot a graph with different width?
还是您知道另一种绘制宽度不同的图的方法?
import networkx as nx
from bokeh.io import show, output_file
from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, TapTool, BoxSelectTool
from bokeh.models.graphs import from_networkx
from bokeh.palettes import Spectral4
import numpy as np
G=nx.karate_club_graph()
ewidth = [np.random.random() for (u, v, d) in G.edges(data=True)]
plot = Plot(plot_width=400, plot_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.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0])
#this works:
#graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5)
# this doesnt work:
graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=ewidth)
plot.renderers.append(graph_renderer)
output_file("interactive_graphs.html")
show(plot)
Found the answer. 找到了答案。 For those who stumble about the same question here the solution.
对于那些偶然遇到相同问题的人,这里提供了解决方案。 You can do it either with
set_edge_attributes
or by adding the property to the data source 您可以使用
set_edge_attributes
或通过将属性添加到数据源来完成此操作
graph_renderer.node_renderer.data_source.data['edge_width'] = ...
below the code: 代码下方:
import networkx as nx
from bokeh.io import show
from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, TapTool, BoxSelectTool
from bokeh.models.graphs import from_networkx
from bokeh.palettes import Spectral4
import numpy as np
G=nx.karate_club_graph()
#option 1
edge_attrs={}
for (u, v, d) in G.edges(data=True):
edge_attrs[(u, v)] = np.round(np.random.random() ,2)
nx.set_edge_attributes(G, edge_attrs, "edge_width")
###
plot = Plot(plot_width=400, plot_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))
# option 2
#graph_renderer.node_renderer.data_source.data['edge_width']=[np.random.random() for (u, v, d) in G.edges(data=True)]
###
graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0])
graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width="edge_width")
plot.renderers.append(graph_renderer)
show(plot)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.