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

后端 未结 2 1624
一个人的身影
一个人的身影 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 02:44

    Background

    Your issue seems to be caused by the new autoscaling algorithm introduced with matplotlib 3.2.0. In the link it states, that the old algorithm did

    for Axes.scatter it would make the limits large enough to not clip any markers in the scatter.

    Hence, the new algorithm has stopped to do this, which results in the cute nodes.

    How to fix your problem

    You can simply increase the length of your axis:

    import networkx as nx
    import matplotlib.pylab as plt
    
    figure = plt.subplot(2, 1, 1)
    plt.scatter(range(10), range(10))
    
    plt.subplot(2, 1, 2)
    G = nx.erdos_renyi_graph(20, p=0.1)
    nx.draw_networkx(G)
    axis = plt.gca()
    # maybe smaller factors work as well, but 1.1 works fine for this minimal example
    axis.set_xlim([1.1*x for x in axis.get_xlim()])
    axis.set_ylim([1.1*y for y in axis.get_ylim()])
    plt.show()
    

提交回复
热议问题