I'm excited to announce the official release of bar_chart_race
, a python package for creating bar chart races. In this post, I'll cover many of the major available options. Navigate to the official documentation for a full breakdown of all of the options.
Bar chart races have become very popular over the last year and no python package existed to create them. I also built some for my coronavirus dashboard.
Install with:
pip install bar_chart_race
import bar_chart_race as bcr
bcr.__version__
In order to use bar_chart_race, your data must be in 'wide' pandas DataFrame where:
A few sample datasets are available to download via the load_dataset
function. The covid19_tutorial dataset contains the total deaths due to COVID-19 of selected countries over a period of 10 days.
df = bcr.load_dataset('covid19_tutorial')
df
Once you have data in the correct format, you can pass it directly to bar_chart_race
.
bcr.bar_chart_race(df)
By default, bars are horizontal, but can be made vertical with the orientation
parameter.
bcr.bar_chart_race(df, orientation='v')
Set sort
to 'asc'
to change the order of the bars.
bcr.bar_chart_race(df, sort='asc')
Limit the number of bars plotted with n_bars
.
bcr.bar_chart_race(df, n_bars=6)
Fix the order of the bars for the duration of the animation by setting fixed_order
to a list.
bcr.bar_chart_race(df, fixed_order=['Iran', 'USA', 'Italy', 'Spain', 'Belgium'])
Fix the maximum value for the entire duration of the animation.
bcr.bar_chart_race(df, fixed_max=True)
By default, 10 frames are used per time period with the entire period lasting 500 milliseconds (half of a second). Both of these are changed below.
bcr.bar_chart_race(df, steps_per_period=20, period_length=200)
Linearly interpolate the period label.
bcr.bar_chart_race(df, interpolate_period=True)
bar_chart_race uses matplotlib for all of the underlying plotting. Many properties can be set by using parameters common to matplotlib.
bcr.bar_chart_race(df,
figsize=(5, 3),
dpi=100,
label_bars=False,
period_label={'x': .99, 'y': .1, 'ha': 'right', 'color': 'red'},
title='COVID-19 Deaths by Country')
Bar properties can also be set.
bcr.bar_chart_race(df, bar_kwargs={'alpha': .2, 'ec': 'black', 'lw': 3})
The period label can be formatted with date directives or new-style string formatting.
bcr.bar_chart_race(df, period_fmt='%b %-d, %Y')
Add a custom label to the plot that summarizes the current time period.
def summary(values, ranks):
total_deaths = int(round(values.sum(), -2))
s = f'Total Deaths - {total_deaths:,.0f}'
return {'x': .99, 'y': .05, 's': s, 'ha': 'right', 'size': 8}
bcr.bar_chart_race(df, period_summary_func=summary)
A single perpendicular bar can be added to summarize each period as well.
def func(values, ranks):
return values.quantile(.9)
bcr.bar_chart_race(df, perpendicular_bar_func=func)
Upon registration, you'll get access to the following free courses: