matplotlib - 节点悬停效果
问题描述
当鼠标放在 networkx 生成的节点上时,我试图显示节点属性(例如:单词)。有没有可能做到这一点。所有这些功能都应该在PyQt5应用程序中进行定制。
例子:
#A bit of idea
for i, elrow in df.iterrows():
G.add_nodes(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict(), weight=elrow(3)) #Just giving an instance
我如何将所有数据抓取attr_dict
为鼠标悬停时的节点属性
解决方案
您应该使用散景库进行这种可视化。
您可以在此处找到针对您的问题的确切解决方案:
import networkx as nx
from bokeh.io import show, output_file
from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, BoxZoomTool, ResetTool
from bokeh.models.graphs import from_networkx
from bokeh.palettes import Spectral4
# Prepare Data
G = nx.karate_club_graph()
SAME_CLUB_COLOR, DIFFERENT_CLUB_COLOR = "black", "red"
edge_attrs = {}
for start_node, end_node, _ in G.edges(data=True):
edge_color = SAME_CLUB_COLOR if G.nodes[start_node]["club"] == G.nodes[end_node]["club"] else DIFFERENT_CLUB_COLOR
edge_attrs[(start_node, end_node)] = edge_color
nx.set_edge_attributes(G, edge_attrs, "edge_color")
# Show with Bokeh
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"
node_hover_tool = HoverTool(tooltips=[("index", "@index"), ("club", "@club")])
plot.add_tools(node_hover_tool, BoxZoomTool(), ResetTool())
graph_renderer = from_networkx(G, nx.spring_layout, scale=1, center=(0, 0))
graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0])
graph_renderer.edge_renderer.glyph = MultiLine(line_color="edge_color", line_alpha=0.8, line_width=1)
plot.renderers.append(graph_renderer)
output_file("interactive_graphs.html")
show(plot)
推荐阅读
- python - 在 Python 中导入子模块失败
- python - 自定义 keras 回调和变分自动编码器损失函数中正则化项的权重 (beta) 变化
- javascript - Javascript将动态位置文本弹出到图像中
- postman - 使用邮递员在合流页面上上传 .doc 文件的问题
- javascript - 将 DOM 保存为图像,然后将其转换为要发送到 API 参数的文件对象
- git - 在 mac(m1) 终端上没有获得完整的分支名称
- android - 滚动时将扩展浮动按钮更改为正常大小
- sql - sql upsert 语句 - postgresql
- r - 将列转换为 dplyr 管道中的百分比
- python-3.x - 构建 Apache Beam 数据管道时如何定义 bigquery 架构