Networkx自定义项不起作用

2024-03-28 10:22:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望使用python中的networkx包绘制网络图。我面临的问题是,我正在进行的定制没有发生,而默认值(可能)正在被使用。我使用的代码如下。它看起来很长,但大部分都是建立数据。在

import pandas as pd
import networkx as NX
from matplotlib import pyplot as plt
import numpy as np
import pygraphviz as PG

# the dataframeI'm using
corr_mat_2 = pd.DataFrame.from_dict({'clump_thickness': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.0, 'cell_shape_uniformity': 0.0, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': 0.0, 'bland_chromatin': 0.0, 'normal_nucleoli': 0.0, 'mitoses': -0.5790403219346321}, 'cell_size_uniformity': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.0, 'cell_shape_uniformity': 0.9490385801487778, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.5726586033292179, 'bare_nuclei': 0.0, 'bland_chromatin': 0.6533249167391942, 'normal_nucleoli': 0.5106708697857533, 'mitoses': -0.5473028893162575}, 'cell_shape_uniformity': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.9490385801487778, 'cell_shape_uniformity': 0.0, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.502767944815973, 'bare_nuclei': 0.5261228487320817, 'bland_chromatin': 0.631017333346977, 'normal_nucleoli': 0.5115973333620983, 'mitoses': -0.5850744184472585}, 'marginal_adhesion': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.0, 'cell_shape_uniformity': 0.0, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': 0.0, 'bland_chromatin': 0.0, 'normal_nucleoli': 0.0, 'mitoses': 0.0}, 'epithelial_cell_size': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.5726586033292179, 'cell_shape_uniformity': 0.502767944815973, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': 0.0, 'bland_chromatin': 0.0, 'normal_nucleoli': 0.0, 'mitoses': 0.0}, 'bare_nuclei': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.0, 'cell_shape_uniformity': 0.5261228487320817, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': 0.0, 'bland_chromatin': 0.5522628091390857, 'normal_nucleoli': 0.0, 'mitoses': -0.7437142606374423}, 'bland_chromatin': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.6533249167391942, 'cell_shape_uniformity': 0.631017333346977, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': 0.5522628091390857, 'bland_chromatin': 0.0, 'normal_nucleoli': 0.0, 'mitoses': -0.716623255542893}, 'normal_nucleoli': {'clump_thickness': 0.0, 'cell_size_uniformity': 0.5106708697857533, 'cell_shape_uniformity': 0.5115973333620983, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': 0.0, 'bland_chromatin': 0.0, 'normal_nucleoli': 0.0, 'mitoses': 0.0}, 'mitoses': {'clump_thickness': -0.5790403219346321, 'cell_size_uniformity': -0.5473028893162575, 'cell_shape_uniformity': -0.5850744184472585, 'marginal_adhesion': 0.0, 'epithelial_cell_size': 0.0, 'bare_nuclei': -0.7437142606374423, 'bland_chromatin': -0.716623255542893, 'normal_nucleoli': 0.0, 'mitoses': 0.0}}
)

G = NX.Graph()

# these are the nodes
nodes = ['clump_thickness', 'cell_size_uniformity', 'cell_shape_uniformity', 'epithelial_cell_size', 'bare_nuclei',
         'bland_chromatin', 'normal_nucleoli', 'mitoses']

# the following list contains the pairs between which I want to add an edge
pairs = [['bare_nuclei', 'bland_chromatin'], ['bare_nuclei', 'cell_shape_uniformity'],
         ['bare_nuclei', 'mitoses'], ['bland_chromatin', 'cell_shape_uniformity'],
         ['bland_chromatin', 'cell_size_uniformity'],
         ['bland_chromatin', 'mitoses'], ['cell_shape_uniformity', 'cell_size_uniformity'],
         ['cell_shape_uniformity', 'epithelial_cell_size'], ['cell_shape_uniformity', 'mitoses'],
         ['cell_shape_uniformity', 'normal_nucleoli'], ['cell_size_uniformity', 'epithelial_cell_size'],
         ['cell_size_uniformity', 'mitoses']]

# the size of each node depends on the average value of the absolute values of the corresponding column.
# the below is the minimum size
node_default_size = 2
for each_node in nodes:
    # the customisation I want for each node
    avg_abs_corr = corr_mat.loc[:, each_node].abs().mean()
    G.add_node(each_node,
               weight=str(avg_abs_corr + node_default_size),
               size=str(avg_abs_corr + node_default_size),
               color='skyblue',
               style='filled',
               fontcolor='red',
               fontname='Calibri',
               fontsize=12,
               penwidth=1)

#
for each_pair in pairs[::-1]:
    edge_len = corr_mat.loc[each_pair[0], each_pair[1]]
    # default edge color is red
    color = 'red'
    # change the edge color if its positive
    if edge_len > 0:
        color = 'green'
    # the customisation for each edge
    G.add_edge(each_pair[0], each_pair[1], len=str(5 * edge_len), color=color, width="2.0")
NX.draw(G)

输出如下enter image description here

这不仅在节点处没有任何标签,而且任何自定义设置都不起作用。你知道我哪里出错了吗?在

我最终想要实现的是这样的graph。在


Tags: thesizecellnormalshapethicknessbarenuclei
1条回答
网友
1楼 · 发布于 2024-03-28 10:22:41

由于您是先创建图形,然后要绘制它,我建议您使用nx.绘制命令。在

例如

NX.draw(G,
    with_labels=True,
    font_color='r',
    node_size=[float(G.node[node]['size']) for node in G.nodes()],
    node_color=[G.node[node]['color'] for node in G.nodes()],
    fontweight=[G.node[node]['fontsize'] for node in G.nodes()],
    edge_color=[G.edge[edge[0]][edge[1]]['color'] for edge in G.edges()],
    width=[G.edge[edge[0]][edge[1]]['width'] for edge in G.edges()]
   )

相关问题 更多 >