Demo of Plotlyviz features

Last run 2021-10-08 with kmapper version 2.0.1

To visualize a Kepler Mapper graph with plotly, we’ve built some handy constructors to help you. This notebook will detail some of the features so you are better able to customize the outputs.

%load_ext autoreload
%autoreload 2
%matplotlib inline
# Necessary imports

import sklearn
from sklearn import datasets

import kmapper as km
from kmapper.plotlyviz import plotlyviz
data, labels = datasets.make_circles(n_samples=5000, noise=0.05, factor=0.3)

# Initialize
mapper = km.KeplerMapper(verbose=0)

# Fit to and transform the data
lens = mapper.fit_transform(data, projection=[0])

# Create dictionary called 'simplicial_complex' with nodes, edges and meta-information
simplicial_complex =, X=data,
                                cover=km.Cover(n_cubes=20, perc_overlap=0.1))

Basic Usage

All plotly outputs come with node tooltips that display the cluster id and basic graph information.

plotlyviz(simplicial_complex, title="Basic plot")


To see more information about each of the nodes, you can use parameters dashboard=True or vbox=True to include node histograms and node membership data.

Hover the graph nodes in the next cell to see their distribution in the right figure. Under the graphs notice the contents of two text widgets: the cluster size, repsctively the member ids or their labels.

plotlyviz(simplicial_complex, title="Dashboard plot", dashboard=True)
plotlyviz(simplicial_complex, title="Dashboard plot", graph_data=True)

Color palettes

You can define new color palettes using the plotly specs for this.

To color the graph nodes or the bars in the node distribution we can choose a Plotly colorscale. The default colorscale set in kmapper.plotlyviz is Viridis. Viridis is the choice over Jet colorscale, by the data visuzlization community, because Jet doesn’t meet scientific standards concerning luminance gradient, and more. It is just colorful but doesn’t convey right information.

Viridis is the default colormap in Matplotlib and R. Matlab also changed Jet by Parula.

A Plotly colorscale is a list of lists, and each inner list contains a float number in [0,1], and a color code. The color codes however are not tuples of float values in [0,1], like in matplotlib, but tuples of integers (np.uint8) between 0 and 255. or hex colorcodes.

pl_matter = [[0.0, 'rgb(253, 237, 176)'], #derived from
             [0.1, 'rgb(250, 202, 143)'],
             [0.2, 'rgb(245, 166, 114)'],
             [0.3, 'rgb(238, 132, 93)'],
             [0.4, 'rgb(226, 97, 82)'],
             [0.5, 'rgb(206, 67, 86)'],
             [0.6, 'rgb(179, 46, 94)'],
             [0.7, 'rgb(147, 31, 99)'],
             [0.8, 'rgb(114, 25, 95)'],
             [0.9, 'rgb(79, 21, 82)'],
             [1.0, 'rgb(47, 15, 61)']]

pl_brewer = [[0.0, '#006837'], #from green to red
             [0.1, '#1a9850'],
             [0.2, '#66bd63'],
             [0.3, '#a6d96a'],
             [0.4, '#d9ef8b'],
             [0.5, '#ffffbf'],
             [0.6, '#fee08b'],
             [0.7, '#fdae61'],
             [0.8, '#f46d43'],
             [0.9, '#d73027'],
             [1.0, '#a50026']]

pl_jet = [[0.0, 'rgb(0, 0, 127)'], #derived for matplotlib jet
          [0.1, 'rgb(0, 0, 241)'],
          [0.2, 'rgb(0, 76, 255)'],
          [0.3, 'rgb(0, 176, 255)'],
          [0.4, 'rgb(41, 255, 205)'],
          [0.5, 'rgb(124, 255, 121)'],
          [0.6, 'rgb(205, 255, 41)'],
          [0.7, 'rgb(255, 196, 0)'],
          [0.8, 'rgb(255, 103, 0)'],
          [0.9, 'rgb(241, 7, 0)'],
          [1.0, 'rgb(127, 0, 0)']]
plotlyviz(simplicial_complex, colorscale=pl_matter, title="Dashboard plot", dashboard=True)

Construct plotly scales from Matplotlib

import numpy as np
import as cm
import cmocean #
from kmapper.plotlyviz import mpl_to_plotly

plotly_RdYlBu = mpl_to_plotly(cm.RdYlBu, 11)
plotly_delta = mpl_to_plotly(, 11)
plotlyviz(simplicial_complex, colorscale=plotly_RdYlBu, title="Dashboard plot", dashboard=True)
plotlyviz(simplicial_complex, colorscale=plotly_delta, title="Dashboard plot", dashboard=True)

Different Layouts

The default layout is Kamada-Kawai. It is possible to specify other layouts to use with the graph_layout keyword argument.

Currently, the two supported options are Kamada-Kawaii by 'kk' and Furchtenberg-Reingold by fr.

plotlyviz(simplicial_complex, graph_layout='fr', title='Kepler-Mapper graph for circles dataset with<br> Fruchtenberg-Reingold layout')

Save figure

To save the figure, just include an argument filename=<your filename> to the constructor. You can save as a png, pdf, svg image file, as well as eps file (the eps files cannot be defined/saved under the Windows OS).

Warning You’ll need to have the psutil package installed. You can use either pip or conda for this.

# !pip install psutil
#plotlyviz(simplicial_complex, colorscale=pl_matter, title="Dashboard plot", filename="my_circles_export.pdf")

Further customization

We’ve supplied one nice function to handle the sanest defaults for you convenience. If you find yourself needing further customization, look at the code in plotlyviz() function and customize at will. If you build a new constructor you find helpful, we would be thrilled if you added it to the package and sent us a pull request.