pandas_highcharts is a Python library to turn your pandas DataFrame into a suited JSON for Highcharts, a Javascript library for interactive charts.
Before introducing Highcharts, I build an arbitrary DataFrame
with a timeseries (a Brownian motion), plot it with matplotlib, then seaborn and Highcharts.
In [1]:
%matplotlib inline
import string
In [2]:
import numpy as np
import pandas as pd
In [3]:
print pd.__version__
In [4]:
# Dimensions
nb_rand_var = 8
nb_dates = 220
np.random.seed(4321)
# Random choice letters
pickme = lambda x: np.random.choice(26, x, replace=False)
labels = np.array(list(string.ascii_uppercase))[pickme(nb_rand_var)]
In [5]:
labels
Out[5]:
In [6]:
# Timeseries
ts = pd.date_range("2015-03-23", periods=nb_dates, freq="B")
ts
Out[6]:
In [7]:
# A Brownian Motion
noise = np.random.randn(nb_dates, nb_rand_var)
df = pd.DataFrame(noise.cumsum(axis=0),
index=ts,
columns=labels)
In [8]:
df.head()
Out[8]:
In [9]:
import matplotlib.pyplot as plt
plt.style.use("ggplot") # only for matplotlib >= 1.4
In [10]:
df.plot();
Even if the figure is nicer than the matplotlib default style, I think it's to small, and legends don't fit when the number of variables is higher than 5 or 6.
Just a try with seaborn.
In [11]:
import seaborn as sns
In [12]:
with sns.axes_style("darkgrid"):
df.plot()
The colors palette, the figure size and the legends position are OK for me.
In [13]:
with sns.axes_style("ticks"):
df.plot()
In [14]:
from pandas_highcharts.display import display_charts
In [15]:
display_charts(df, title="Brownian Motion")
You can also retrieve the JSON data generated by pandas_highcharts thanks to the function serialize
.
In [16]:
from pandas_highcharts.core import serialize
Serialize the previous DataFrame
with the pandas_highcharts function serialize
.
In [17]:
json_data = serialize(df, render_to="brownian", title="Brownian Motion")
In [18]:
type(json_data)
Out[18]:
And display the charts thanks to the HTML renderer and the function display
of IPython
In [19]:
from IPython.core.display import display, HTML
In [20]:
display(HTML("""<div id="{chart_id}"</div>
<script type="text/javascript">{data}</script>""".format(chart_id="brownian", data=json_data)))
You can also retrive the related Python dict before getting the JSON version. You'll have the same structure as Highcharts options and can change some parameters.
In [21]:
data = serialize(df, render_to="brownian", output_type="dict")
In [22]:
data.keys()
Out[22]:
In [23]:
data["chart"]
Out[23]:
In [24]:
data["legend"]
Out[24]:
Add a subtitle
In [25]:
data["subtitle"] = {"text": "a subtitle here..."}
Change the chart type
In [26]:
data["chart"]["type"] = "spline"
In [27]:
data["plotOptions"] = {"spline": {
"lineWidth": 2,
"states": {
"hover": {
"lineWidth": 3}
}}}
In [28]:
data['chart']['type'] = 'line'
data['chart']['zoomType'] = 'x'
data['chart']['panning'] = True
data['chart']['panKey'] = 'shift'
In [29]:
data["chart"]["renderTo"] = "new_brownian"
In [30]:
print data["chart"]
print data["subtitle"]
print data["yAxis"]
Serialize this new dict.
In [31]:
from pandas_highcharts.core import json_encode
In [32]:
json_data_2 = "new Highcharts.StockChart(%s);" % json_encode(data)
In [33]:
display(HTML("""<div id="{chart_id}"</div>
<script type="text/javascript">{data}</script>""".format(chart_id="new_brownian", data=json_data_2)))