gpt4 book ai didi

python - 是否可以更改 pydot 图的节点大小?

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:43 24 4
gpt4 key购买 nike

我编写了一段代码将我的 MultiDiGraph() 转换为 pydot 图形以显示自循环和箭头,但在转换之后,pydotA 丢失了 G 的所有属性。如何更改图 A 的节点大小并将它们设置为等于 node_sizes[] 列表中的相应值,就像我为 G 所做的那样?

代码:

def draw_graph(graph, size):

# extract nodes from graph
nodes = set([n1 for n1, n2 in graph] + [n2 for n1, n2 in graph])
print("Nodes ",nodes,"\n")

node_sizes = []

for n in nodes:
#Scaling up the node importance by a factor of 2000 to make it visible
node_sizes.append( size[n] * 2000)

print("Node size ",node_sizes,"\n")

# create networkx graph
G=nx.MultiDiGraph()

G.add_edges_from(edges)
G.add_nodes_from(nodes)

edge_colours = ['black' for edge in G.edges()]

pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, cmap=plt.get_cmap('jet'), node_size = node_sizes)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edges(G, pos, edge_color='black', arrows=True)

plt.show()

# render pydot by calling dot, no file saved to disk
A=nx.to_pydot(G, strict=True)

png_str = A.create_png(prog='dot')

# treat the dot output string as an image file
sio = BytesIO()
sio.write(png_str)
sio.seek(0)
img = mpimg.imread(sio)

# plot the image
imgplot = plt.imshow(img, aspect='equal')
plt.show(block=False)

最佳答案

networkx设置 DOT转换期间节点和边的属性使用 networkx.MultiDiGraph 节点和边标记的属性。例如,g.add_node(1, label='first node') 设置 label 属性:

import networkx as nx

graph = nx.MultiDiGraph()
graph.add_node(1, label='first node')
pd = nx.drawing.nx_pydot.to_pydot(graph)
print(pd.to_string())
# output:
# 'digraph {\n1 [label="first node"];\n}\n'

(另外:调用函数 nx.drawing.nx_pydot.to_pydot 而不是 nx.to_pydot,它在 networkx == 2.0 时不存在。)

利用这个原理,设置节点的height属性(或width,详见下文),我们可以在绘制时改变节点的大小:

import networkx as nx

# create a `networkx` graph
graph = nx.MultiDiGraph()
graph.add_edge(1, 2)
graph.add_node(3, height=3)
graph.add_node(4, height=6)
# convert to a `pydot` graph
pd = nx.drawing.nx_pydot.to_pydot(graph)
pd.write_png('foo.png', prog='dot')

如果您想编写自己的转换器(而不是调用 networkx.drawing.nx_pydot.to_pydot),请直接使用 pydot 图形:

import pydot

pd = pydot.Dot()
# create one node
nd = pydot.Node("a")
nd.set_height('"4"')
pd.add_node(nd)
# create another node
nd = pydot.Node("b")
nd.set_height('"2"')
pd.add_node(nd)
print(pd.to_string())
pd.write_png('bar.png')

与设置节点大小相关的属性有heightwidthfixedsize。来自 GraphViz 的 documentation :

height

Height of node, in inches. This is taken as the initial, minimum height of the node. If fixedsize is true, this will be the final height of the node. Otherwise, if the node label requires more height to fit, the node's height will be increased to contain the label. Note also that, if the output format is dot, the value given to height will be the final value.

If the node shape is regular, the width and height are made identical. In this case, if either the width or the height is set explicitly, that value is used. In this case, if both the width or the height are set explicitly, the maximum of the two values is used. If neither is set explicitly, the minimum of the two default values is used.

以上结果使用 networkx == 2.0pydot == 1.2.3

关于python - 是否可以更改 pydot 图的节点大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47124810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com