【发布时间】:2018-03-01 00:02:35
【问题描述】:
我无法在 networkx 中可视化具有平行边的多向图,因此我使用了 pydot,但我遇到了两个问题
1) 我似乎无法理解为什么节点位置没有被固定我想在 x 和 y 指定的坐标处绘制它们 2)如何设置正在绘制的图形的大小 plt.figure 命令实际上不起作用 3)我如何添加边缘标签(如果我有的话)
非常感谢
import networkx as nx
from io import StringIO
from io import BytesIO
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import networkx as nx
graph= nx.MultiGraph()
#add 4 nodes in the vertexs of a square. X and Y are the coordinates
graph.add_node(1,x=100,y=100)
graph.add_node(2,x=100,y=200)
graph.add_node(3,x=200,y=100)
graph.add_node(4,x=200,y=200)
graph.add_edge(1,2)
graph.add_edge(2,1)
graph.add_edge(3,1)
graph.add_edge(3,4)
graph.add_edge(4,1)
# assign positions
for n in graph:
graph.node[n]['pos'] = '"%d,%d"'%(graph.node[n]['x'], graph.node[n]['y'])
p = nx.drawing.nx_pydot.to_pydot(graph)
# render pydot by calling dot, no file saved to disk
png_str = p.create_png(prog='C:\\Anaconda3\\Library\\bin\\graphviz\\dot.exe')
# treat the dot output string as an image file
sio = BytesIO()
sio.write(png_str)
sio.seek(0)
img = mpimg.imread(sio)
plt.figure(figsize = (10,10))
imgplot = plt.imshow(img)
【问题讨论】:
标签: python networkx graphviz pydot