简体   繁体   English

使用.networkx绘制圆形图时如何调整半径大小?

[英]How to adjust radius size when plotting circular graph with networkx?

I need to plot several circular graphs in a figure having multiple rows and columns.我需要 plot 在具有多行和多列的图中绘制多个圆形图。 A simple example is given by the following, where I create a subplot with 2 rows and 2 columns.下面给出了一个简单的示例,其中我创建了一个包含 2 行和 2 列的子图。 Each entry contains a circle graph having 10, 6, 8, and 8 nodes, respectively.每个条目包含一个分别具有 10、6、8 和 8 个节点的圆图。

import networkx as nx
from matplotlib import pyplot as plt
from matplotlib.pyplot import figure
plt.rcParams["figure.figsize"] = (8,8)

n1=10
labels1={k:str(k) for k in range(n1)}
G1=nx.Graph()
G1.add_nodes_from(range(n1))

n2=6
labels2={k:str(k) for k in range(n2)}
G2=nx.Graph()
G2.add_nodes_from(range(n2))

n3=8
labels3={k:str(k) for k in range(n3)}
G3=nx.Graph()
G3.add_nodes_from(range(n3))

n4=8
labels4={k:str(k) for k in range(n4)}
G4=nx.Graph()
G4.add_nodes_from(range(n4))

fig,ax=plt.subplots(2,2)
node_size=250

nx.draw_circular(G1,labels=labels1,node_size=node_size,ax=ax[0,0],node_color='red')
nx.draw_circular(G2,labels=labels2,node_size=node_size,ax=ax[0,1],node_color='gray')
nx.draw_circular(G3,labels=labels3,node_size=node_size,ax=ax[1,0],node_color='yellow')
 nx.draw_circular(G4,labels=labels4,node_size=node_size,ax=ax[1,1],node_color='cyan')

plt.show()

The resulting figure is given below.结果图如下所示。 How can I shrink the radii of the circles, which will be useful when I add several more rows and/or columns to my subplots??我怎样才能缩小圆圈的半径,这在我向我的子图中添加更多行和/或列时会很有用?

在此处输入图像描述

Instead of using nx.draw_circular , you can use nx.draw with the node positions set with nx.circular_layout .除了使用nx.draw_circular ,您还可以使用nx.draw以及使用nx.circular_layout设置的节点位置。 You can then adjust the radius of nx.circular_layout by changing the scale argument.然后,您可以通过更改scale参数来调整nx.circular_layout的半径。 Finally, when you call plt.subplots , you should change sharex and sharey to True to make sure that the subplots have the same y and x limits.最后,当您调用plt.subplots时,您应该将sharexsharey更改为True以确保子图具有相同的yx限制。

See code below:请参阅下面的代码:

import networkx as nx
from matplotlib import pyplot as plt
from matplotlib.pyplot import figure
plt.rcParams["figure.figsize"] = (8,8)

n1=10
labels1={k:str(k) for k in range(n1)}
G1=nx.Graph()
G1.add_nodes_from(range(n1))

n2=6
labels2={k:str(k) for k in range(n2)}
G2=nx.Graph()
G2.add_nodes_from(range(n2))

n3=8
labels3={k:str(k) for k in range(n3)}
G3=nx.Graph()
G3.add_nodes_from(range(n3))

n4=8
labels4={k:str(k) for k in range(n4)}
G4=nx.Graph()
G4.add_nodes_from(range(n4))

fig,ax=plt.subplots(2,2,sharex=True, sharey=True)
node_size=250

nx.draw(G1,pos=nx.circular_layout(G1,scale=0.2),labels=labels1,node_size=node_size,ax=ax[0,0],node_color='red')
nx.draw(G2,pos=nx.circular_layout(G2,scale=0.4),labels=labels2,node_size=node_size,ax=ax[0,1],node_color='gray')
nx.draw(G3,pos=nx.circular_layout(G3,scale=0.6),labels=labels3,node_size=node_size,ax=ax[1,0],node_color='yellow')
nx.draw(G4,pos=nx.circular_layout(G4,scale=0.8),labels=labels4,node_size=node_size,ax=ax[1,1],node_color='cyan')

plt.show()

And the output:和 output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM