1

Changing the Colours and Icons

 2 years ago
source link: https://datacrayon.com/posts/plotapi/barfight/changing-the-colours-and-icons/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Preamble

from plotapi import BarFight

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

Introduction

Let's take a look at how we can change the colours and icons for nodes in our Bar Fight diagram.

Plotapi Bar Fight is a beautiful and feature rich take on the popular Bar Chart Race. As we can see, we have set our license details in the preamble with BarFight.set_license().

Dataset

Plotapi Bar Fight expects at minimum a list of dictionary items, these will define the value of our segments over time.

samples = [
    {"order": 0, "name": "Sankey", "value": 10},
    {"order": 0, "name": "Terminus", "value": 12},
    {"order": 0, "name": "Chord", "value": 8},
    {"order": 0, "name": "Bar Fight", "value": 9},
    {"order": 0, "name": "Pie Fight", "value": 12},

    {"order": 1, "name": "Sankey", "value": 18},
    {"order": 1, "name": "Terminus", "value": 24},
    {"order": 1, "name": "Chord", "value": 22},
    {"order": 1, "name": "Bar Fight", "value": 14},
    {"order": 1, "name": "Pie Fight", "value": 17},

    {"order": 2, "name": "Sankey", "value": 24},
    {"order": 2, "name": "Terminus", "value": 40},
    {"order": 2, "name": "Chord", "value": 32},
    {"order": 2, "name": "Bar Fight", "value": 19},
    {"order": 2, "name": "Pie Fight", "value": 42},

    {"order": 3, "name": "Sankey", "value": 32},
    {"order": 3, "name": "Terminus", "value": 62},
    {"order": 3, "name": "Chord", "value": 40},
    {"order": 3, "name": "Bar Fight", "value": 25},
    {"order": 3, "name": "Pie Fight", "value": 64},

    {"order": 4, "name": "Sankey", "value": 38},
    {"order": 4, "name": "Terminus", "value": 75},
    {"order": 4, "name": "Chord", "value": 55},
    {"order": 4, "name": "Bar Fight", "value": 45},
    {"order": 4, "name": "Pie Fight", "value": 120},
]

We can see that each dictionary item has three properties:

  • order which determines with time period this item belongs to. This should be numerical, but can be formatted e.g. as dates.
  • name the name of the item, and the text that appears on the bar.
  • value the value of the bar at the associated point in time.

Next, we'll start customising the bars, or nodes, that will represent our data over time. Plotapi Bar Fight expects a list of dictionary items to configure each node.

nodes = [
    {
        "name": "Sankey",
        "color": "#ffd166", 
        "icon": "https://datacrayon.com/datasets/pokemon_img/003.png"
    },
    {
        "name": "Terminus",
        "color": "#06d6a0", 
        "icon": "https://datacrayon.com/datasets/pokemon_img/004.png"
    },
    {
        "name": "Chord",
        "color": "#118ab2", 
        "icon": "https://datacrayon.com/datasets/pokemon_img/025.png"
    },
    {
        "name": "Bar Fight",
        "color": "#073b4c", 
        "icon": "https://datacrayon.com/datasets/pokemon_img/151.png"
    },
    {
        "name": "Pie Fight",
        "color": "#ef476f", 
        "icon": "https://datacrayon.com/datasets/pokemon_img/232.png"
    }
]

We can see that each dictionary item has three properties:

  • name, the name of the item, which corresponds to names specified in the samples definitions above.
  • color, the desired colour of the bars (CSS colour e.g. "#073b4c" or "red").
  • icon, the location of the image to use for the icon.

Visualisation

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 default settings can do!

BarFight(samples,
         nodes=nodes).show()

Plotapi - Bar Fight Diagram

0246810121416182015.43Sankey20.14Terminus17.50Chord12.39Bar Fight15.39Pie Fight1Produced with Plotapi

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK