networkx in a subplot is drawing nodes partially outside of axes frame

后端 未结 2 1657
一个人的身影
一个人的身影 2021-01-27 02:16

When I draw a networkx graph in a subplot, some of the nodes are partially cut off in the frame of the axes. I\'ve tried this with all different types of graphs and layouts, it\

2条回答
  •  长发绾君心
    2021-01-27 03:05

    Just playing a with the figure sizes should do the trick. Try setting a larger figure size through the subplots' figsize parameter:

    f, axs = plt.subplots(2,1,figsize=(15,15))
    axs[0].scatter(range(10), range(10))
    G = nx.erdos_renyi_graph(20, p=0.1)
    nx.draw_networkx(G, ax=axs[1], node_color='lightgreen')
    


    You can also look into networkX' layouts, such as spring_layout, which allow to encapsulate the nodes within a given box size, specified by a scale parameter. Here's an example:

    f, axs = plt.subplots(2,1,figsize=(15,15))
    axs[0].scatter(range(10), range(10))
    G = nx.erdos_renyi_graph(20, p=0.05)
    pos = nx.spring_layout(G, k=0.7, scale=0.05)
    nx.draw_networkx(G, pos=pos, ax=axs[1], node_color='lightgreen')
    

提交回复
热议问题