NetworkX 多重图不显示标签
NetworkX multigraph plot does not show labels
我正在尝试使用 Python 绘制知识图,查看了许多示例和答案,但仍然无法从边列表中自动绘制边标签。这是我正在尝试做的简化工作示例:
import pandas as pd
import networkx as nx
minidf = pd.DataFrame(data={'relation': ['subject', 'subject', 'broader'],
'source': ['pmt3423', 'pmt2040', 'category:myoblasts'],
'target': ['conceito', 'frio', 'category:non-terminally_differentiated_(blast)']})
miniG = nx.from_pandas_edgelist(minidf,'source', 'target',
edge_key='relation', create_using=nx.MultiDiGraph())
nx.draw_networkx(miniG, with_labels=True)
我得到的输出如下:
我也试过 draw_circular 和其他人。我也尝试过使用 pyvis 并生成一个点文件 + 使用 neato
转换为 png。还没完全明白。
感谢任何帮助。
以下是向图表添加边标签的方法:
import pandas as pd
import networkx as nx
from matplotlib import pyplot as plt
minidf = pd.DataFrame(data = {'relation': ['subject', 'subject', 'broader'],
'source': ['pmt3423', 'pmt2040', 'category:myoblasts'],
'target': ['conceito', 'frio', 'category:non-terminally_differentiated_(blast)']})
miniG = nx.from_pandas_edgelist(minidf,'source', 'target', create_using=nx.MultiDiGraph())
pos = nx.spring_layout(miniG)
e_labels = {(minidf.source[i], minidf.target[i]):minidf.relation[i]
for i in range(len(minidf['relation']))}
nx.draw_networkx_edge_labels(miniG, pos, edge_labels= e_labels)
nx.draw(miniG, pos = pos,with_labels=True)
plt.show()
但是,正如您在上面看到的,这可能会很混乱,因为您的图中没有太多空间用于边缘标签。更好的解决方案是对边缘进行颜色编码并提供图例:
import pandas as pd
import networkx as nx
from matplotlib import pyplot as plt
minidf = pd.DataFrame(data = {'relation': ['subject', 'subject', 'broader'],
'source': ['pmt3423', 'pmt2040', 'category:myoblasts'],
'target': ['conceito', 'frio', 'category:non-terminally_differentiated_(blast)']})
miniG = nx.from_pandas_edgelist(minidf,'source', 'target', create_using=nx.MultiDiGraph())
#color-code the edges
color_code = {'subject':'red', 'broader':'lime'}
edge_color_list = [color_code[rel] for rel in minidf.relation]
nx.draw(miniG, with_labels= True, edge_color= edge_color_list)
#create a color-coded legend
leg = plt.legend(color_code,labelcolor=color_code.values())
for i, item in enumerate(leg.legendHandles):
item.set_color(list(color_code.values())[i])
plt.show()
我正在尝试使用 Python 绘制知识图,查看了许多示例和答案,但仍然无法从边列表中自动绘制边标签。这是我正在尝试做的简化工作示例:
import pandas as pd
import networkx as nx
minidf = pd.DataFrame(data={'relation': ['subject', 'subject', 'broader'],
'source': ['pmt3423', 'pmt2040', 'category:myoblasts'],
'target': ['conceito', 'frio', 'category:non-terminally_differentiated_(blast)']})
miniG = nx.from_pandas_edgelist(minidf,'source', 'target',
edge_key='relation', create_using=nx.MultiDiGraph())
nx.draw_networkx(miniG, with_labels=True)
我得到的输出如下:
我也试过 draw_circular 和其他人。我也尝试过使用 pyvis 并生成一个点文件 + 使用 neato
转换为 png。还没完全明白。
感谢任何帮助。
以下是向图表添加边标签的方法:
import pandas as pd
import networkx as nx
from matplotlib import pyplot as plt
minidf = pd.DataFrame(data = {'relation': ['subject', 'subject', 'broader'],
'source': ['pmt3423', 'pmt2040', 'category:myoblasts'],
'target': ['conceito', 'frio', 'category:non-terminally_differentiated_(blast)']})
miniG = nx.from_pandas_edgelist(minidf,'source', 'target', create_using=nx.MultiDiGraph())
pos = nx.spring_layout(miniG)
e_labels = {(minidf.source[i], minidf.target[i]):minidf.relation[i]
for i in range(len(minidf['relation']))}
nx.draw_networkx_edge_labels(miniG, pos, edge_labels= e_labels)
nx.draw(miniG, pos = pos,with_labels=True)
plt.show()
但是,正如您在上面看到的,这可能会很混乱,因为您的图中没有太多空间用于边缘标签。更好的解决方案是对边缘进行颜色编码并提供图例:
import pandas as pd
import networkx as nx
from matplotlib import pyplot as plt
minidf = pd.DataFrame(data = {'relation': ['subject', 'subject', 'broader'],
'source': ['pmt3423', 'pmt2040', 'category:myoblasts'],
'target': ['conceito', 'frio', 'category:non-terminally_differentiated_(blast)']})
miniG = nx.from_pandas_edgelist(minidf,'source', 'target', create_using=nx.MultiDiGraph())
#color-code the edges
color_code = {'subject':'red', 'broader':'lime'}
edge_color_list = [color_code[rel] for rel in minidf.relation]
nx.draw(miniG, with_labels= True, edge_color= edge_color_list)
#create a color-coded legend
leg = plt.legend(color_code,labelcolor=color_code.values())
for i, item in enumerate(leg.legendHandles):
item.set_color(list(color_code.values())[i])
plt.show()