Plotapi, beautiful by default.

Let plotapi do the heavy lifting – enabling beautiful interactive visualisations with a single line of code (instead of hundreds).

Get Plotapi

Changing the Colours


In [1]:
from plotapi import Sankey

Sankey.set_license("your username", "your license key")


Let's take a look at the colors parameter. This will allow us to select from a predefined set of colours, or even pass in our own! We'll also look at how we can override colours for specific nodes.

As we can see, we have set our license details in the preamble with Sankey.set_license().


Plotapi Sankey expects a list of dictionary items, these will define the flow between a source and a target.

In [2]:
links = [
    {"source":"Group A", "target":"Rank 1", "value": 1000},
    {"source":"Group B", "target":"Rank 1", "value": 300},
    {"source":"Group B", "target":"Rank 2", "value": 600},
    {"source":"Group B", "target":"Rank 3", "value": 400},
    {"source":"Rank 1", "target":"Club A", "value": 700},
    {"source":"Rank 1", "target":"Club B", "value": 400},
    {"source":"Rank 1", "target":"Club C", "value": 200},
    {"source":"Rank 2", "target":"Club B", "value": 200},
    {"source":"Rank 2", "target":"Club C", "value": 400},
    {"source":"Rank 3", "target":"Withdrawn", "value": 400},
    {"source":"Club A", "target":"The Most Amazing Prize", "value": 500},

We can add many source's and target's in any arrangement.


Changing the Colours

To specify colours, we can set the colors parameter to one of the following:

  • A list of colour strings, e.g. ["#264653","#2a9d8f","#e9c46a","#f4a261","#e76f51"]
  • The string name of a predefined set of colours from the following list: ['monsters', 'category10', 'accent', 'dark2', 'paired', 'pastel1', 'pastel2', 'set1', 'set2', 'set3', 'tableau10', 'rainbow', 'sinebow', 'yellow_red', 'yellow_brown', 'yellow_green', 'yellow_blue', 'red_purple', 'purple_red', 'purple_blue', 'orange_red', 'green_blue', 'blue_purple', 'blue_green', 'cubehelix', 'cool', 'warm', 'cividis', 'plasma', 'magma', 'inferno', 'viridis', 'turbo', 'brown_green', 'purple_green', 'pink_green', 'red_blue', 'red_grey', 'red_yellow_blue', 'red_yellow_green', 'spectral', 'blues', 'greens', 'greys', 'oranges', 'purples', 'reds'].

The default colour scheme is "rainbow". Let's demonstrate a few colour schemes.

Here we're using .show() and show_png() which outputs to a Jupyter Notebook cell, however, we may want to output to a file with .to_html() or .to_png() instead. More on the different output methods later!

In [3]:
Sankey(links, colors="cool").show()
Plotapi - Sankey Diagram
In [4]:
Sankey(links, colors="spectral").show_png()
In [5]:
Sankey(links, colors=['red', 'green', 'blue']).show_png()

Overriding Specific Node Colours

We can override specific node colours by constructing and supplying the following data structure. The name corresponds to either a source or target string from the links data structure, and the colour can be a HEX colour code or string alias.

In [6]:
nodes = [
    {"name": "Rank 1", "color": "red"}

Finally, all we need to do is pass in the nodes parameter.

In [8]:
Sankey(links, nodes=nodes, colors="greys").show_png()

You can do so much more than what's presented in this example, and we'll cover this in later sections. If you want to see the full list of growing features, check out the Plotapi Documentation.

Made with Plotapi

You can create beautiful, interactive, and engaging visualisations like this one in any programming language with Plotapi.

Get the Books

Enjoying these notebooks and want more on the subject? Check out the practical books on Data Science, Visualisation, and Evolutionary Algorithms.

Get the books