如何在图形绘图中添加键?
How to add a key to a graph drawing?
我使用 networkx
创建了一个图表。当我绘制此图时,节点标签不适合(节点名称可能很长),所以我用数字索引替换了它们。此变通方法的问题在于生成的图形更难解释,因为这些数字毫无意义。这是我的代码:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_nodes_from(['First', 'Second', 'Third'])
G.add_edges_from([('First', 'Second'), ('First', 'Third'), ('Second', 'Third')])
name2num = {name: num + 1 for num, name in enumerate(list(G.nodes))}
H = nx.relabel_nodes(G, mapping=name2num, copy=True)
fig, (ax0, ax1) = plt.subplots(1, 2)
nx.draw(G, ax=ax0, with_labels=True)
nx.draw(H, ax=ax1, with_labels=True)
我的问题是:如何在右侧的绘图中添加一个键?
这是我要显示的信息:
1 - First
2 - Second
3 - Third
我尝试添加图例,但没有成功:
ax0.legend(list(name2num.values()), list(name2num.keys()))
PS:我发现了很多与我的问题相关的话题。所提出的解决方案包括通过不同的颜色对节点标签进行编码。我宁愿通过数字对节点标签进行编码。
您可以放置 text box
:
而不是使用 ax.legend
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_nodes_from(["First", "Second", "Third"])
G.add_edges_from([("First", "Second"), ("First", "Third"), ("Second", "Third")])
name2num = {name: num + 1 for num, name in enumerate(list(G.nodes))}
H = nx.relabel_nodes(G, mapping=name2num, copy=True)
fig, (ax0, ax1) = plt.subplots(1, 2)
nx.draw(G, ax=ax0, with_labels=True)
nx.draw(H, ax=ax1, with_labels=True)
legend_text = "\n".join(f"{v} - {k}" for k, v in name2num.items())
props = dict(boxstyle="round", facecolor="w", alpha=0.5)
ax1.text(
1.15,
0.95,
legend_text,
transform=ax1.transAxes,
fontsize=14,
verticalalignment="top",
bbox=props,
)
您可以使用 nx.draw_networkx_labels
并使用定义的字典 name2num
将图表中的标签映射到第二个轴上。请注意,您不需要创建带有重命名标签的新图形,只需绘制相同的图形,禁用 with_labels
并使用字典:
name2num = {name: f'{num}-{name}' for num, name in enumerate(list(G.nodes), 1)}
有了这个你会得到:
fig, (ax0, ax1) = plt.subplots(1, 2)
posG_renamed = nx.spring_layout(G)
nx.draw(G, ax=ax0, with_labels=True,
node_size = 2000, node_color='lightblue')
nx.draw(G, pos=posG_renamed, ax=ax1,
node_size = 2000, node_color='lightgreen')
nx.draw_networkx_labels(G, posG_renamed, ax=ax1,
labels=name2num,
font_size=12)
我使用 networkx
创建了一个图表。当我绘制此图时,节点标签不适合(节点名称可能很长),所以我用数字索引替换了它们。此变通方法的问题在于生成的图形更难解释,因为这些数字毫无意义。这是我的代码:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_nodes_from(['First', 'Second', 'Third'])
G.add_edges_from([('First', 'Second'), ('First', 'Third'), ('Second', 'Third')])
name2num = {name: num + 1 for num, name in enumerate(list(G.nodes))}
H = nx.relabel_nodes(G, mapping=name2num, copy=True)
fig, (ax0, ax1) = plt.subplots(1, 2)
nx.draw(G, ax=ax0, with_labels=True)
nx.draw(H, ax=ax1, with_labels=True)
我的问题是:如何在右侧的绘图中添加一个键?
这是我要显示的信息:
1 - First
2 - Second
3 - Third
我尝试添加图例,但没有成功:
ax0.legend(list(name2num.values()), list(name2num.keys()))
PS:我发现了很多与我的问题相关的话题。所提出的解决方案包括通过不同的颜色对节点标签进行编码。我宁愿通过数字对节点标签进行编码。
您可以放置 text box
:
ax.legend
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_nodes_from(["First", "Second", "Third"])
G.add_edges_from([("First", "Second"), ("First", "Third"), ("Second", "Third")])
name2num = {name: num + 1 for num, name in enumerate(list(G.nodes))}
H = nx.relabel_nodes(G, mapping=name2num, copy=True)
fig, (ax0, ax1) = plt.subplots(1, 2)
nx.draw(G, ax=ax0, with_labels=True)
nx.draw(H, ax=ax1, with_labels=True)
legend_text = "\n".join(f"{v} - {k}" for k, v in name2num.items())
props = dict(boxstyle="round", facecolor="w", alpha=0.5)
ax1.text(
1.15,
0.95,
legend_text,
transform=ax1.transAxes,
fontsize=14,
verticalalignment="top",
bbox=props,
)
您可以使用 nx.draw_networkx_labels
并使用定义的字典 name2num
将图表中的标签映射到第二个轴上。请注意,您不需要创建带有重命名标签的新图形,只需绘制相同的图形,禁用 with_labels
并使用字典:
name2num = {name: f'{num}-{name}' for num, name in enumerate(list(G.nodes), 1)}
有了这个你会得到:
fig, (ax0, ax1) = plt.subplots(1, 2)
posG_renamed = nx.spring_layout(G)
nx.draw(G, ax=ax0, with_labels=True,
node_size = 2000, node_color='lightblue')
nx.draw(G, pos=posG_renamed, ax=ax1,
node_size = 2000, node_color='lightgreen')
nx.draw_networkx_labels(G, posG_renamed, ax=ax1,
labels=name2num,
font_size=12)