NetworkX - 노드 묶기 중지 - 시도된 Scale/K 매개변수 (NetworkX - Stop Nodes from Bunching Up - Tried Scale/K parameters)


문제 설명

NetworkX ‑ 노드 묶기 중지 ‑ 시도된 Scale/K 매개변수 (NetworkX ‑ Stop Nodes from Bunching Up ‑ Tried Scale/K parameters)

대부분 사이에 가장자리가 있는 ~28개의 노드가 있으며 일부는 격리되어 있습니다(가장자리 없음). 고립된 노드는 멋지게 펼쳐져 있지만 연결된 노드는 너무 쌓여서 아무것도 볼 수 없습니다. 다양한 node_sizes, scale 및 k 매개변수를 시도했지만 항상 (거의) 동일한 결과를 제공합니다. 더 나은 보기를 강제하는 방법이 있습니까?

nx.draw_spring(candidateGraph, node_size = 1000, with_labels=True, scale=100, weight=weightVal, k=100)

plt.show( )

여기에 이미지 설명 입력


참조 솔루션

방법 1:

There are several ways to do this.

First a comment on how networkx draws things. Here's the documentation. It creates a dictionary pos which has the coordinates of each node. Using draw_networkx you can send the optional argument pos=my_position_dict to it.

Networkx also has commands that will define the positons for you. This way you can have more control over things.

You have several options to do what you want.

The simplest is to plot just the connected component and leave out the isolated nodes.

nodes_of_largest_component  = max(nx.connected_components(G), key = len)
largest_component = G.subgraph(nodes_of_largest_component)
nx.draw_spring(largest_component)

Another would be to try one of the other (non‑spring) layouts. For example:

nx.draw_spectral(G)

Alternately, you can start manipulating the positions. One way is to set a couple positions as fixed and let spring_layout handle the rest.

pre_pos = {Huck: (0,0), Christie:(0,1), Graham:(1,1), Bush: (1,2)}
my_pos = nx.spring_layout(G, pos=pre_pos, fixed = pre_pos.keys())
nx.draw_networkx(G,pos=my_pos)

Then it will hold the nodes fixed that you've specified in fixed.

Alternately, define all the positions for just the largest component. Then hold those fixed and add the other nodes. The largest component will "fill" most of the available space, and then I think spring_layout will try to keep the added nodes from being too far away (alternately once you see the layout, you can specify these by hand).

nodes_of_largest_component  = max(nx.connected_components(G), key = len)
largest_component = G.subgraph(nodes_of_largest_component)
pos = nx.spring_layout(largest_component)
pos = nx.spring_layout(G,pos=pos,fixed=nodes_of_largest_component)
nx.draw_networkx(G,pos=pos)

One more thing to be aware of is that each call to spring_layout will result in a different layout. This is because it starts with some random positions. This is part of why it's useful to save the position dictionary, particularly if you plan to do anything fancy with the figure.

(by JibrilJoel)

참조 문서

  1. NetworkX ‑ Stop Nodes from Bunching Up ‑ Tried Scale/K parameters (CC BY‑SA 2.5/3.0/4.0)

#networkx #Python #matplotlib






관련 질문

단순 팻테일 로그 비닝 (Simple fat-tailed log-binning)

NetworkX - 노드 묶기 중지 - 시도된 Scale/K 매개변수 (NetworkX - Stop Nodes from Bunching Up - Tried Scale/K parameters)

문자열의 int 부분으로 문자열 레이블의 노드에 액세스 (Access a node of string label with an int part of the string)

시계열 분석을 위해 Pandas Groupby 및 date_range를 사용하는 동안 오류가 발생했습니다. (Error using Pandas Groupby and date_range for timeseries analysis)

regraph 계층 구조에서 가장자리 속성을 자리 표시자로 사용하는 방법이 있습니까? (Is there a way to have edge attributes as placeholders in a regraph hierarchy?)

networkx .module 'matplotlib.pyplot'에서 그래프를 그리는 동안 오류가 발생했습니다. (Error while drawing a graph in networkx .module 'matplotlib.pyplot' has no attribute 'ishold')

svg의 NetworkX node_shape (NetworkX node_shape from svg)

그래프를 통해 그래프 노드인 클래스 인스턴스의 속성에 액세스하는 방법은 무엇입니까? (How to access attributes of a class instance which is a graph node via the graph?)

그래프의 이미지를 분리하는 방법은 무엇입니까? (How to separate images of graphs?)

networkx 도면을 업데이트하는 방법 (How to update a networkx drawing)

그래프 노드 간의 메시지 흐름을 위한 Python 함수 (Python function for message flow between nodes of a graph)

networkx에서 임의의 에지 가중치를 사용하여 여러 임의의 그래프를 효율적으로 생성하는 방법 (How to efficiently generate multiple random graphs with random edge weights in networkx)







코멘트