Note
Click here to download the full example code
Network layout¶
Example illustrating the network layout
import os
import numpy as np
import pandas as pd
from netchos import network
import plotly.io as pio
import plotly.graph_objects as go
from plotly.subplots import make_subplots
pio.templates.default = 'plotly_white'
Load the data¶
# load the connectivity 2D matrix
ufc = pd.read_excel('ufc.xlsx', index_col=0)
print(ufc)
# load a table that contains informations about the nodes
ma = pd.read_excel('ma.xlsx')
print(ma)
# computes nodes' degree
ma['degree'] = (~np.isnan(ufc.values)).sum(0)
# compute node's strength
ma['strength'] = np.nansum(ufc, axis=0)
Out:
/home/runner/work/netchos/netchos/examples/plot_network.py:27: FutureWarning:
Your version of xlrd is 1.2.0. In xlrd >= 2.0, only the xls format is supported. As a result, the openpyxl engine will be used if it is installed and the engine argument is not specified. Install openpyxl instead.
aINS pINS ACC PFCvm ... Cu VCs VCl VCcm
sources ...
aINS 0.005500 0.001185 NaN NaN ... NaN NaN NaN NaN
pINS 0.001185 NaN NaN NaN ... NaN NaN NaN NaN
ACC NaN NaN NaN NaN ... NaN NaN NaN NaN
PFCvm NaN NaN NaN NaN ... NaN NaN NaN NaN
vmPFC NaN NaN NaN NaN ... NaN NaN NaN NaN
OFCvm NaN NaN NaN NaN ... NaN NaN NaN NaN
OFCv NaN NaN NaN NaN ... NaN NaN NaN NaN
lOFC NaN NaN NaN NaN ... NaN NaN NaN NaN
OFCvl NaN NaN NaN NaN ... NaN NaN NaN NaN
PFrm NaN NaN NaN NaN ... NaN NaN NaN NaN
PFrd NaN NaN NaN NaN ... NaN NaN NaN NaN
Pfrdls NaN NaN NaN NaN ... NaN NaN NaN NaN
dlPFC 0.001219 NaN NaN NaN ... NaN NaN 0.001760 0.012980
Pfrdli NaN NaN NaN NaN ... NaN NaN NaN NaN
PFrvl 0.003716 NaN NaN NaN ... NaN NaN 0.003139 0.007489
MCC NaN NaN NaN NaN ... NaN NaN NaN NaN
PFcdm NaN NaN NaN NaN ... NaN NaN NaN NaN
PFcdl NaN NaN NaN NaN ... NaN NaN NaN NaN
PMdm NaN NaN NaN NaN ... NaN NaN NaN NaN
PMdl NaN NaN NaN NaN ... NaN NaN NaN NaN
PMrv NaN NaN NaN NaN ... NaN NaN NaN NaN
Mdm NaN NaN NaN NaN ... NaN NaN NaN NaN
Mdl NaN 0.002862 NaN NaN ... 0.008246 NaN 0.002460 NaN
Mv 0.005842 NaN NaN NaN ... NaN NaN NaN NaN
Sdm NaN NaN NaN NaN ... NaN NaN NaN NaN
Sdl NaN NaN NaN NaN ... NaN NaN NaN NaN
Sv NaN NaN NaN NaN ... NaN NaN NaN NaN
PCC NaN NaN NaN NaN ... NaN NaN NaN NaN
PCm NaN NaN NaN NaN ... NaN NaN NaN NaN
SPCm NaN NaN NaN NaN ... NaN NaN NaN NaN
SPC NaN NaN NaN NaN ... NaN NaN NaN NaN
IPCd NaN NaN NaN NaN ... NaN NaN NaN NaN
IPCv NaN NaN NaN NaN ... NaN NaN 0.001141 0.002808
ICC NaN NaN NaN NaN ... NaN NaN NaN NaN
MTCr NaN NaN NaN NaN ... NaN NaN NaN NaN
STCr NaN NaN NaN NaN ... NaN NaN NaN NaN
STCc NaN NaN NaN NaN ... NaN NaN 0.001088 0.002290
MTCc 0.002699 NaN NaN NaN ... 0.020681 NaN 0.000745 0.001717
ITCr 0.002164 NaN NaN NaN ... NaN NaN NaN NaN
ITCm 0.001082 NaN NaN NaN ... NaN NaN 0.002184 0.006301
VCrm NaN NaN NaN NaN ... NaN NaN NaN NaN
Cu NaN NaN NaN NaN ... NaN NaN NaN NaN
VCs NaN NaN NaN NaN ... NaN NaN NaN NaN
VCl NaN NaN NaN NaN ... NaN NaN 0.001851 0.005478
VCcm NaN NaN NaN NaN ... NaN NaN 0.005478 0.014947
[45 rows x 45 columns]
/home/runner/work/netchos/netchos/examples/plot_network.py:31: FutureWarning:
Your version of xlrd is 1.2.0. In xlrd >= 2.0, only the xls format is supported. As a result, the openpyxl engine will be used if it is installed and the engine argument is not specified. Install openpyxl instead.
Unnamed: 0 Name LR_Name ... ycoord_3D zcoord_3D lobe_color
0 0 aINS aINS ... 33.0 49.0 #636EFA
1 1 pINS pINS ... 33.0 49.0 #636EFA
2 2 ACC ACC ... 5.0 51.0 #636EFA
3 3 PFCvm PFCvm ... 7.0 37.0 #636EFA
4 4 vmPFC vmPFC ... 11.0 34.0 #636EFA
5 5 OFCvm OFCvm ... 15.0 31.0 #636EFA
6 6 OFCv OFCv ... 27.0 33.0 #636EFA
7 7 lOFC lOFC ... 31.0 33.0 #636EFA
8 8 OFCvl OFCvl ... 35.0 33.0 #636EFA
9 9 PFrm PFrm ... 5.0 68.0 #636EFA
10 10 PFrd PFrd ... 11.0 69.0 #636EFA
11 11 Pfrdls Pfrdls ... 24.0 62.0 #636EFA
12 12 dlPFC dlPFC ... 29.5 58.5 #636EFA
13 13 Pfrdli Pfrdli ... 35.0 55.0 #636EFA
14 14 PFrvl PFrvl ... 41.0 49.0 #636EFA
15 15 MCC MCC ... 4.0 79.0 #636EFA
16 16 PFcdm PFcdm ... 9.0 87.0 #636EFA
17 17 PFcdl PFcdl ... 32.0 78.0 #636EFA
18 18 PMdm PMdm ... 10.0 99.0 #636EFA
19 19 PMdl PMdl ... 30.0 89.0 #636EFA
20 20 PMrv PMrv ... 42.0 58.0 #636EFA
21 21 Mdm Mdm ... 9.0 104.0 #636EFA
22 22 Mdl Mdl ... 37.0 90.0 #636EFA
23 23 Mv Mv ... 48.0 61.0 #636EFA
24 24 Sdm Sdm ... 10.0 106.0 #EF553B
25 25 Sdl Sdl ... 37.0 96.0 #EF553B
26 26 Sv Sv ... 50.0 70.0 #EF553B
27 27 PCC PCC ... 3.0 80.0 #EF553B
28 28 PCm PCm ... 5.0 88.0 #EF553B
29 29 SPCm SPCm ... 13.0 104.0 #EF553B
30 30 SPC SPC ... 27.0 99.0 #EF553B
31 31 IPCd IPCd ... 41.0 94.0 #EF553B
32 32 IPCv IPCv ... 48.0 76.0 #EF553B
33 33 ICC ICC ... 11.0 59.0 #EF553B
34 34 MTCr MTCr ... 40.0 22.0 #00CC96
35 35 STCr STCr ... 43.0 36.0 #00CC96
36 36 STCc STCc ... 50.0 57.0 #00CC96
37 37 MTCc MTCc ... 51.0 54.0 #00CC96
38 38 ITCr ITCr ... 37.0 25.0 #00CC96
39 39 ITCm ITCm ... 26.0 37.0 #00CC96
40 40 VCrm VCrm ... 15.0 57.0 #AB63FA
41 41 Cu Cu ... 11.0 78.0 #AB63FA
42 42 VCs VCs ... 33.0 83.0 #AB63FA
43 43 VCl VCl ... 42.0 57.0 #AB63FA
44 44 VCcm VCcm ... 20.0 55.0 #AB63FA
[45 rows x 13 columns]
Default 2D layout¶
fig = network(
ufc, # 2D connectivity matrix
nodes_data=ma, # dataframe with data attached to each node
nodes_x='xcoord_2D', # x-coordinate name in nodes_data table
nodes_y='ycoord_2D' # y-coordinate name in nodes_data table
)
pio.show(fig)
Default 3D layout¶
fig = network(
ufc, # 2D connectivity matrix
nodes_data=ma, # dataframe with data attached to each node
nodes_x='xcoord_3D', # x-coordinate name in nodes_data table
nodes_y='ycoord_3D', # y-coordinate name in nodes_data table
nodes_z='zcoord_3D' # z-coordinate name in nodes_data table
)
pio.show(fig)
Control of aesthetics¶
# sphinx_gallery_thumbnail_number = 3
fig = network(
ufc, nodes_data=ma,
nodes_x='xcoord_2D', # x-coordinate (column name in ma)
nodes_y='ycoord_2D', # y-coordinate (column name in ma)
nodes_color='degree', # color of the node given by the degree
nodes_size='strength', # marker size proportional to the strentgh
nodes_size_min=1., # minimum size of the nodes
nodes_size_max=30., # maximum size of the nodes
nodes_cmap='agsunset_r', # colormap associated to the nodes
edges_cmap='agsunset_r', # colormap associated to the edges
edges_opacity_min=.5, # weak connections semi-transparents
edges_opacity_max=1., # strong connections opaques
cbar_title='UFC'
)
title = '<b>Control of aesthetics</b>'
fig.update_layout(template='plotly_dark', title=title, title_x=.5)
pio.show(fig)
Network layout in subplots¶
fig = make_subplots(rows=1, cols=2, subplot_titles=('Subplot 1', 'Subplot 2'))
# configuring the first subplot
network(
ufc, nodes_data=ma, nodes_x='xcoord_2D', nodes_y='ycoord_2D',
nodes_name='Name', nodes_size='degree', nodes_color='degree',
nodes_cmap='plasma_r', edges_cmap='plasma_r', fig=fig,
edges_opacity_min=0., edges_opacity_max=1., kw_trace=dict(row=1, col=1),
kw_cbar=dict(x=0.45)
)
# configuring the second subplot
network(
ufc, nodes_data=ma, nodes_x='xcoord_2D', nodes_y='ycoord_2D',
nodes_name='Name', nodes_size='strength', nodes_color='strength',
nodes_cmap='magma_r', edges_cmap='magma_r', fig=fig,
edges_opacity_min=.6, edges_opacity_max=.8, kw_trace=dict(row=1, col=2),
kw_cbar=dict(x=1.)
)
title = "<b>Illustration of adding network layouts to subplots</b>"
fig.update_layout(width=1200, height=600, title=title, title_x=0.5)
pio.show(fig)
Total running time of the script: ( 0 minutes 6.603 seconds)