【问题标题】:Matplotlib: How do you save an interactive networkx plot?Matplotlib:如何保存交互式 networkx 图?
【发布时间】:2020-10-01 08:31:51
【问题描述】:

我创建了一个具有悬停功能的加权 networkx 图,它显示每个节点的所有连接。当我保存绘图时,我得到一个静态 PNG 图像。我想问有没有什么办法可以让我用悬停功能保存这个情节,以便与其他人分享?

这是我的代码:

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

G = nx.Graph()

G.add_edge("Ted", "May", weight=0.5)
G.add_edge("Ted", "Ray", weight=1)
G.add_edge("Ted", "Chris", weight=1)
G.add_edge("Ted", "Sam", weight=3)
G.add_edge("Ted", "April", weight=1)
G.add_edge("Ted", "Ana", weight=0)


G.add_edge("Ana", "Ryan", weight=1)
G.add_edge("Ana", "Jim", weight=0.5)
G.add_edge("Ana", "Ben", weight=1)


for0 = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] == 0]
for05 = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] == 0.5]
for1 = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] == 1]
for15 = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] == 1.5]
for3 = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] == 3]



fig, ax = plt.subplots(figsize=(7.2, 7.2))
pos = nx.circular_layout(G)  # positions for all nodes
names = np.array(["Ted", "May", "Ray", "Chris", "Sam", "April", "Ana", "Ryan", "Jim", "Ben"])

# nodes
sc = nx.draw_networkx_nodes(G, pos, node_size=700)

# edges


nx.draw_networkx_edges(G, pos, edgelist=for0, width=0)
nx.draw_networkx_edges(G, pos, edgelist=for05, width=0.5)

nx.draw_networkx_edges(G, pos, edgelist=for1, width=1)
nx.draw_networkx_edges(G, pos, edgelist=for15, width=1.5)

nx.draw_networkx_edges(G, pos, edgelist=for3, width=3)


# labels
nx.draw_networkx_labels(G, pos, font_size=20, font_family="sans-serif")

annot = ax.annotate("", xy=(0,0), xytext=(20,20),textcoords="offset points",
                    bbox=dict(boxstyle="round", fc="w"),
                    arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)

def update_annot(ind):

    pos = sc.get_offsets()[ind["ind"][0]]
    annot.xy = pos
    es = [e for e in G[names[ind["ind"]][0]]]
    text = "{}".format(es)
    annot.set_text(text)
    annot.get_bbox_patch().set_alpha(0.4)
    return sc


def hover(event):
    vis = annot.get_visible()
    if event.inaxes == ax:
        cont, ind = sc.contains(event)
        if cont:
            update_annot(ind)
            annot.set_visible(True)
            fig.canvas.draw_idle()
        else:
            if vis:
                annot.set_visible(False)
                fig.canvas.draw_idle()

fig.canvas.mpl_connect("motion_notify_event", hover)
plt.show()

谢谢。

【问题讨论】:

  • 保存到html可以开启悬停效果,这种情况下不推荐matplotlib,使用bokeh,plotly,或者mpld3代替。但是你需要一些定制,这并不容易。
  • 我该怎么做呢? @ted930511
  • 我认为保存到 GEXF 可能会有所帮助,否则您必须学习 Javascript 和 css 才能进行一些自定义。

标签: python python-3.x matplotlib hover networkx


【解决方案1】:

如果你不想重写你的代码,你可以将它保存在GEXF中,以便其他人可以在Gephi中打开它:

nx.write_gexf(G, "test.gexf")

或者,如果您想将其保存在 HTML 文件中,则需要使用不同的绘图 API,例如支持交互式绘图 (see here for some examples) 的 plotly

【讨论】:

  • 我用 gexf 打开它,但节点失去了它们的标签和颜色 - 我该怎么做?
  • 你见过this关于颜色的事情吗? Documentation 还解释了标签。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-12
  • 2017-09-03
  • 2013-11-28
相关资源
最近更新 更多