Sparklines in Pandas

Sparklines are small unlabeled plots, used to visually convey an idea in a small space. This script creates sparklines in a Pandas DataFrame which can then be displayed inline in a Jupyter Notebook or output to an HTML file. It does not annotate the figure, other columns of the DataFrame can be used to convey details about the sparklines.

Background: https://en.wikipedia.org/wiki/Sparkline

Forked and extended from: https://github.com/iiSeymour/sparkline-nb


In [1]:
import numpy as np
import pandas as pd
from scipy import stats
import matplotlib.pyplot as plt
%matplotlib inline

import sparklines

Create some plot data

Function assumes data to plot is an array-like object in a single cell per row.


In [2]:
density_func = 78
mean, var, skew, kurt = stats.chi.stats(density_func, moments='mvsk')
x_chi = np.linspace(stats.chi.ppf(0.01, density_func),
                    stats.chi.ppf(0.99, density_func), 100)
y_chi = stats.chi.pdf(x_chi, density_func)

x_expon = np.linspace(stats.expon.ppf(0.01), stats.expon.ppf(0.99), 100)
y_expon = stats.expon.pdf(x_expon)

a_gamma = 1.99
x_gamma = np.linspace(stats.gamma.ppf(0.01, a_gamma),
                      stats.gamma.ppf(0.99, a_gamma), 100)
y_gamma = stats.gamma.pdf(x_gamma, a_gamma)

In [3]:
n = 100

In [4]:
np.random.seed(0)  # keep generated data the same for git commit

In [5]:
data = [np.random.rand(n), 
        np.random.randn(n), 
        np.random.beta(2, 1, size=n), 
        np.random.binomial(3.4, 0.22, size=n), 
        np.random.exponential(size=n),
        np.random.geometric(0.5, size=n), 
        np.random.laplace(size=n), 
        y_chi, 
        y_expon, 
        y_gamma]

In [6]:
function = ['rand',
            'randn',
            'beta',
            'binomial',
            'exponential',
            'geometric',
            'laplace',
            'chi',
            'expon',
            'gamma']

In [7]:
df = pd.DataFrame(data)
df['function'] = function

In [8]:
df


Out[8]:
0 1 2 3 4 5 6 7 8 9 ... 91 92 93 94 95 96 97 98 99 function
0 0.548814 0.715189 0.602763 0.544883 0.423655 0.645894 0.437587 0.891773 0.963663 0.383442 ... 0.667410 0.131798 0.716327 0.289406 0.183191 0.586513 0.020108 0.828940 0.004695 rand
1 -1.165150 0.900826 0.465662 -1.536244 1.488252 1.895889 1.178780 -0.179925 -1.070753 1.054452 ... 0.318728 0.856831 -0.651026 -1.034243 0.681595 -0.803410 -0.689550 -0.455533 0.017479 randn
2 0.667468 0.335994 0.546961 0.895499 0.364307 0.291348 0.936391 0.854830 0.973605 0.846768 ... 0.676358 0.886607 0.961239 0.971125 0.073274 0.955266 0.499539 0.536154 0.326569 beta
3 2.000000 0.000000 0.000000 0.000000 2.000000 0.000000 0.000000 2.000000 0.000000 2.000000 ... 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 1.000000 1.000000 binomial
4 0.509429 0.565212 0.953286 0.072616 1.728259 1.059645 1.295878 0.769862 0.117070 0.519254 ... 0.613418 0.588044 0.409627 2.125931 2.900909 4.814698 0.472794 3.385741 1.569638 exponential
5 2.000000 1.000000 1.000000 1.000000 4.000000 1.000000 1.000000 1.000000 3.000000 3.000000 ... 4.000000 1.000000 1.000000 2.000000 5.000000 1.000000 1.000000 3.000000 1.000000 geometric
6 1.599102 -0.991457 0.067569 -0.426884 -0.457150 -0.112325 -0.143692 -0.335901 1.771613 0.622667 ... 0.282082 -1.500619 -0.085624 -0.439021 -0.457283 -0.035453 0.615548 -1.977858 1.420251 laplace
7 0.040498 0.045391 0.050737 0.056560 0.062882 0.069724 0.077106 0.085044 0.093554 0.102646 ... 0.077317 0.070661 0.064455 0.058683 0.053326 0.048368 0.043787 0.039566 0.035686 chi
8 0.990000 0.945099 0.902234 0.861314 0.822249 0.784956 0.749355 0.715368 0.682923 0.651949 ... 0.014497 0.013839 0.013211 0.012612 0.012040 0.011494 0.010973 0.010475 0.010000 expon
9 0.129412 0.174739 0.213651 0.246827 0.274862 0.298294 0.317606 0.333234 0.345575 0.354988 ... 0.013531 0.012809 0.012124 0.011475 0.010859 0.010276 0.009722 0.009198 0.008701 gamma

10 rows × 101 columns

Define range of data to make sparklines

Note: data must be row wise


In [9]:
a = df.ix[:, 0:100]

Output to new DataFrame of Sparklines


In [10]:
df_out = pd.DataFrame()
df_out['sparkline'] = sparklines.create(data=a)
sparklines.show(df_out[['sparkline']])


sparkline

Insert Sparklines into source DataFrame


In [11]:
df['sparkline'] = sparklines.create(data=a)
sparklines.show(df[['function', 'sparkline']])


function sparkline
rand
randn
beta
binomial
exponential
geometric
laplace
chi
expon
gamma

Detailed Formatting

Return only sparklines, format the line, fill and marker.


In [12]:
df_out = pd.DataFrame()
df_out['sparkline'] = sparklines.create(data=a,
                                        color='#1b470a',
                                        fill_color='#99a894',
                                        fill_alpha=0.2,
                                        point_color='blue',
                                        point_fill='none',
                                        point_marker='*',
                                        point_size=3,
                                        figsize=(6, 0.25))
sparklines.show(df_out[['sparkline']])


sparkline

Example Data and Sparklines Layout


In [13]:
df_copy = df[['function', 'sparkline']].copy()

In [14]:
df_copy['value'] = df.ix[:, 100]

In [15]:
df_copy['change'] = df.ix[:,98] - df.ix[:,99]

In [16]:
df_copy['change_%'] = df_copy.change / df.ix[:,99]

In [17]:
sparklines.show(df_copy)


function sparkline value change change_%
rand rand 0.824245 175.540141
randn randn -0.473012 -27.061466
beta beta 0.209585 0.641778
binomial binomial 0.000000 0.000000
exponential exponential 1.816102 1.157020
geometric geometric 2.000000 2.000000
laplace laplace -3.398109 -2.392611
chi chi 0.003881 0.108753
expon expon 0.000475 0.047509
gamma gamma 0.000497 0.057133

Export to HTML

Inline Jupyter Notebook


In [18]:
sparklines.to_html(df_copy, 'pandas_sparklines_demo')

HTML text for rendering elsewhere


In [19]:
html = sparklines.to_html(df_copy)