Heatmap

D3Blocks.heatmap(df, scaler='zscore', color='cluster', cmap='Set2', filepath='heatmap.html', title='Heatmap - D3blocks', stroke='red', fontsize=10, fontsize_mouseover=18, description=None, cluster_params={'cluster': 'agglomerative', 'evaluate': 'silhouette', 'linkage': 'complete', 'max_clust': 25, 'metric': 'euclidean', 'min_clust': 3, 'normalize': False}, figsize=[720, 720], showfig=True, overwrite=True, notebook=False, save_button: bool = True, return_html: bool = False, reset_properties=True)

Heatmap block.

heatmap is a Python package to create interactive heatmaps based on d3js. The heatmap allows interactive clustering where the cluster coloring can be customized. Clusters are colored and within each cluster the color is incremental based on the value. Adjacency matrix must be symetric.

Parameters:
  • df (pd.DataFrame()) – Input data. The index and column names are used for the row/column naming.

  • scaler (str, (default: 'zscore')) – 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.

  • color (str or list) –

    Class label to color the clustering.
    • ’cluster’: colors are based on clustering

    • ’label’: colors are based on the presence of unique labels

  • stroke (String, (default: 'red').) –

    Color of the recangle when hovering over a cell.
    • ’red’

    • ’black’

  • fontsize (int, (default: 10)) – The fontsize of the columns and rows

  • fontsize_mouseover (int, (default: 10)) – The fontsize of the columns and rows with mouse-over

  • description (String, (default: 'Heatmap description')) – Description text of the heatmap.

  • cluster_params (dict (defaults)) – Parameters for clustering the data and using the cluster labels to color the heatmap. See references for more information.

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

    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’

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

    Title of the figure.
    • ’Heatmap’

  • 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].
    • [800, 800]

  • 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()

Examples

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

Examples

>>> # Load d3blocks
>>> from d3blocks import D3Blocks
>>> #
>>> # Initialize
>>> d3 = D3Blocks()
>>> #
>>> # Load example data
>>> df = d3.import_example('energy')
>>> #
>>> # Change cluster parameters
>>> d3.heatmap(df, cluster_params={'evaluate':'dbindex',
>>>                                'metric':'hamming',
>>>                                'linkage':'complete',
>>>                                'normalize': False,
>>>                                'min_clust': 3,
>>>                                'max_clust': 15})
>>> #

Examples

>>> # Initialize
>>> d3 = D3Blocks()
>>> #
>>> # Load example data
>>> df = d3.import_example('bigbang')
>>> #
>>> # Plot and color on label
>>> d3.heatmap(df, color=[1,1,1,2,2,2,3])
>>> d3.node_properties
>>> #
>>> # Plot and specify the hex color
>>> d3.heatmap(df, color=['#FFF000', '#FFF000', '#FFF000', '#000FFF' , '#000FFF', '#000FFF', '#000FFF'])
>>> d3.node_properties

Examples

>>> # Initialize
>>> d3 = D3Blocks()
>>> #
>>> # Network graph
>>> d3.d3graph(df, charge=800, collision=2, showfig=True)
>>> #
>>> # Extract the node colors from the network graph.
>>> node_colors = d3.D3graph.node_properties
>>> #
>>> # Heatmap
>>> d3 = D3Blocks()
>>> # Create the heatmap but do not show it yet because we first need to adjust the colors
>>> d3.heatmap(df, showfig=False)
>>> # Update the colors of the network graph to be consistent with the colors
>>> d3.node_properties
>>> #
>>> for i, label in enumerate(d3.node_properties['label']):
>>>     if node_colors.get(label) is not None:
>>>         d3.node_properties.loc[i, 'color'] = node_colors.get(label)['color']
>>> #
>>> d3.show(showfig=True, figsize=[600, 600], fontsize=8, scaler='zscore')

References

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]

Charts