我有一个Pandas数据帧,并想基于该数据帧绘制一个网络。当前的图表如下:
它从右上角开始,向左下角延伸。如果我下次绘制它,它可能会有不同的起始位置,如何避免这种情况?此外,如何将起始节点设置为左上角,结束节点(也可以预先拒绝)始终设置为右下角?
我的代码到目前为止是:
![enter image description here](https://istack.dev59.com/Gg5Ij.webp)
我的代码到目前为止是:
###make the graph based on my dataframe
G3 = nx.from_pandas_edgelist(df2, 'Activity description', 'Activity followed', create_using=nx.DiGraph(), edge_attr='weight')
#plot the figure and decide about the layout
plt.figure(3, figsize=(18,18))
pos = nx.spring_layout(G3, scale=2)
#draw the graph based on the labels
nx.draw(G3, pos, node_size=500, alpha=0.9, labels={node:node for node in G3.nodes()})
#make weights with labels to the edges
edge_labels = nx.get_edge_attributes(G3,'weight')
nx.draw_networkx_edge_labels(G3, pos, edge_labels = edge_labels)
plt.title('Main Processes')
#save and plot the ifgure
plt.savefig('StandardProcessflow.png')
plt.show()
我使用的包是networkx和matlotlib。
nx.spring_layout
中设置一个seed
,例如pos=nx.spring_layout(G3, scale=2, seed=42)
。看看是否有帮助。 - Itamar Mushkin