Changing the Link Background Colour

Preamble

In [1]:
from plotapi import Sankey

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

Introduction

Until we mouseover a Plotapi Sankey link, what we actually see is the background colour. By default, this background colour is set to be the same as the link foreground colour, as defined by the colors parameter, which by default is "gradient".

Let's see how we can change this!

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

Dataset

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.

Visualisation

We can set the link background colour by passing a HEX colour code or string alias into the link_background_color parameter. Let's try setting the link background colour to grey.

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

Be sure to interact with the visualisation to see what the settings can do!

In [5]:
Sankey(links, link_background_color="grey").show()
Plotapi - Sankey Diagram

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.

Changing the Link Colour Mode

Preamble

In [1]:
from plotapi import Sankey

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

Introduction

By default, a Plotapi Sankey diagram colours its links with a gradient moving from the source node colour to the target node colour, with a default opacity of 0.45. This behaviour can be changed - let's see how below!

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

Dataset

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.

Visualisation

Link colour is controlled by the link_color_mode parameter which is set to "gradient" by default, and this gradient can be reversed with reverse_gradients=True if needed. The other options for link_color_mode are "source" which uses the source node's colour, or "target" which uses the target node's colour.

To change the foreground link colour opacity, we set link_opacity with a number between or including $0$ and $1$. The change in opacity is only noticeable when hovering over a node.

Let's demonstrate these below.

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!

Be sure to interact with the visualisation to see what the settings can do!

In [8]:
Sankey(links, link_color_mode="target", link_opacity=0.75).show()
Plotapi - Sankey Diagram
In [4]:
Sankey(links, link_color_mode="source").show_png()
In [5]:
Sankey(links, reverse_gradients=True).show_png()
In [6]:
Sankey(links).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.

Changing the Colours

Preamble

In [1]:
from plotapi import Sankey

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

Introduction

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

Dataset

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.

Visualisation

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