子图中的 networkx 正在绘制部分在轴框架之外的节点 [英] networkx in a subplot is drawing nodes partially outside of axes frame

查看:30
本文介绍了子图中的 networkx 正在绘制部分在轴框架之外的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在子图中绘制networkx图时,某些节点在轴的框架中被部分切除.我已经尝试过使用所有不同类型的图形和布局,但这始终是一个问题.它总是切断我的节点.就好像 networkx 在比实际更大的轴上绘制图形一样.

这是一个最小的例子

  plt.subplot(2,1,1)plt.scatter(范围(10),范围(10))plt.subplot(2, 1, 2)G = nx.erdos_renyi_graph(20, p=0.1)nx.draw_networkx(G)plt.show()

这就是我从中得到的.如您所见,节点 0 和节点 7 不适合框架.

解决方案

只要玩弄图形大小就可以解决问题.尝试通过子图的 figsize 参数设置更大的图形尺寸:

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')

<小时>

您还可以查看 networkX 的布局,例如

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's always a problem. It always cuts off my nodes. It's as if networkx is drawing the graph on a bigger axes than is actually there.

Here is a minimal example

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)
plt.show()

This is what I get from that. As you can see, node 0 and node 7 do not fit in the frame.

解决方案

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')

这篇关于子图中的 networkx 正在绘制部分在轴框架之外的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆