Skip to main content

Easy Plotly

Build Status codecov.io Language grade: Python Pypi pyversions Jupyter Notebook GitHub.io Star

This is on-going research on how ploting with Plotly.py, especially ploting of hierarchical data, could be made easier.

See the outputs of the commands below - tables and plots - in the HTML export of this notebook. Or even, open this README.md as a notebook and run it interactively on Binder!

Installation

Install the easyplotly python package with

pip install easyplotly

Sample data

Our sample data is the population and life expectancy, per country and region:

import world_bank_data as wb
import itables.interactive

# Collect countries
countries = wb.get_countries()
region_country = countries[['region', 'name']].rename(columns={'name': 'country'})

# Population & life expectancy
region_country['population'] = wb.get_series('SP.POP.TOTL', mrv=1, id_or_value='id', simplify_index=True)
region_country['life_expectancy'] = wb.get_series('SP.DYN.LE00.IN', mrv=1, id_or_value='id', simplify_index=True)

# Observations restricted to the countries
pop_and_exp = region_country.loc[countries.region != 'Aggregates'].set_index(['region', 'country']).sort_index()
pop_and_exp

Sunburst Charts

import plotly.graph_objects as go
import plotly.io as pio
import easyplotly as ep

pio.renderers.default = 'notebook_connected'
layout = go.Layout(title='World Population and Life Expectancy<br>Data from the World Bank', height=800)

Our Sunburst function accepts inputs of many types: pandas Series, dictionaries, and list of such objects. If you want, you can redefine labels, or add other arguments like text - use either a Series with an index identical to that of values, or a function that to any tuple (level0, level1, ... leveln) associates the corresponding label or value.

sunburst = ep.Sunburst(pop_and_exp.population, text=pop_and_exp.life_expectancy)
go.Figure(sunburst, layout)

Treemaps

The Treemap function works like the Sunburst one:

treemap = ep.Treemap(pop_and_exp.population, text=pop_and_exp.life_expectancy)
go.Figure(treemap, layout)

Just like the Sunburst function, it also accepts all the arguments supported by the original go.Sunburst object. You're even welcome to use the magic underscore notation, as we do below when we set marker.colors with marker_colors:

import numpy as np


def average(values, weights):
    """Same as np.average, but remove nans"""
    total_obs = 0.
    total_weight = 0.
    if isinstance(values, np.float):
        values = [values]
        weights = [weights]
    for x, w in zip(values, weights):
        xw = x * w
        if np.isnan(xw):
            continue
        total_obs += xw
        total_weight += w
    return total_obs / total_weight if total_weight != 0 else np.NaN


def life_expectancy(item):
    """Life expectancy associated to a tuple like (), ('Europe & Central Asia') or ('East Asia & Pacific', 'China')"""
    sub = pop_and_exp.loc[item] if item else pop_and_exp
    return average(sub.life_expectancy, weights=sub.population)


def text(item):
    """Return the text associated to a tuple like (), ('Europe & Central Asia') or ('East Asia & Pacific', 'China')"""
    life_exp = life_expectancy(item)
    if life_exp > 0:
        pop = pop_and_exp.population.loc[item].sum() if item else pop_and_exp.population.sum()  
        return 'Population: {:,}<br>Life expectancy: {:.2f}'.format(int(pop), life_exp)


treemap = ep.Treemap(pop_and_exp.population,
                     hoverinfo='label+text',
                     text=text,
                     root_label='World',
                     # magic underscore notation
                     marker_colors=life_expectancy,
                     marker_colorscale='RdBu')

go.Figure(treemap, layout)

Sankey Plot

Plot links from a dict, or a series with a source/target multiindex:

links = {('A', 'B'): 3, ('B', 'C'): 1, ('B', 'D'): 2, ('C', 'A'): 1, ('D', 'A'): 1, ('A', 'D'): 1}
go.Figure(ep.Sankey(links))

Plot links from a DataFrame (sources as the index, targets as the columns):

import pandas as pd
links = pd.DataFrame(1, index=['Source A', 'Source B'], columns=['Target'])
go.Figure(ep.Sankey(links))

We conclude the examples with a plot in which the links are a list of pandas Series:

region_income = wb.get_countries().query("region != 'Aggregates'").copy()
region_income['population'] = wb.get_series('SP.POP.TOTL', mrv=1, id_or_value='id', simplify_index=True)
income_lending = region_income.copy()
region_income.set_index(['region', 'incomeLevel'], inplace=True)
income_lending.set_index(['incomeLevel', 'lendingType'], inplace=True)

layout = go.Layout(title='Regions income and lending type<br>Data from the World Bank')

sankey = ep.Sankey(
    link_value=[region_income['population'], income_lending['population']],
    link_label=[region_income['name'], income_lending['name']])

go.Figure(sankey, layout)

Metadata

Release files for easyplotly 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for easyplotly 0.1.3
File Size Uploaded
easyplotly-0.1.3.tar.gz 8.0 kB Details

Release files / easyplotly-0.1.3.tar.gz

Download URL easyplotly-0.1.3.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0a8f29c297f2be9eab299d9f769f6d0715bfe870352fc4394ba8a5d16f249cba
BLAKE2b-256 checksum
How to use checksums
06e22886d53133b463a4d78bafc6920911481f4cba96f299cedbf853f7aa31a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0.post20191030 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5

Release history Release notifications | RSS feed

This release

0.1.3 This release

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page