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]:
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