D3graph
Note
This page contains a summary of the possiblities what you can do with D3graph. More details can be found on the D3graph documentation pages.
d3graph block.
d3graph is integrated in d3blocks and is to create interactive and stand-alone D3 force-directed graphs. The input data is a dataframe containing source, target, and weight. In underneath example, we load the energy dataset which contains 68 relationships that are stored in a DataFrame with the columns source, target, and weight. The nodes are colored based on the Louvain heuristics which is the partition of highest modularity, i.e. the highest partition of the dendrogram generated by the Louvain algorithm. The strength of the edges is based on the weights. To explore the network, and the strength of the edges more extensively, the slider (located at the top) can break the network based on the edge weights. The ouput is a html file that is interactive and stand alone. For demonstration purposes, the “energy” and “stormofswords” dataset can be used.
- param df:
- Input data containing the following columns:
‘source’
‘target’
‘weight’
- type df:
pd.DataFrame()
- param color:
- Color of the node.
‘cluster’ : Colours are based on the community distance clusters.
None: All nodes will have the same color (auto generated).
[‘#000000’]: All nodes will have the same hex color.
[‘#377eb8’,’#ffffff’,’#000000’,…]: Hex colors are directly used.
[‘A’]: All nodes will have hte same color. Color is generated on CMAP and the unique labels.
[‘A’,’A’,’B’,…]: Colors are generated using cmap and the unique labels accordingly colored.
- type color:
list of strings (default: ‘#000080’)
- param size:
- Size of the nodes.
‘degree’ opacity is based on the centrality measure.
10: all nodes sizes are set to 10
[10, 5, 3, 1, …]: Specify node sizes
- type size:
array of integers (default: 5)
- param opacity:
- Set the opacity of the node [0-1] where 0=transparant and 1=no transparancy.
None: Colors are inhereted from the initialization
‘degree’ opacity is based on the centrality measure.
0.99: All nodes will get this transparancy
[‘0.4, 0.1, 0.3,…]
[‘A’,’A’,’B’,…]: Opacity is generated using cmap and according to the unique labels.
- type opacity:
list of floats (default: ‘degree’)
- param scaler:
Scale the edge-width using the following scaler: ‘zscore’ : Scale values to Z-scores. ‘minmax’ : The sklearn scaler will shrink the distribution between minmax. None : No scaler is used.
- type scaler:
str, (default: ‘zscore’)
- param collision:
Response of the network. Higher means that more collisions are prevented.
- type collision:
float, (default: 0.5)
- param charge:
Edge length of the network. Towards zero becomes a dense network. Higher make edges longer.
- type charge:
int, (default: 400)
- param cmap:
- All colors can be reversed with ‘_r’, e.g. ‘binary’ to ‘binary_r’
‘tab20c’, ‘Set1’, ‘Set2’, ‘rainbow’, ‘bwr’, ‘binary’, ‘seismic’, ‘Blues’, ‘Reds’, ‘Pastel1’, ‘Paired’, ‘twilight’, ‘hsv’, ‘inferno’
- type cmap:
String, (default: ‘Set1’)
- param slider:
Slider is automatically set to the range of the edge weights.
- type slider:
typle [min: int, max: int]:, (default: [None, None])
- param set_slider:
0: Set the the slider with all edges connected 1,2,3, etc: Set slider at a threshold with that particular network state.
- type set_slider:
int, (default: 0)
- param show_slider:
True: Slider is shown in the HTML. False: Slider is not shown in the HTML.
- type show_slider:
bool, (default: True)
- param title:
- Title of the figure.
‘d3graph’
- type title:
String, (default: None)
- param filepath:
- File path to save the output.
Temporarily path: ‘d3blocks.html’
Relative path: ‘./d3blocks.html’
Absolute path: ‘c://temp//d3blocks.html’
None: Return HTML
- type filepath:
String, (Default: user temp directory)
- param figsize:
- Size of the figure in the browser, [width, height].
[1500, 800]
- type figsize:
tuple
- param showfig:
True: Open browser-window.
False: Do not open browser-window.
- type showfig:
bool, (default: True)
- param notebook:
True: Use IPython to show chart in notebook.
False: Do not use IPython.
- type notebook:
bool
- param save_button:
True: Save button is shown in the HTML to save the image in svg.
False: No save button is shown in the HTML.
- type save_button:
bool, (default: True)
- param overwrite:
True: Overwrite the html in the destination directory.
False: Do not overwrite destination file but show warning instead.
- type overwrite:
bool, (default: True)
- rtype:
None.
Examples
>>> # Load library
>>> from d3blocks import D3Blocks
>>> #
>>> # Initialize
>>> d3 = D3Blocks()
>>> #
>>> # Import example
>>> df = d3.import_example('energy') # 'bigbang', 'stormofswords'
>>> #
>>> # Create network using default
>>> d3.d3graph(df, filepath='d3graph.html')
>>> #
>>> # Change scaler
>>> d3.d3graph(df, scaler='minmax')
>>> #
>>> # Change node properties
>>> d3.D3graph.set_node_properties(color=None)
>>> d3.D3graph.node_properties['Solar']['size']=30
>>> d3.D3graph.node_properties['Solar']['color']='#FF0000'
>>> d3.D3graph.node_properties['Solar']['edge_color']='#000000'
>>> d3.D3graph.node_properties['Solar']['edge_size']=5
>>> d3.D3graph.show()
>>> #
>>> # Change edge properties
>>> d3.D3graph.set_edge_properties(directed=True, marker_end='arrow')
>>> d3.D3graph.show()
>>> #
>>> # Node properties
>>> d3.D3graph.node_properties
>>> #
>>> # Node properties
>>> d3.D3graph.edge_properties
>>> #
>>> # After making changes, show the graph again using show()
>>> d3.D3graph.show()
References
Github : https://github.com/erdogant/d3graph
Documentation: https://erdogant.github.io/d3graph/
Input Data
The input dataset is a DataFrame with three column, source, target and weight.
# source target weight
# 0 Agricultural 'waste' Bio-conversion 124.729
# 1 Bio-conversion Liquid 0.597
# 2 Bio-conversion Losses 26.862
# 3 Bio-conversion Solid 280.322
# 4 Bio-conversion Gas 81.144
# .. ... ... ...
# 63 Thermal generation District heating 79.329
# 64 Tidal Electricity grid 9.452
# 65 UK land based bioenergy Bio-conversion 182.010
# 66 Wave Electricity grid 19.013
# 67 Wind Electricity grid 289.366
# [68 rows x 3 columns]