How do degree classes vary by graduates' gender?

Preamble

In [1]:
from plotapi import Terminus
import json

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

Introduction

In this notebook we're going to use Plotapi Terminus to visualise how degree classes vary by graduates' gender. We"ll use Python, but Plotapi can be used from any programming language.

In the Terminus diagram (a type of flow diagram), we can watch the journey of something or someone from some starting category to some ending category. In this case, we'll be watching each unit travel from a graduate's gender to their awarded degree classification.

Dataset

We're going to use data that has been published by the Higher Education Statistics Agency (HESA) in the UK. The data table is titled "UK domiciled first degree qualifiers by classification of first degree, religious belief, sex, age group, disability marker, ethnicity marker, mode of study and academic year" and it can be found at this link.

We will use data from all countries, all modes of study, and from the academic year 2019/20. "Other" was not included due to a small sample size..

The degree class is the result achieved by a student UK, similar to a Grade Point Average (GPA). You can find a mapping between UK degree classifications and GPA here

In [2]:
data = [
    {"source": "Female", "target": "First class", "value": 68190},
    {"source": "Female", "target": "Second class (upper)", "value": 90935},
    {"source": "Female", "target": "Second class (lower)", "value": 25700},
    {"source": "Female", "target": "Third class", "value": 4940},
    {"source": "Female", "target": "Unclassified", "value": 10660},
    
    {"source": "Male", "target": "First class", "value": 47175},
    {"source": "Male", "target": "Second class (upper)", "value": 63870},
    {"source": "Male", "target": "Second class (lower)", "value": 21455},
    {"source": "Male", "target": "Third class", "value": 4350},
    {"source": "Male", "target": "Unclassified", "value": 6860}
]

Visualisation

Let's use Plotapi Terminus for this visualisation, you can see more examples in the Gallery.

We're going to adjust some layout and template parameters. We're setting pixels_per_unit=40 to represent $40$ graduates with a single pixel. This effects the length of the animation, making it shorter than the default of setting.

In [3]:
Terminus(data, bundled=True, pipe_alignment="middle", pixel_size=4, pixels_per_unit=40,
         title="Watch <plotapi_count> graduate's collect their degree").show()
Plotapi - Terminus Diagram

How do degree classes vary by graduates' ethnicity?

Preamble

In [ ]:
from plotapi import Terminus
import json

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

Introduction

In this notebook we're going to use Plotapi Terminus to visualise how degree classes vary by graduates' ethnicity. We"ll use Python, but Plotapi can be used from any programming language.

In the Terminus diagram (a type of flow diagram), we can watch the journey of something or someone from some starting category to some ending category. In this case, we'll be watching each unit travel from a graduate's ethnicity to their awarded degree classification.

Dataset

We're going to use data that has been published by the Higher Education Statistics Agency (HESA) in the UK. The data table is titled "UK domiciled first degree qualifiers by classification of first degree, religious belief, sex, age group, disability marker, ethnicity marker, mode of study and academic year" and it can be found at this link.

We will use data from all countries, all modes of study, and from the academic year 2019/20. "Other" and "Not known" have been combined.

The degree class is the result achieved by a student UK, similar to a Grade Point Average (GPA). You can find a mapping between UK degree classifications and GPA here

In [ ]:
data = [
    {"source": "White", "target": "First class", "value": 92260},
    {"source": "White", "target": "Second class (upper)", "value": 114885},
    {"source": "White", "target": "Second class (lower)", "value": 29325},
    {"source": "White", "target": "Third class", "value": 4775},
    {"source": "White", "target": "Unclassified", "value": 12585},
    
    {"source": "Black", "target": "First class", "value": 4340},
    {"source": "Black", "target": "Second class (upper)", "value": 10640},
    {"source": "Black", "target": "Second class (lower)", "value": 6245},
    {"source": "Black", "target": "Third class", "value": 1555},
    {"source": "Black", "target": "Unclassified", "value": 750},
    
    {"source": "Asian", "target": "First class", "value": 11515},
    {"source": "Asian", "target": "Second class (upper)", "value": 17925},
    {"source": "Asian", "target": "Second class (lower)", "value": 7080},
    {"source": "Asian", "target": "Third class", "value": 1440},
    {"source": "Asian", "target": "Unclassified", "value": 2710},
    
    {"source": "Mixed", "target": "First class", "value": 4575},
    {"source": "Mixed", "target": "Second class (upper)", "value": 6740},
    {"source": "Mixed", "target": "Second class (lower)", "value": 2095},
    {"source": "Mixed", "target": "Third class", "value": 355},
    {"source": "Mixed", "target": "Unclassified", "value": 565},
    
    {"source": "Other/NA", "target": "First class", "value": 1415 + 1370},
    {"source": "Other/NA", "target": "Second class (upper)", "value": 2385 + 2350},
    {"source": "Other/NA", "target": "Second class (lower)", "value": 1065 + 1380},
    {"source": "Other/NA", "target": "Third class", "value": 190 + 985},
    {"source": "Other/NA", "target": "Unclassified", "value": 290 + 635}
]

Visualisation

Let's use Plotapi Terminus for this visualisation, you can see more examples in the Gallery.

We're going to adjust some layout and template parameters. We're setting percentage_by_source=True to see the degree class awarded as a percentage within ethnicity, i.e. each percentage column will sum to 100. We'll also set pixels_per_percentage=50 to control the length of the animation, making it shorter than the default of setting.

In [3]:
Terminus(data, width=1000, percentage_by_source=True, pixels_per_percentage=50,
         title="Watch graduate's collect their degree (by percentage within ethnicity)").show()
Plotapi - Terminus Diagram

Apple's 2021 Q3 Results with Plotapi Sankey

Preamble

In [1]:
from plotapi import Sankey

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

Introduction

In this notebook we're going to use Plotapi Sankey to visualise some of the Apple's filings for the third quarter of 2021.

Dataset

The data can be found in the 10-Q filing dated the 28th of July 2021, i.e. the quarterly report.

In [2]:
links = [
    {"source":"iPhone", "target":"Total Revenue", "value": 39.6},
    {"source":"Mac", "target":"Total Revenue", "value": 8.2},
    {"source":"iPad", "target":"Total Revenue", "value": 7.4},
    {"source":"Wearables & Accessories", "target":"Total Revenue", "value": 8.8},
    {"source":"Services", "target":"Total Revenue", "value": 17.5},

    {"source":"Total Revenue", "target":"Cost of Products", "value": 40.9},
    {"source":"Total Revenue", "target":"Products Gross Profit", "value": 23},
    {"source":"Total Revenue", "target":"Cost of Services", "value": 5.3},
    {"source":"Total Revenue", "target":"Services Gross Profit", "value": 12.2},
]

Visualisation

Let's use Plotapi Sankey for this visualisation, you can see more examples in the Gallery.

We're going to adjust some colors, layout/template parameters, and flip the intro animation on too.

In [3]:
colors = ["#61bb46", "#fdb827", "#f5821f", "#e03a3e", "#963d97", "#009ddc",
          "red", "lightgreen", "red", "lightgreen",]

Sankey(links, colors=colors, animated_intro=True,
       link_verb="$", node_verb="$", noun="billion", 
       link_numbers_template="$<plotapi_value>bn").show()
Plotapi - Sankey Diagram

Animations: Intros and Loops

Preamble

In [1]:
from plotapi import Chord

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

Introduction

Plotapi Chord supports animations - both for looping and for nice introductions to your visualisation.

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

Dataset

Chord expects a list of names (list[str]) and a co-occurence matrix (list[list[float]]) as input.

In [2]:
matrix = [
    [0, 5, 6, 4, 7, 4],
    [5, 0, 5, 4, 6, 5],
    [6, 5, 0, 4, 5, 5],
    [4, 4, 4, 0, 5, 5],
    [7, 6, 5, 5, 0, 4],
    [4, 5, 5, 5, 4, 0],
]

names = ["Action", "Adventure", "Comedy", "Drama", "Fantasy", "Thriller"]

It may look more clear if we present this as a table with the columns and indices labelled. This is entirely optional.

In [3]:
import pandas as pd
pd.DataFrame(matrix, columns=names, index=names)
Out[3]:
Action Adventure Comedy Drama Fantasy Thriller
Action 0 5 6 4 7 4
Adventure 5 0 5 4 6 5
Comedy 6 5 0 4 5 5
Drama 4 4 4 0 5 5
Fantasy 7 6 5 5 0 4
Thriller 4 5 5 5 4 0

Visualisation

Animations can be controlled with the following parameters:

  • animated_loop will result in an animation that loops forever, this is primarily useful in combination with the to_mp4() end-point to create a looping video.
  • animated_intro will result in an animation that loops once, and serves as a nice introduction to your visualisation as it loads.
  • animated_duration determines how long a single loop will take.

Let's try both approaches.

Here we're using .show() which outputs to a Jupyter Notebook cell, however, we may want to output to a 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!

You may miss the first - you can refresh the page and scroll back down here to catch it!

In [6]:
Chord(matrix, names, wrap_labels=True,
     animated_intro=True, animated_duration=3000).show()
Plotapi - Chord Diagram

This next one will loop forever.

In [8]:
Chord(matrix, names, wrap_labels=True,
     animated_loop=True, animated_duration=3000).show()
Plotapi - Chord 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. and the Plotapi Gallery.

Animations: Intros and Loops

Preamble

In [1]:
from plotapi import Chord

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

Introduction

Let's take a look at some parameters that control the layout of our diagram.

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

Dataset

Chord expects a list of names (list[str]) and a co-occurence matrix (list[list[float]]) as input.

In [2]:
matrix = [
    [0, 5, 6, 4, 7, 4],
    [5, 0, 5, 4, 6, 5],
    [6, 5, 0, 4, 5, 5],
    [4, 4, 4, 0, 5, 5],
    [7, 6, 5, 5, 0, 4],
    [4, 5, 5, 5, 4, 0],
]

names = ["Action", "Adventure", "Comedy", "Drama", "Fantasy", "Thriller"]

It may look more clear if we present this as a table with the columns and indices labelled. This is entirely optional.

In [3]:
import pandas as pd
pd.DataFrame(matrix, columns=names, index=names)
Out[3]:
Action Adventure Comedy Drama Fantasy Thriller
Action 0 5 6 4 7 4
Adventure 5 0 5 4 6 5
Comedy 6 5 0 4 5 5
Drama 4 4 4 0 5 5
Fantasy 7 6 5 5 0 4
Thriller 4 5 5 5 4 0

Visualisation

We can modify the width, margin, and rotation of our diagram.

The width parameter will also set the height, as the Plotapi Chord maintains a $1:1$ ratio.

The margin parameter expects a single float to imply an equal margin of that size.

The rotate paramater will rotate the Chord diagram around its center.

By default a Chord diagram is centered, but this can be disabled with center=False.

Here we're using .show() which outputs to a Jupyter Notebook cell, however, we may want to output to a 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!

In [5]:
Chord(matrix, names, width=500, margin=100, rotate=-30).show()
Plotapi - Chord 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. and the Plotapi Gallery.

Linked Data Table

Preamble

In [1]:
from plotapi import Chord

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

Introduction

Plotapi Chord supports a linked data table. This means as you hover over segments and ribbons in the Chord diagram, a data table will be filtering in real-time to show more information.

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

Dataset

Chord expects a list of names (list[str]) and a co-occurence matrix (list[list[float]]) as input.

In [2]:
matrix = [
    [0, 5, 6, 4, 7, 4],
    [5, 0, 5, 4, 6, 5],
    [6, 5, 0, 4, 5, 5],
    [4, 4, 4, 0, 5, 5],
    [7, 6, 5, 5, 0, 4],
    [4, 5, 5, 5, 4, 0],
]

names = ["Action", "Adventure", "Comedy", "Drama", "Fantasy", "Thriller"]

It may look more clear if we present this as a table with the columns and indices labelled. This is entirely optional.

In [3]:
import pandas as pd
pd.DataFrame(matrix, columns=names, index=names)
Out[3]:
Action Adventure Comedy Drama Fantasy Thriller
Action 0 5 6 4 7 4
Adventure 5 0 5 4 6 5
Comedy 6 5 0 4 5 5
Drama 4 4 4 0 5 5
Fantasy 7 6 5 5 0 4
Thriller 4 5 5 5 4 0

To make use of the linked data table, we need to provide some data in CSV format. This could be loaded through a file, directly in a strong, or using a pandas DataFrame (.to_csv(index=False)).

In [4]:
data_table='''Genre 1,Genre 2,Awesome
Thriller,Action,The Plotapi
Thriller,Action,Action Plotapi
Thriller,Drama,Feeling Plotapi
Thriller,Comedy,Plotapi Force'''

Visualisation

We'll enable the linked data table by passing data into the data_table parameter, and we'll modify the data_table_column_width by setting it to a smaller value of $80$.

Here we're using .show() which outputs to a Jupyter Notebook cell, however, we may want to output to a 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!

In [5]:
Chord(matrix, names, width=400, margin=20, curved_labels=True,
      data_table=data_table, data_table_column_width=80).show()
Plotapi - Chord Diagram

Data table mode includes the "locking" feature. Try clicking on the Thriller segment and you will see a padlock icon appear in the top right, and until you click somewhere again the current selection will persist.

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. and the Plotapi Gallery.

Directed Chord Diagrams

Preamble

In [ ]:
from plotapi import Chord

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

Introduction

The asymmetric (symmetric=False) Plotapi Chord diagram allows use to represent the value at each end of the relationship with a single ribbon. However, it may be more suitable to use two different ribbons with arrows to indicate the direction of the relationship. This is possible with Plotapi Chord.

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

Dataset

Chord expects a list of names (list[str]) and a co-occurence matrix (list[list[float]]) as input.

In [ ]:
matrix = [
    [0, 5, 6, 4],
    [2, 0, 5, 4],
    [6, 5, 0, 4],
    [2, 4, 3, 0],
]

names = ["Action", "Adventure", "Comedy", "Drama"]

It may look more clear if we present this as a table with the columns and indices labelled. This is entirely optional.

In [3]:
import pandas as pd
pd.DataFrame(matrix, columns=names, index=names)
Out[3]:
Action Adventure Comedy Drama
Action 0 5 6 4
Adventure 2 0 5 4
Comedy 6 5 0 4
Drama 2 4 3 0

Visualisation

To enable directed mode, we only need to set the directed parameter to true.

Here we're using .show() which outputs to a Jupyter Notebook cell, however, we may want to output to a 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!

In [7]:
Chord(matrix, names, directed=True, colors="yellow_blue").show()
Plotapi - Chord Diagram

The directed Chord diagram can be paired with reverse_gradients=True to make it easier to see where inbound/outbound relationships are coming from or going to.

In [6]:
Chord(matrix, names, directed=True, colors="yellow_blue", reverse_gradients=True).show()
Plotapi - Chord 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. and the Plotapi Gallery.

Mix and Match

Preamble

In [1]:
from plotapi import Chord

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

Introduction

We can mix and match some of our parameters to create a visualisation that looks significantly different, although it uses the same data.

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

Dataset

Chord expects a list of names (list[str]) and a co-occurence matrix (list[list[float]]) as input.

In [2]:
matrix = [
    [0, 5, 6, 4, 7, 4],
    [5, 0, 5, 4, 6, 5],
    [6, 5, 0, 4, 5, 5],
    [4, 4, 4, 0, 5, 5],
    [7, 6, 5, 5, 0, 4],
    [4, 5, 5, 5, 4, 0],
]

names = ["Action", "Adventure", "Comedy", "Drama", "Fantasy", "Thriller"]

It may look more clear if we present this as a table with the columns and indices labelled. This is entirely optional.

In [3]:
import pandas as pd
pd.DataFrame(matrix, columns=names, index=names)
Out[3]:
Action Adventure Comedy Drama Fantasy Thriller
Action 0 5 6 4 7 4
Adventure 5 0 5 4 6 5
Comedy 6 5 0 4 5 5
Drama 4 4 4 0 5 5
Fantasy 7 6 5 5 0 4
Thriller 4 5 5 5 4 0

Visualisation

Let's mix and match some parameters that we've covered in this gallery.

Here we're using .show() which outputs to a Jupyter Notebook cell, however, we may want to output to a 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!

In [4]:
Chord(matrix, names, padding=0.5, reverse_gradients=True,
      colors="accent", curved_labels=True, label_color="#777777",
      font_size_large="30px", arc_numbers=True,
      inner_radius_scale=0.45, outer_radius_scale=1.4).show()
Plotapi - Chord 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. and the Plotapi Gallery.

Equal-sized Segments

Preamble

In [2]:
from plotapi import Chord

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

Introduction

It may be desirable to have equal-sized segments in our Chord Diagram. This is possible with Plotapi, and makes use of the colored_diagonals feature. The idea is to determine the max frequency in our plot, and set all the diagonals to that value. Let's assume that is $20$ for this example.

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

Dataset

Chord expects a list of names (list[str]) and a co-occurence matrix (list[list[float]]) as input.

In [3]:
matrix = [
    [20, 0, 0, 0, 0, 0],
    [0, 20, 0, 0, 0, 0],
    [0, 0, 20, 0, 0, 0],
    [0, 0, 0, 20, 0, 0],
    [0, 0, 0, 0, 20, 0],
    [0, 0, 0, 0, 0, 20],
]

names=['one','two','three','four','five','six']

Chord(matrix, names, title="Diagonals not coloured", 
      colored_diagonals=False).show()
Plotapi - Chord Diagram

We can see above that this creates a chord diagram with equal-sized segments. All we need to do now is introduce our relationships, and subtract them from the corresponding diagonal value.

In [5]:
matrix = [
    [11, 4, 5, 0, 0, 0],
    [4, 14, 2, 0, 0, 0],
    [5, 2, 13, 0, 0, 0],
    [0, 0, 0, 20, 0, 0],
    [0, 0, 0, 0, 20, 0],
    [0, 0, 0, 0, 0, 20],
]

It may look more clear if we present this as a table with the columns and indices labelled. This is entirely optional.

In [6]:
import pandas as pd
pd.DataFrame(matrix, columns=names, index=names)
Out[6]:
one two three four five six
one 11 4 5 0 0 0
two 4 14 2 0 0 0
three 5 2 13 0 0 0
four 0 0 0 20 0 0
five 0 0 0 0 20 0
six 0 0 0 0 0 20

Visualisation

With our matrix prepared, we can use Plotapi with colored_diagonals=False to create a diagram with equal-sized segments and some ribbons between categories. We could add some padding to separate them too.

Here we're using .show() which outputs to a Jupyter Notebook cell, however, we may want to output to a 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!

In [20]:
Chord(matrix, names, colors="set2",
      title="Diagonals not coloured", 
      colored_diagonals=False, padding=0.2).show()
Plotapi - Chord 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. and the Plotapi Gallery.

Coloured and Invisible Diagonals

Preamble

In [1]:
from plotapi import Chord

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

Introduction

By default, Plotapi Chord displays occurrences where a category is not related to another category. These values appear in the diagonal of the matrix. It may be desirable to hide (but not remove) these values, this is possible with Plotapi.

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

Dataset

Chord expects a list of names (list[str]) and a co-occurence matrix (list[list[float]]) as input.

In [2]:
names=['one','two','three','four','five','six']

matrix = [
    [19, 5, 6, 4, 7, 4],
    [5, 4, 5, 4, 6, 5],
    [6, 5, 0, 4, 5, 5],
    [4, 4, 4, 0, 5, 5],
    [7, 6, 5, 5, 0, 4],
    [4, 5, 5, 5, 4, 0],
]

It may look more clear if we present this as a table with the columns and indices labelled. This is entirely optional.

In [3]:
import pandas as pd
pd.DataFrame(matrix, columns=names, index=names)
Out[3]:
one two three four five six
one 19 5 6 4 7 4
two 5 4 5 4 6 5
three 6 5 0 4 5 5
four 4 4 4 0 5 5
five 7 6 5 5 0 4
six 4 5 5 5 4 0

Visualisation

The representation of the diagonal values can be hidden by setting colored_diagonals=False.

Let's first see what they look like when they're visible.

Here we're using .show() which outputs to a Jupyter Notebook cell, however, we may want to output to a 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!

In [4]:
Chord(matrix, names, title="Diagonals coloured").show()
Plotapi - Chord Diagram

Now let's see what it looks like when they're not visible.

In [5]:
Chord(matrix, names, title="Diagonals not coloured",
      colored_diagonals=False).show()
Plotapi - Chord 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. and the Plotapi Gallery.