Chord

D3Blocks.chord(df, color='source', opacity='source', ordering='ascending', fontsize=10, arrowhead=10, cmap='tab20', title='Chord - D3blocks', filepath='chord.html', figsize=[900, 900], showfig=True, overwrite=True, notebook=False, save_button=True, margin=150, text_offset=5, return_html: bool = False, reset_properties=True)

Chord block.

A chord represents flows or connections between several entities or nodes. Each entity is represented by a fragment on the outer part of the circular layout. Then, arcs are drawn between each entity. The size of the arc is proportional to the importance of the flow.

Parameters:
  • df (pd.DataFrame()) –

    Input data containing the following columns:
    • ”source”

    • ”target”

    • ”weight”

    • ”color” (optional)

    • ”opacity” (optional)

  • color ((default: 'source')) –

    Link colors in Hex notation. Should be the same size as input DataFrame.
    • ”source” : Color edges/links similar to that of source-color node.

    • ”target” : Color edges/links similar to that of target-color node.

    • ”source-target” : Color edges/link based on unique source-target edges using the colormap.

    • ”#ffffff” : All links have the same hex color.

    • [“#000000”, “#ffffff”,…] : Define per link.

  • opacity ((default: 'source')) –

    Link Opacity. Should be the same size as input DataFrame.
    • ’source’: Opacity of edges/links similar to that of source-opacity node.

    • ’target’: Opacity of edges/links similar to that of target-opacity node.

    • 0.8: All links have the same opacity.

    • [0.1, 0.75,…]: Set opacity per edge/link.

  • ordering ((default: 'ascending')) –

    Sort the labels.
    • ’ascending’

    • ’descending’

    • ’’: Do not sort

    • [‘label1’, ‘label2’, ‘label3’]: Custom sort. Note that all labels shoul be included only once for the best result.

  • fontsize (int, (default: 8)) – The Fontsize.

  • arrowhead (int, (default: 10)) –

    The head of the arrow.
    • -1: No arrow

    • 10: default

    • 50: The larger the more pointy the arrow becomes

  • cmap (String, (default: 'tab20')) –

    colormap is only used in case color=None. 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’

  • title (String, (default: None)) –

    Title of the figure.
    • ’Chord’

  • filepath (String, (Default: user temp directory)) –

    File path to save the output.
    • Temporarily path: ‘d3blocks.html’

    • Relative path: ‘./d3blocks.html’

    • Absolute path: ‘c://temp//d3blocks.html’

    • None: Return HTML

  • figsize (tuple) –

    Size of the figure in the browser, [width, height].
    • [900, 900]

  • showfig (bool, (default: True)) –

    • True: Open browser-window.

    • False: Do not open browser-window.

  • overwrite (bool, (default: True)) –

    • True: Overwrite the html in the destination directory.

    • False: Do not overwrite destination file but show warning instead.

  • notebook (bool) –

    • True: Use IPython to show chart in notebook.

    • False: Do not use IPython.

  • save_button (bool, (default: True)) –

    • True: Save button is shown in the HTML to save the image in svg.

    • False: No save button is shown in the HTML.

  • return_html (bool, (default: False)) –

    • True: Return html

    • False: Nothing is returned

  • reset_properties (bool, (default: True)) –

    • True: Reset the node_properties at each run.

    • False: Use the d3.node_properties()

  • margin (int, (default: 150)) –

    • Margin around the chord diagram in pixels.

    • Larger values create more space around the diagram.

  • text_offset (int, (default: 5)) –

    • Additional offset for text labels in pixels.

    • Larger values move labels further from the chord arcs.

Returns:

  • d3.node_properties (DataFrame of dictionary) – Contains properties of the unique input label/nodes/samples.

  • d3.edge_properties (DataFrame of dictionary) – Contains properties of the unique input edges/links.

  • d3.config (dictionary) – Contains configuration properties.

Examples

>>> # Load d3blocks
>>> from d3blocks import D3Blocks
>>> #
>>> # Initialize
>>> d3 = D3Blocks()
>>> #
>>> # Load example data
>>> df = d3.import_example('energy')
>>> #
>>> # Plot
>>> d3.chord(df)
>>> #

Examples

>>> # Load d3blocks
>>> from d3blocks import D3Blocks
>>> #
>>> # Initialize
>>> d3 = D3Blocks()
>>> #
>>> # Load example data
>>> df = d3.import_example('energy')
>>> #
>>> # Plot with very large margins for long labels
>>> d3.chord(df,
>>>          filepath='chord_large_margins.html',
>>>          figsize=[1200, 1200],  # Even larger figure size
>>>          margin=300,  # Large margin for long labels
>>>          text_offset=50)  # Large text offset

Examples

>>> # Load d3blocks
>>> from d3blocks import D3Blocks
>>> #
>>> # Initialize
>>> d3 = D3Blocks(chart='Chord', frame=False)
>>> #
>>> # Import example
>>> df = d3.import_example('energy')
>>> #
>>> # Node properties
>>> d3.set_node_properties(df, opacity=0.2, cmap='tab20')
>>> d3.set_edge_properties(df, color='source', opacity='source')
>>> #
>>> # Show the chart
>>> d3.show()
>>> #
>>> # Make some edits to highlight the Nuclear node
>>> # d3.node_properties
>>> d3.node_properties.get('Nuclear')['color']='#ff0000'
>>> d3.node_properties.get('Nuclear')['opacity']=1
>>> # Show the chart
>>> #
>>> d3.show()
>>> # Make edits to highlight the Nuclear Edge
>>> d3.edge_properties.loc[(d3.edge_properties['source'] == 'Nuclear') & (d3.edge_properties['target'] == 'Thermal generation'), 'color'] = '#ff0000'
>>> d3.edge_properties.loc[(d3.edge_properties['source'] == 'Nuclear') & (d3.edge_properties['target'] == 'Thermal generation'), 'opacity'] = 0.8
>>> d3.edge_properties.loc[(d3.edge_properties['source'] == 'Nuclear') & (d3.edge_properties['target'] == 'Thermal generation'), 'weight'] = 1000
>>> #
>>> # Show the chart
>>> d3.show()

Examples

>>> # Change the order of the labels.
>>> #
>>> # Load d3blocks
>>> from d3blocks import D3Blocks
>>> #
>>> # Import example
>>> df = d3.import_example('energy')
>>> #
>>> # Custom order of the labels
>>> d3.chord(df, ordering=np.sort(np.unique(df['source'].values)))
>>> #
>>> # Sort Ascending
>>> d3.chord(df, ordering='ascending')
>>> #
>>> # Sort Descending
>>> d3.chord(df, ordering='descending')
>>> #
>>> # Do not sort
>>> d3.chord(df, ordering='')

References

Input Data

The input dataset is a DataFrame with three column, source, target and weight.

#       source       target  weight
# 0      Aemon        Grenn       5
# 1      Aemon      Samwell      31
# 2      Aerys        Jaime      18
# 3      Aerys       Robert       6
# 4      Aerys       Tyrion       5
# ..       ...          ...     ...
# 347   Walder        Petyr       6
# 348   Walder       Roslin       6
# 349   Walton        Jaime      10
# 350  Ygritte       Qhorin       7
# 351  Ygritte  Rattleshirt       9

# [352 rows x 3 columns]

Chart