Core Functionalities
In order to create, manipulate, and study the structure, dynamics, and functions of complex networks, it is usefull to understand the various functions of d3graph
. Here I will describe the core functionalities that can help to customize your network. In the following examples I will be using the karate network.
Import
Importing the d3graph
library is the first step after the pip installation.
# Import library
from d3graph import d3graph
Initalization
The initialization is directly performed after importing the d3graph
library. During the initialization, the following parameters can be set:
* collision : 0.5 : Response of the network. Higher means that more collisions are prevented.
* charge : 250 : Edge length of the network. Towards zero becomes a dense network.
* slider : [None, None] : Slider to break the network. The default is based on the edge weights.
* verbose : 20 : Print progress to screen, 60: None, 40: Error, 30: Warn, 20: Info, 10: Debug
A run with default initialization.
# Import library
from d3graph import d3graph
# Initialization with default parameters
d3 = d3graph()
# Load karate example
adjmat, df = d3.import_example('karate')
# Process the adjacency matrix
d3.graph(adjmat)
# Plot
d3.show()
The collision parameter
This network will go wild because it tries to prevent collisions from happening. At some point, the network will stop trying. You can reset it by breaking the network with the silder.
# Import library
from d3graph import d3graph
# Initialization to make the network be more nervous when nodes are close together.
d3 = d3graph(collision=3)
# Load karate example
adjmat, df = d3.import_example('karate')
# Process the adjacency matrix
d3.graph(adjmat)
# Plot
d3.show()
The charge parameter.
This network is much wider than the previous ones. This is certainly helpfull if you have a dense network and need to expand it for visualization purposes.
# Import library
from d3graph import d3graph
# Initialization to make network edges reltively longer.
d3 = d3graph(charge=1000)
# Load karate example
adjmat, df = d3.import_example('karate')
# Process the adjacency matrix
d3.graph(adjmat)
# Plot
d3.show()
Processing
The graph function d3graph.d3graph.d3graph.graph()
processes the adjacency matrix to create a network with default node properties and edge properties. The nodes are the column and index names, and a connect edge for vertices with value larger than 0. The strenght of edges are based on the vertices values. The input for d3graph
is the adjacency matrix.
Show
The show function d3graph.d3graph.d3graph.show()
has several tasks.
Creating networkx graph G based on the node properties and edge properties.
Embedding of the data.
Writes the final HTML file to disk.
Opens the webbroswer with the network graph.
Node properties
There are various possabilities to customize the network using the node properties function d3graph.d3graph.d3graph.set_node_properties()
. Intially, all default node properties are created which can than be customized. The underneath properties can be changed for each node. I will use the karate network to demonstrate the working.
Note
Node label
Node hover text
Node color
Node size
Node edge color
Noe edge size
Node label
Lets change the node labels from the karate example into something more meaningfull.
# Import library
from d3graph import d3graph
# Initialization
d3 = d3graph()
# Load karate example
adjmat, df = d3.import_example('karate')
# Process the adjacency matrix
d3.graph(adjmat)
# Set node properties
d3.set_node_properties(label=df['label'].values)
# Plot
d3.show()
Hover over text
Getting more information when hovering over a node can be easily done using the hover
parameter.
# Import library
from d3graph import d3graph
# Initialization
d3 = d3graph()
# Load karate example
adjmat, df = d3.import_example('karate')
# Process the adjacency matrix
d3.graph(adjmat)
# Set node properties
hover = '\nId: ' + adjmat.columns.astype(str) +'\nDegree: ' + df['degree'].astype(str) + '\nLabel: ' + df['label'].values
hover = hover.values
label = df['label'].values
# Set node properties
d3.set_node_properties(label=label, hover=hover, color=label)
# Plot
d3.show()
Node color
Lets change the node colors from the karate example using the label information. We do not need to re-initialize the whole graph but we can simply update the node properties.
# Set node properties
d3.set_node_properties(label=df['label'].values, color=df['label'].values)
# Plot
d3.show()
Node color on clustering
We can also change the node color on the clustering.
# Set node properties
d3.set_node_properties(label=df['label'].values, color='cluster')
# Plot
d3.show()
Node size
Lets change the node size from the karate example using the degree of the network. We do not need to re-initialize the whole graph but we can simply update the node properties.
# Set node properties
d3.set_node_properties(label=df['label'].values, color=df['label'].values, size=df['degree'].values)
# Plot
d3.show()
Node edge size
Lets change the node edge size from the karate example using the degree of the network. We do not need to re-initialize the whole graph but we can simply update the node properties.
# Set node properties
d3.set_node_properties(label=df['label'].values, color=df['label'].values, size=df['degree'].values, edge_size=df['degree'].values)
# Plot
d3.show()
Node edge color
Lets change the node edge color from the karate example using a specified color. We do not need to re-initialize the whole graph but we can simply update the node properties.
# Set node properties
d3.set_node_properties(label=df['label'].values, color=df['label'].values, size=df['degree'].values, edge_size=df['degree'].values, edge_color='#FFF000')
# Plot
d3.show()
Customize the properties of one specific node
# Import library
from d3graph import d3graph
# Initialization
d3 = d3graph()
# Load karate example
adjmat = d3.import_example('bigbang')
# Process the adjacency matrix
d3.graph(adjmat)
# Examine the node properties
print(d3.node_properties)
# {'Amy': {'label': 'Amy', 'color': '#000080', 'size': 10, 'edge_size': 0.1, 'edge_color': '#000000'},
# 'Bernadette': {'label': 'Bernadette', 'color': '#000080', 'size': 10, 'edge_size': 0.1, 'edge_color': '#000000'},
# 'Howard': {'label': 'Howard', 'color': '#000080', 'size': 10, 'edge_size': 0.1, 'edge_color': '#000000'},
# 'Leonard': {'label': 'Leonard', 'color': '#000080', 'size': 10, 'edge_size': 0.1, 'edge_color': '#000000'},
# 'Penny': {'label': 'Penny', 'color': '#000080', 'size': 10, 'edge_size': 0.1, 'edge_color': '#000000'},
# 'Rajesh': {'label': 'Rajesh', 'color': '#000080', 'size': 10, 'edge_size': 0.1, 'edge_color': '#000000'},
# 'Sheldon': {'label': 'Sheldon', 'color': '#000080', 'size': 10, 'edge_size': 0.1, 'edge_color': '#000000'}}
# Customize the properties of one specific node
d3.node_properties['Penny']['label']='Penny Hofstadter'
d3.node_properties['Penny']['color']='#ffc0cb'
d3.node_properties['Penny']['size']=20
d3.node_properties['Penny']['edge_size']=5
d3.node_properties['Penny']['edge_color']='#000000'
# Print
print(d3.node_properties['Penny'])
# {'label': 'Penny Hofstadter', 'color': '#ffc0cb', 'size': 20, 'edge_size': 5, 'edge_color': '#000000'}
# Plot
d3.show()
Edge properties
The edge properties can be customized using four options. After creating the d3.graph()
, the edges are based on the strength of the vertices.
Edge network properties can also be changed for the edges:
Note
weight
edge_distance
edge_distance_minmax
directed
Customize edge_properties
# Import library
from d3graph import d3graph
# Initialization
d3 = d3graph()
# Load karate example
adjmat = d3.import_example('bigbang')
# Process the adjacency matrix
d3.graph(adjmat)
# Examine the node properties
print(d3.edge_properties)
# ('Sheldon', 'Amy'): {'weight': 5.0, 'weight_scaled': 20.0, 'color': '#000000'},
# ('Sheldon', 'Howard'): {'weight': 2.0, 'weight_scaled': 1.0, 'color': '#000000'},
# ('Sheldon', 'Leonard'): {'weight': 3.0,'weight_scaled': 7.3333, 'color': '#000000'}}
# ...
# Customize the properties of one specific edge
d3.edge_properties[('Sheldon', 'Howard')]['weight']=10
# Plot
d3.show()
Customize all edges
# Set edge properties with a edge distance
d3.set_edge_properties(edge_distance=100)
# Plot
d3.show()