Skip to main content

Tiny package to make 'race' barplots using plotly

Project description

Test Python package PyPI version

Making race plots with Plotly!

Motivation

Bar race plots, barchart race plots or simply race plots are very common when evaluating rankings over time. Python plotting is not the most user friendly and whenever I've wanted to make race plots I have ended up with tonnes of code for what is a simple plot in the end. I wish to remove that headache for many users that simply want to make quick plot and then move on.

Usage

Installation

raceplotly can be installed from pip.The only dependencies are pandas and plotly.

pip install raceplotly

Basic documentation

The package only contains one module called barplot. This module takes the following arguments at initialisation:

  • df: (type: pandas.DataFrame) dataframe from which to query data
  • item_column: (type: string) Name of column describing the items to be ranked (e.g. countries, corporations, names of people...)
  • value_column: (type: string) Name of column describing the value to be used for ranking (e.g. GDP, population, volume of sales...)
  • time_column: (type: string) Name of column describing the time variable. This must be a sequence (e.g. years, days). Support of Date format has not been tested yet.
  • item_color: (type: string) [OPTIONAL ATTRIBUTE] Name of column describing the color for different categories (e.g. colors = {'Category 1': 'rgba(0, 76, 109, 1)', 'Category 2': 'rgb(208, 210, 211)'}...) [DEFAULT = Random Color]
  • top_entries: (type: numeric) [OPTIONAL ATTRIBUTE] Number of top entries to display (e.g. 5 for top 5 for any given time period...) [DEFAULT = 10]

The barplot object contains one main method:

  • plot(title, orientation, item_label, value_label, time_label, frame_duration, date_format):
    • title: (type: string) Main title of the plot (static by default)
    • orientation: (type: string -> 'horizontal' or 'vertical') whether bars grow upwards ('vertical') or rightwards ('horizontal')
    • initial_frame: (type: numeric or string) Should either match one of the values from the time_column or be provided as min or max, in which case the initial frame would correspond to the minimum or maximum value of the time_column.
    • item_label: (type: string) Title of the axis corresponding to the item values
    • value_label: (type: string) Title of the axis corresponding to the value
    • time_label: (type: string) Title for the time axis which appears in each frame next to the formmated date/time variable
    • frame_duration: (type: int -> default 500) Frame and transition duration time in milliseconds
    • date_format: (type: str) Format for the displayed date/time, should be compatible with strftime format, see strftime reference.

Example plot: Top 10 crops from 1961 to 2018

See example notebooks under example/.

import pandas as pd
from raceplotly.plots import barplot

data = pd.read_csv('https://raw.githubusercontent.com/lc5415/raceplotly/main/example/FAOSTAT_data.csv')

my_raceplot = barplot(data,  item_column='Item', value_column='Value', time_column='Year')

my_raceplot.plot(item_label = 'Top 10 crops', value_label = 'Production quantity (tonnes)', frame_duration = 800)

Example with specified colors for different category.

See example notebooks under example/color.

import pandas as pd
from raceplotly.plots import barplot

data = pd.read_csv('https://raw.githubusercontent.com/lc5415/raceplotly/main/example/FAOSTAT_data.csv')

# To add specific color to the categories, a new coloumn with rgb values for each category has to be created.
# Assigning colors to the categories.
colors = {'Sugar cane': 'rgba(0, 76, 109, 1)',
          'Potatoes': 'rgb(208, 210, 211)',
          'Wheat': 'rgb(208, 210, 211)',
          'Rice, paddy':'rgba(66, 114, 146, 1)',
          'Maize':'rgba(40, 95, 127, 1)',
          'Sugar beet':'rgb(208, 210, 211)',
          'Rice, paddy (rice milled equivalent)':'rgb(208, 210, 211)',
          'Sweet potatoes':'rgb(208, 210, 211)',
          'Barley':'rgb(208, 210, 211)',
          'Cassava':'rgb(208, 210, 211)',
          'Soybeans':'rgb(208, 210, 211)',
          'Oil palm fruit':'rgb(208, 210, 211)',
          'Vegetables, fresh nes':'rgb(208, 210, 211)'}

# Default color for cateogry is black when color is not specifed explicitly

# Mapping the items with the color for the whole dataset.
data['color'] = data['Item'].map(colors)

my_raceplot = barplot(data,  item_column='Item', value_column='Value', time_column='Year', item_color='color')

my_raceplot.plot(item_label = 'Top 10 crops', value_label = 'Production quantity (tonnes)', time_label = 'Year: ', ## overwrites default `Date: `
                 frame_duration = 800)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

raceplotly-0.1.5.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

raceplotly-0.1.5-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file raceplotly-0.1.5.tar.gz.

File metadata

  • Download URL: raceplotly-0.1.5.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for raceplotly-0.1.5.tar.gz
Algorithm Hash digest
SHA256 0af7f71cad27776f0eb6384140029619b411c12f8761b7f8bf8b09b59d56816c
MD5 4ae5f18d878f79741cf84eaa609c0da7
BLAKE2b-256 cdb0f06d50ecba6466d8519f717c95702ed4153794a3d5459fb1882e7871c2a4

See more details on using hashes here.

File details

Details for the file raceplotly-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: raceplotly-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for raceplotly-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 59b1e2674475cbd7cf029932a9e97b103311a0ea27de5852dd88c905255a6b6d
MD5 22ba63fd1a8810468345676a9ffb43af
BLAKE2b-256 f1e77daf4401b82d9304a38ad7126aff9cdc063a8727c59f2402459d082d7ef3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page