Saturday, January 2, 2016

Make great-looking d3.js charts in Python without coding a line of JavaScript

d3.js is an excellent visualization library due to the ability it gives a developer to gain control over all aspects of visualization. However, its extreme power can often also be a problem -- sometimes, we just want to create a set of standard bar and line charts that look good, without needing to spend hours fine-tuning aspects of charts. This is where nvd3 can come in handy: it is a handy library that offers a set of reusable presets based on d3.js. Though you still have some level of control over the output, you can move more quickly and reuse the visualization types that the library supports.

As a Pythonista, I've particularly enjoyed using Python-nvd3 -- a great library that offers a Python wrapper for the nvd3.js interface. You can create great-looking d3.js charts without ever touching a line of JavaScript. I'll go through a quick bar chart example.


We use initialize_javascript so that we are able to render our plots within the iPython notebook, and np.random.seed so we are able to reproduce our results.

In [2]:
import numpy as np
import nvd3


nvd3.ipynb.initialize_javascript(use_remote=True)
np.random.seed(100)

Next, we initialize our discreteBarChart object with our desired chart configurations. We pass it our y and x data series, making sure to convert from the numpy.float64, since nvd3 cannot work with unconverted numpy types at this point.

In [3]:
chart_type = 'discreteBarChart'
chart = nvd3.discreteBarChart(name=chart_type, height=500, width=500)

ydata = [float(x) for x in np.random.randn(10)]
xdata = [int(x) for x in np.arange(10)]

chart.add_serie(y=ydata, x=xdata)
chart.buildhtml()
chart_html = chart.htmlcontent

We can see the output of the rendered HTML -- it's created some JS and HTML elements -

In [4]:
chart_html
Out[4]:
'<!DOCTYPE html>\n<html lang="en">\n    <head>\n        <meta charset="utf-8" />\n        <link href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.7.0/nv.d3.min.css" rel="stylesheet" />\n        <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>\n        <script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.7.0/nv.d3.min.js"></script>\n    </head>\n    <body>\n        \n    <div id="discretebarchart"><svg style="width:500px;height:500px;"></svg></div>\n\n\n    <script>\n\n\n\n                data_discretebarchart=[{"key": "Serie 1", "values": [{"x": 0, "y": -1.7497654730546974}, {"x": 1, "y": 0.34268040332750216}, {"x": 2, "y": 1.153035802563644}, {"x": 3, "y": -0.25243603652138985}, {"x": 4, "y": 0.9813207869512316}, {"x": 5, "y": 0.5142188413943821}, {"x": 6, "y": 0.22117966922140045}, {"x": 7, "y": -1.0700433305682933}, {"x": 8, "y": -0.18949583082317534}, {"x": 9, "y": 0.25500144427338167}], "yAxis": "1"}];\n\n\n            nv.addGraph(function() {\n        var chart = nv.models.discreteBarChart();\n\n        chart.margin({top: 30, right: 60, bottom: 20, left: 60});\n\n        var datum = data_discretebarchart;\n\n\n\n                    chart.yAxis\n                .tickFormat(d3.format(\',.0f\'));\n\n    \n    \n\n        \n\n\n\n            d3.select(\'#discretebarchart svg\')\n            .datum(datum)\n            .transition().duration(500)\n            .attr(\'width\', 500)\n            .attr(\'height\', 500)\n            .call(chart);\n\n    \n        });\n\n\n\n    </script>\n\n    </body>\n</html>'
And if we want to view the actual chart, we can use the display and HTML functions below:

In [5]:
from IPython.display import display, HTML


display(HTML(chart_html))

For more info, check out the official nvd3.js docs here, or the Python-nvd3 docs here.

No comments:

Post a Comment

Sign up to receive data viz talk tips and updates via email. We're low traffic and take privacy very seriously.