本文整理汇总了Python中networkx.kamada_kawai_layout方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.kamada_kawai_layout方法的具体用法?Python networkx.kamada_kawai_layout怎么用?Python networkx.kamada_kawai_layout使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.kamada_kawai_layout方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: apply_network_layout
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def apply_network_layout(G, layout='kamada_kawai', verbose=True):
if layout == 'kamada_kawai':
if verbose:
print('Applying the Kamada-Kawai network layout... (may take several minutes)')
pos = nx.kamada_kawai_layout(G)
elif layout == 'spring_embedded':
if verbose:
print('Applying the spring-embedded network layout... (may take several minutes)')
pos = nx.spring_layout(G, k=0.2, iterations=100)
for n in G:
G.nodes[n]['x'] = pos[n][0]
G.nodes[n]['y'] = pos[n][1]
return G
示例2: draw_kamada_kawai
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def draw_kamada_kawai(G, **kwargs):
"""Draw networkx graph with circular layout.
Parameters
----------
G : graph
A networkx graph
kwargs : optional keywords
See hvplot.networkx.draw() for a description of optional
keywords, with the exception of the pos parameter which is not
used by this function.
Returns
-------
graph : holoviews.Graph or holoviews.Overlay
Graph element or Graph and Labels
"""
return draw(G, pos=nx.kamada_kawai_layout, **kwargs)
示例3: build_word_ego_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def build_word_ego_graph():
import networkx as nx
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
plt.rcParams['axes.unicode_minus'] = False # 步骤二(解决坐标轴负数的负号显示问题)
from harvesttext import get_sanguo, get_sanguo_entity_dict, get_baidu_stopwords
ht0 = HarvestText()
entity_mention_dict, entity_type_dict = get_sanguo_entity_dict()
ht0.add_entities(entity_mention_dict, entity_type_dict)
sanguo1 = get_sanguo()[0]
stopwords = get_baidu_stopwords()
docs = ht0.cut_sentences(sanguo1)
G = ht0.build_word_ego_graph(docs,"刘备",min_freq=3,other_min_freq=2,stopwords=stopwords)
pos = nx.kamada_kawai_layout(G)
nx.draw(G,pos)
nx.draw_networkx_labels(G,pos)
plt.show()
G = ht0.build_entity_ego_graph(docs, "刘备", min_freq=3, other_min_freq=2)
pos = nx.spring_layout(G)
nx.draw(G, pos)
nx.draw_networkx_labels(G, pos)
plt.show()
示例4: test_smoke_int
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def test_smoke_int(self):
G = self.Gi
vpos = nx.random_layout(G)
vpos = nx.circular_layout(G)
vpos = nx.planar_layout(G)
vpos = nx.spring_layout(G)
vpos = nx.fruchterman_reingold_layout(G)
vpos = nx.fruchterman_reingold_layout(self.bigG)
vpos = nx.spectral_layout(G)
vpos = nx.spectral_layout(G.to_directed())
vpos = nx.spectral_layout(self.bigG)
vpos = nx.spectral_layout(self.bigG.to_directed())
vpos = nx.shell_layout(G)
if self.scipy is not None:
vpos = nx.kamada_kawai_layout(G)
vpos = nx.kamada_kawai_layout(G, dim=1)
示例5: test_complete_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def test_complete_graph(self, dim):
"""Tests that nodes in a complete graph are located in the unit circle when using the
Kamada-Kawai layout"""
graph = nx.complete_graph(dim)
layout = nx.kamada_kawai_layout(graph)
coords = plot._node_coords(graph, layout)
x = coords["x"]
y = coords["y"]
radii = [np.sqrt(x[i] ** 2 + y[i] ** 2) for i in range(dim)]
assert np.allclose(radii, np.ones(dim), atol=1e-5)
示例6: test_cycle_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def test_cycle_graph(self, dim):
"""Tests that the length of edges in a circular cycle graph are all of equal length."""
graph = nx.cycle_graph(dim)
layout = nx.kamada_kawai_layout(graph)
coords = plot._edge_coords(graph, layout)
x = coords["x"]
y = coords["y"]
dists = [
np.sqrt((x[3 * k] - x[3 * k + 1]) ** 2 + (y[3 * k] - y[3 * k + 1]) ** 2)
for k in range(dim)
]
first_dist = np.sqrt((x[0] - x[1]) ** 2 + (y[0] - y[1]) ** 2)
assert np.allclose(dists, first_dist)
示例7: layout_obs_sub_only
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def layout_obs_sub_only(obs, scale=1000.0):
n_sub = obs.n_sub
n_line = obs.n_line
or_sub = obs.line_or_to_subid
ex_sub = obs.line_ex_to_subid
# Create a graph of substations vertices
G = nx.Graph()
# Set lines edges
for line_idx in range(n_line):
lor_sub = or_sub[line_idx]
lex_sub = ex_sub[line_idx]
# Compute edge vertices indices for current graph
left_v = lor_sub
right_v = lex_sub
# Register edge in graph
G.add_edge(left_v, right_v)
# Convert our layout to nx format
initial_layout = {}
for sub_idx, sub_name in enumerate(obs.name_sub):
initial_layout[sub_idx] = obs.grid_layout[sub_name]
# Use kamada_kawai algorithm
kkl = nx.kamada_kawai_layout(G, scale=scale)
# Convert back to our layout format
improved_layout = {}
for sub_idx, v in kkl.items():
sub_key = obs.name_sub[sub_idx]
vx = int(np.round(v[0]))
vy = int(np.round(v[1]))
improved_layout[sub_key] = [vx, vy]
return improved_layout
示例8: test_build_word_ego_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def test_build_word_ego_graph():
sys.stdout, expected = open(get_current_function_name()+"_current","w"), open(get_current_function_name()+"_expected").read()
import networkx as nx
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
plt.rcParams['axes.unicode_minus'] = False # 步骤二(解决坐标轴负数的负号显示问题)
from harvesttext import get_sanguo, get_sanguo_entity_dict, get_baidu_stopwords
ht0 = HarvestText()
entity_mention_dict, entity_type_dict = get_sanguo_entity_dict()
ht0.add_entities(entity_mention_dict, entity_type_dict)
sanguo1 = get_sanguo()[0]
stopwords = get_baidu_stopwords()
docs = ht0.cut_sentences(sanguo1)
G = ht0.build_word_ego_graph(docs,"刘备",min_freq=3,other_min_freq=2,stopwords=stopwords)
pos = nx.kamada_kawai_layout(G)
nx.draw(G,pos)
nx.draw_networkx_labels(G,pos)
G = ht0.build_entity_ego_graph(docs, "刘备", min_freq=3, other_min_freq=2)
pos = nx.spring_layout(G)
nx.draw(G, pos)
nx.draw_networkx_labels(G, pos)
sys.stdout.close()
assert open(get_current_function_name() + "_current").read() == expected
示例9: test_networkx_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def test_networkx_graph():
ng = nx.Graph()
ng.add_edge(1, 2, weight=1)
ng.add_edge(1, 3, weight=10)
return eda.networkx_graph(ng, nx.kamada_kawai_layout(ng),
figsize=(5, 5), alpha=1, node_with_labels=True)
示例10: test_smoke_empty_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def test_smoke_empty_graph(self):
G = []
vpos = nx.random_layout(G)
vpos = nx.circular_layout(G)
vpos = nx.planar_layout(G)
vpos = nx.spring_layout(G)
vpos = nx.fruchterman_reingold_layout(G)
vpos = nx.spectral_layout(G)
vpos = nx.shell_layout(G)
vpos = nx.bipartite_layout(G, G)
if self.scipy is not None:
vpos = nx.kamada_kawai_layout(G)
示例11: test_smoke_string
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def test_smoke_string(self):
G = self.Gs
vpos = nx.random_layout(G)
vpos = nx.circular_layout(G)
vpos = nx.planar_layout(G)
vpos = nx.spring_layout(G)
vpos = nx.fruchterman_reingold_layout(G)
vpos = nx.spectral_layout(G)
vpos = nx.shell_layout(G)
if self.scipy is not None:
vpos = nx.kamada_kawai_layout(G)
vpos = nx.kamada_kawai_layout(G, dim=1)
示例12: test_scale_and_center_arg
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def test_scale_and_center_arg(self):
sc = self.check_scale_and_center
c = (4, 5)
G = nx.complete_graph(9)
G.add_node(9)
sc(nx.random_layout(G, center=c), scale=0.5, center=(4.5, 5.5))
# rest can have 2*scale length: [-scale, scale]
sc(nx.spring_layout(G, scale=2, center=c), scale=2, center=c)
sc(nx.spectral_layout(G, scale=2, center=c), scale=2, center=c)
sc(nx.circular_layout(G, scale=2, center=c), scale=2, center=c)
sc(nx.shell_layout(G, scale=2, center=c), scale=2, center=c)
if self.scipy is not None:
sc(nx.kamada_kawai_layout(G, scale=2, center=c), scale=2, center=c)
示例13: test_default_scale_and_center
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def test_default_scale_and_center(self):
sc = self.check_scale_and_center
c = (0, 0)
G = nx.complete_graph(9)
G.add_node(9)
sc(nx.random_layout(G), scale=0.5, center=(0.5, 0.5))
sc(nx.spring_layout(G), scale=1, center=c)
sc(nx.spectral_layout(G), scale=1, center=c)
sc(nx.circular_layout(G), scale=1, center=c)
sc(nx.shell_layout(G), scale=1, center=c)
if self.scipy is not None:
sc(nx.kamada_kawai_layout(G), scale=1, center=c)
示例14: test_smoke_empty_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def test_smoke_empty_graph(self):
G = []
vpos = nx.random_layout(G)
vpos = nx.circular_layout(G)
vpos = nx.spring_layout(G)
vpos = nx.fruchterman_reingold_layout(G)
vpos = nx.spectral_layout(G)
vpos = nx.shell_layout(G)
if self.scipy is not None:
vpos = nx.kamada_kawai_layout(G)
示例15: test_smoke_int
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import kamada_kawai_layout [as 别名]
def test_smoke_int(self):
G = self.Gi
vpos = nx.random_layout(G)
vpos = nx.circular_layout(G)
vpos = nx.spring_layout(G)
vpos = nx.fruchterman_reingold_layout(G)
vpos = nx.fruchterman_reingold_layout(self.bigG)
vpos = nx.spectral_layout(G)
vpos = nx.spectral_layout(G.to_directed())
vpos = nx.spectral_layout(self.bigG)
vpos = nx.spectral_layout(self.bigG.to_directed())
vpos = nx.shell_layout(G)
if self.scipy is not None:
vpos = nx.kamada_kawai_layout(G)