Reputation: 7977
I am looking to plot a network graph using the networkx
package in python. The problem I am facing is that the customizations that I'm making are not taking place and the defaults values(probably) are getting used instead. The code I am using is below. It looks lengthy but it most of it setting up the data.
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)
The output is something like this
Not only does this not have any labels at the nodes, none of the customizations work. Any ideas on where I am going wrong?
What I eventually want to achieve is something like this graph.
Upvotes: 1
Views: 633
Reputation: 1018
Since you are creating the graph first and then you want to draw it, I would recommend to play with the options of the nx.draw command.
e.g.
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()]
)
Upvotes: 1