ubergarm

sui generis, linux, python, devops, robotics, polyamory, data custodian

Visualizing python dataframes in jupyter notebook with Altair

2015-12-12 data science ubergarm

Finally, an easy way to turn Pandas DataFrames into sorted bar charts using brewer color schemes!

It took a little playing around and it isn’t quite perfect yet, but here is a quick example of how to turn a Series or DataFrame into a lovely sorted stacked bar chart.

References

Run Jupyter Container

Running as root allows you to !pip install or !apt-get install dependencies.

#!/bin/bash
docker run --name jupyter -d -p 8888:8888 -v `pwd`/notebooks:/home/jovyan/work --user=root -e GRANT_SUDO=yes -e NB_UID=$UID jupyter/datascience-notebook

Example Notebook

!pip install --upgrade pip
!pip install git+https://github.com/ellisonbg/altair
!pip install --upgrade notebook  # need jupyter_client >= 4.2 for sys-prefix below
!jupyter nbextension install --sys-prefix --py vega
!pip install palettable # for brewer color maps
import pandas as pd
import numpy as np
# pretty graphs https://github.com/ellisonbg/altair
from altair import *
# excellent brewer color palettes https://jiffyclub.github.io/palettable/
from palettable import colorbrewer as cb
data = pd.DataFrame()
data['counts'] = pd.Series(np.round(100 * np.abs(np.random.randn(8))))
data.index = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'i']
data['title'] = data.index
data
counts title
a 34 a
b 49 b
c 77 c
d 111 d
e 3 e
f 36 f
g 156 g
i 15 i
Chart(data).mark_bar().encode(
    x='counts',
    y='title'
)

png

c = Chart(data).mark_bar().encode(
    x=X('counts', axis=Axis(title='Counts')),
    y=Y('title', sort=SortField(field='counts', order='descending', op='sum'), axis=Axis(title='Title')),
    color=Color('title:N', scale=Scale(range=cb.qualitative.Dark2_8.hex_colors))
)
#c.to_dict()
c

png

comments powered by Disqus