SlideShare a Scribd company logo
Brian Coffey
Data Scientist, Stitch Fix
PyData NYC 2015
D3 in Jupyter
https://github.com/stitchfix/d3-jupyter-tutorial
Overview
1. Why you may occasionally wish to do this (but likely not all the time)
2. Hello DOM!
3. Meet bl.ocks.org, I think you’ll be great friends
4. A simple D3 scatterplot in Jupyter
5. A bar chart with interactivity (hello again DOM!)
6. Networks, maps and more
7. D3_lib.py (if you want to)
8. Discussion: From sketch to notebook frame
Why not all the time?
matplotlib, mpld3, bokeh, seaborn, toyplot, vispy, vincent, …
1. Why you may occasionally wish to do this (but likely not all the time)
Why not all the time?
matplotlib, mpld3, bokeh, seaborn, toyplot, vispy, vincent, …
Okay. Then why bother at all?
1. Maybe you just want the ultimate freedom in your data visualization, and
you happen to have some extra time on your hands, and/or you happen
to be a D3 wiz.
2. Maybe the perfect visualization for your analytical needs is not readily
available with other graphing tools, but there is a D3 example out there
that would be perfect with just some minor tweaks.
3. Maybe you are collaborating with a front-end person on a custom viz
1. Why you may occasionally wish to do this (but likely not all the time)
Just a heads up before we dig in.
If you are not already familiar with html, css and javascript, your eyes may
glaze over a bit during the first part of this tutorial.
But get what you can out of it, as understanding how it works will help you
to make the most of D3 in your Jupyter notebooks. I will try to be very clear
on the essential pieces of information you need to function, and on the
major gotchas for troubleshooting.
And if you get too lost, fear not. At the other end, there is a small helper
library and a collection of examples that you can use straight out of the box.
https://github.com/stitchfix/d3-jupyter-tutorial
2. Hello DOM!
“Oh, you can just call me Document Object Model for short”
Check me out in your browser’s development tools. Highlight a particular
element using the magnifying glass.
notebook: hello_dom.ipynb
from IPython.core.display import HTML
HTML( ‘<h1>Hello DOM!</h1>’ )
in general … HTML( [insert a html string here and I will put it in the DOM] )
(I will put it in the div assigned to the output_result of the code block that I am in.)
2. Hello DOM! hello_dom.ipynb
Let’s add some style…
HTML(‘
<style> … </style>
<h1 class=“steely”>Hello DOM!</h1>’)
2. Hello DOM! hello_dom.ipynb
And some javascript to modify the newly-added DOM element
HTML( ‘…
<h1 class=“steely” id=“steely-DOM”>Hello DOM!</h1>
<script>$(‘steely-DOM’).text(“Hello JavaScript!!!”)</script>’)
2. Hello DOM! hello_dom.ipynb
Great. But how does D3 fit into this?
D3 is a JavaScript library. The Ds are for Data Driven Documents.
Think ‘Data Driven DOM’.
A core aspect of using the D3 library is the following pattern:
1. Select some array of DOM elements
Note that this may be an empty array
2. ‘Attach’ an array of data to that array of DOM elements
The array lengths need not match. Extra data may ‘enter’, or DOM elements may ‘exit’. D3
coordinates all of this with elegance.
3. Perform some actions for each element in the data array
This will generally involve modifying the selected DOM elements, or removing or appending
new DOM elements, based on the values of the data array.
2. Hello DOM! hello_dom.ipynb
Appending elements to the DOM with D3
2. Hello DOM! hello_dom.ipynb
Just one last step – writing data from Python into the JavaScript for use by
D3
2. Hello DOM! hello_dom.ipynb
A Brief Review of Key Points:
IPython.core.display.HTML allows you to directly write new DOM elements
With that HTML function, we can add css (<style>) and javascript (<script>)
elements, as well as <div> elements (or <h1> elements, etc) for them to act
on
D3 is a clever javascript library for DOM modification based on arrays of
data
We can write python data directly into <script> element text so that it can be
acted upon by D3.
2. Hello DOM! hello_dom.ipynb
for any late-joiners:
https://github.com/stitchfix/d3-jupyter-tutorial
A Brief Review of Key Points:
IPython.core.display.HTML allows you to directly write new DOM elements
With that HTML function, we can add css (<style>) and javascript (<script>)
elements, as well as <div> elements (or <h1> elements, etc) for them to act
on
D3 is a clever javascript library for DOM modification based on arrays of
data
We can write python data directly into <script> element text so that it can be
acted upon by D3.
2. Hello DOM! hello_dom.ipynb
https://github.com/stitchfix/d3-jupyter-tutorial
Sure. Great. But I haven’t seen any data visualizations yet…
3. Meet bl.ocks.org, I think you’ll be great friends
http://bl.ocks.org/mbostock/4063269http://bl.ocks.org/mbostock/1353700http://bl.ocks.org/mbostock/4060954
d3js.org , bl.ocks.org/mbostock
(And welcome to data visualization heaven)
http://bl.ocks.org/mbostock/4060954
Anatomy of a bl.ock
Pretty graphic, often interactive
Short description
html, css and javascript that
produces the graphic
data that gets read by script,
usually in csv or tsv form
3. Meet bl.ocks.org, I think you’ll be great friends
Using this bl.ocks example: http://bl.ocks.org/mbostock/3887118
We will produce this in Jupyter:
4. A simple D3 scatterplot in Jupyter iris_scatterplot.ipynb
5. A bar chart with interactivity bar_chart_with_update.ipynb
(hello again DOM!)
watch out for the twist…
6. Networks, maps and more
sigma_js_graph.ipynb
temperature_histories.ipynb
polyFill_d3.ipynb
3d_meshing.ipynb
7. D3_lib.py (if you want to) multiple_simple_examples.ipynb
We have encapsulated a lot of the boilerplate for this method into an
external python file, which in turn calls external files containing your css
and js templates.
This just helps to keep things visually clean.
Feel free to use it as is and/or modify it to suit your needs.
8. Discussion: From sketch to notebook frame randomized_sketch.ipynb
Data Science @ Stitch
Fixbcoffey@stitchfix.com multithreaded.stitchfix.com
Thank you!

More Related Content

PDF
Data analytics in the cloud with Jupyter notebooks.
PDF
Parallel Programming in Python: Speeding up your analysis
ODP
Dynamic Python
ODP
Introduce Django
PDF
Multiprocessing with python
PDF
Memory Management In Python The Basics
KEY
Gae icc fall2011
Data analytics in the cloud with Jupyter notebooks.
Parallel Programming in Python: Speeding up your analysis
Dynamic Python
Introduce Django
Multiprocessing with python
Memory Management In Python The Basics
Gae icc fall2011

What's hot (20)

ODP
Pythonpresent
PPT
The Theory Of The Dom
PDF
TensorFlow.Data 및 TensorFlow Hub
PPTX
An Introduction to TensorFlow architecture
PPTX
Intro to Python (High School) Unit #2
PDF
Pitfalls of object_oriented_programming_gcap_09
PDF
NANO266 - Lecture 9 - Tools of the Modeling Trade
PDF
MAVRL Workshop 2014 - pymatgen-db & custodian
PDF
the productive programer: mechanics
PDF
Python Interview Questions And Answers 2019 | Edureka
PPTX
Data oriented design and c++
PPTX
refORM: Death to ORMs in .NET
PDF
FireWorks workflow software
PPTX
Python Homework Help
PPT
Task and Data Parallelism
PDF
Textual Modeling Framework Xtext
PPTX
Tensorflow internal
PDF
Perl-C/C++ Integration with Swig
PPT
jQuery with javascript training by Technnovation Labs
Pythonpresent
The Theory Of The Dom
TensorFlow.Data 및 TensorFlow Hub
An Introduction to TensorFlow architecture
Intro to Python (High School) Unit #2
Pitfalls of object_oriented_programming_gcap_09
NANO266 - Lecture 9 - Tools of the Modeling Trade
MAVRL Workshop 2014 - pymatgen-db & custodian
the productive programer: mechanics
Python Interview Questions And Answers 2019 | Edureka
Data oriented design and c++
refORM: Death to ORMs in .NET
FireWorks workflow software
Python Homework Help
Task and Data Parallelism
Textual Modeling Framework Xtext
Tensorflow internal
Perl-C/C++ Integration with Swig
jQuery with javascript training by Technnovation Labs
Ad

Viewers also liked (20)

PDF
Unsupervised Computer Vision: The Current State of the Art
PDF
Jupyter Kernel: How to Speak in Another Language
KEY
Interactive Data Visualization (with D3.js)
PDF
Jupyter, A Platform for Data Science at Scale
PDF
Introduction to data visualisations with d3.js — Data Driven Documents
PDF
Big Data visualization with Apache Spark and Zeppelin
PDF
Visual Design with Data
PPTX
Jupyter Ascending: a practical hand guide to galactic scale, reproducible dat...
PDF
Data to Go: Mobile API Design
PDF
Building Mobile Dashboards With D3 and Google Charts
PDF
The Compatibility Challenge:Examining R and Developing TERR
PPTX
Veracity Embracing Uncertainty
PPTX
OSINT using Twitter & Python
PDF
Introduction to data visualisation with D3
PDF
Hear no evil, see no evil, patch no evil: Or, how to monkey-patch safely.
PDF
Jupyterの機能を拡張してみた
ODP
Examples of D3 Visualizations
PPTX
D3js learning tips
PDF
Advanced D3 Charting
PDF
THE DATA DRIVEN WEB OF NOW: EXTENDING D3JS (Travis Smith)
Unsupervised Computer Vision: The Current State of the Art
Jupyter Kernel: How to Speak in Another Language
Interactive Data Visualization (with D3.js)
Jupyter, A Platform for Data Science at Scale
Introduction to data visualisations with d3.js — Data Driven Documents
Big Data visualization with Apache Spark and Zeppelin
Visual Design with Data
Jupyter Ascending: a practical hand guide to galactic scale, reproducible dat...
Data to Go: Mobile API Design
Building Mobile Dashboards With D3 and Google Charts
The Compatibility Challenge:Examining R and Developing TERR
Veracity Embracing Uncertainty
OSINT using Twitter & Python
Introduction to data visualisation with D3
Hear no evil, see no evil, patch no evil: Or, how to monkey-patch safely.
Jupyterの機能を拡張してみた
Examples of D3 Visualizations
D3js learning tips
Advanced D3 Charting
THE DATA DRIVEN WEB OF NOW: EXTENDING D3JS (Travis Smith)
Ad

Similar to D3 in Jupyter : PyData NYC 2015 (20)

PDF
Introduction to d3js (and SVG)
PDF
Learn D3.js in 90 minutes
PPTX
Visdjango presentation django_boston_oct_2014
PDF
Data Visualization on the Web - Intro to D3
PPTX
Academy PRO: D3, part 1
PDF
React, D3 and the dataviz ecosystem
PPTX
Introduction to D3.js
PPT
Data Visualizations with D3
PDF
A Quick and Dirty D3.js Tutorial
PPTX
PDF
D3 data visualization
PDF
Utahjs D3
PDF
Data visualization with d3 may19
PDF
Overview of D3.js (1)
PPTX
D3.JS Tips & Tricks (export to svg, crossfilter, maps etc.)
PPTX
Data visualisation with D3
PPTX
SVCC 2013 D3.js Presentation (10/05/2013)
PPTX
Svcc 2013-d3
PPTX
The simplest guide to d3
PDF
Improving D3 Performance with CANVAS and other Hacks
Introduction to d3js (and SVG)
Learn D3.js in 90 minutes
Visdjango presentation django_boston_oct_2014
Data Visualization on the Web - Intro to D3
Academy PRO: D3, part 1
React, D3 and the dataviz ecosystem
Introduction to D3.js
Data Visualizations with D3
A Quick and Dirty D3.js Tutorial
D3 data visualization
Utahjs D3
Data visualization with d3 may19
Overview of D3.js (1)
D3.JS Tips & Tricks (export to svg, crossfilter, maps etc.)
Data visualisation with D3
SVCC 2013 D3.js Presentation (10/05/2013)
Svcc 2013-d3
The simplest guide to d3
Improving D3 Performance with CANVAS and other Hacks

Recently uploaded (20)

PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPT
Quality review (1)_presentation of this 21
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PDF
Launch Your Data Science Career in Kochi – 2025
PPTX
Computer network topology notes for revision
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PPTX
Business Acumen Training GuidePresentation.pptx
PPT
Reliability_Chapter_ presentation 1221.5784
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PPTX
Global journeys: estimating international migration
PPTX
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPTX
CEE 2 REPORT G7.pptxbdbshjdgsgjgsjfiuhsd
PPTX
Database Infoormation System (DBIS).pptx
PDF
Mega Projects Data Mega Projects Data
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Quality review (1)_presentation of this 21
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Launch Your Data Science Career in Kochi – 2025
Computer network topology notes for revision
oil_refinery_comprehensive_20250804084928 (1).pptx
Business Acumen Training GuidePresentation.pptx
Reliability_Chapter_ presentation 1221.5784
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Global journeys: estimating international migration
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
Introduction-to-Cloud-ComputingFinal.pptx
.pdf is not working space design for the following data for the following dat...
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
CEE 2 REPORT G7.pptxbdbshjdgsgjgsjfiuhsd
Database Infoormation System (DBIS).pptx
Mega Projects Data Mega Projects Data

D3 in Jupyter : PyData NYC 2015

  • 1. Brian Coffey Data Scientist, Stitch Fix PyData NYC 2015 D3 in Jupyter
  • 3. Overview 1. Why you may occasionally wish to do this (but likely not all the time) 2. Hello DOM! 3. Meet bl.ocks.org, I think you’ll be great friends 4. A simple D3 scatterplot in Jupyter 5. A bar chart with interactivity (hello again DOM!) 6. Networks, maps and more 7. D3_lib.py (if you want to) 8. Discussion: From sketch to notebook frame
  • 4. Why not all the time? matplotlib, mpld3, bokeh, seaborn, toyplot, vispy, vincent, … 1. Why you may occasionally wish to do this (but likely not all the time)
  • 5. Why not all the time? matplotlib, mpld3, bokeh, seaborn, toyplot, vispy, vincent, … Okay. Then why bother at all? 1. Maybe you just want the ultimate freedom in your data visualization, and you happen to have some extra time on your hands, and/or you happen to be a D3 wiz. 2. Maybe the perfect visualization for your analytical needs is not readily available with other graphing tools, but there is a D3 example out there that would be perfect with just some minor tweaks. 3. Maybe you are collaborating with a front-end person on a custom viz 1. Why you may occasionally wish to do this (but likely not all the time)
  • 6. Just a heads up before we dig in. If you are not already familiar with html, css and javascript, your eyes may glaze over a bit during the first part of this tutorial. But get what you can out of it, as understanding how it works will help you to make the most of D3 in your Jupyter notebooks. I will try to be very clear on the essential pieces of information you need to function, and on the major gotchas for troubleshooting. And if you get too lost, fear not. At the other end, there is a small helper library and a collection of examples that you can use straight out of the box. https://github.com/stitchfix/d3-jupyter-tutorial
  • 7. 2. Hello DOM! “Oh, you can just call me Document Object Model for short” Check me out in your browser’s development tools. Highlight a particular element using the magnifying glass. notebook: hello_dom.ipynb
  • 8. from IPython.core.display import HTML HTML( ‘<h1>Hello DOM!</h1>’ ) in general … HTML( [insert a html string here and I will put it in the DOM] ) (I will put it in the div assigned to the output_result of the code block that I am in.) 2. Hello DOM! hello_dom.ipynb
  • 9. Let’s add some style… HTML(‘ <style> … </style> <h1 class=“steely”>Hello DOM!</h1>’) 2. Hello DOM! hello_dom.ipynb
  • 10. And some javascript to modify the newly-added DOM element HTML( ‘… <h1 class=“steely” id=“steely-DOM”>Hello DOM!</h1> <script>$(‘steely-DOM’).text(“Hello JavaScript!!!”)</script>’) 2. Hello DOM! hello_dom.ipynb
  • 11. Great. But how does D3 fit into this? D3 is a JavaScript library. The Ds are for Data Driven Documents. Think ‘Data Driven DOM’. A core aspect of using the D3 library is the following pattern: 1. Select some array of DOM elements Note that this may be an empty array 2. ‘Attach’ an array of data to that array of DOM elements The array lengths need not match. Extra data may ‘enter’, or DOM elements may ‘exit’. D3 coordinates all of this with elegance. 3. Perform some actions for each element in the data array This will generally involve modifying the selected DOM elements, or removing or appending new DOM elements, based on the values of the data array. 2. Hello DOM! hello_dom.ipynb
  • 12. Appending elements to the DOM with D3 2. Hello DOM! hello_dom.ipynb
  • 13. Just one last step – writing data from Python into the JavaScript for use by D3 2. Hello DOM! hello_dom.ipynb
  • 14. A Brief Review of Key Points: IPython.core.display.HTML allows you to directly write new DOM elements With that HTML function, we can add css (<style>) and javascript (<script>) elements, as well as <div> elements (or <h1> elements, etc) for them to act on D3 is a clever javascript library for DOM modification based on arrays of data We can write python data directly into <script> element text so that it can be acted upon by D3. 2. Hello DOM! hello_dom.ipynb for any late-joiners: https://github.com/stitchfix/d3-jupyter-tutorial
  • 15. A Brief Review of Key Points: IPython.core.display.HTML allows you to directly write new DOM elements With that HTML function, we can add css (<style>) and javascript (<script>) elements, as well as <div> elements (or <h1> elements, etc) for them to act on D3 is a clever javascript library for DOM modification based on arrays of data We can write python data directly into <script> element text so that it can be acted upon by D3. 2. Hello DOM! hello_dom.ipynb https://github.com/stitchfix/d3-jupyter-tutorial Sure. Great. But I haven’t seen any data visualizations yet…
  • 16. 3. Meet bl.ocks.org, I think you’ll be great friends http://bl.ocks.org/mbostock/4063269http://bl.ocks.org/mbostock/1353700http://bl.ocks.org/mbostock/4060954 d3js.org , bl.ocks.org/mbostock (And welcome to data visualization heaven)
  • 17. http://bl.ocks.org/mbostock/4060954 Anatomy of a bl.ock Pretty graphic, often interactive Short description html, css and javascript that produces the graphic data that gets read by script, usually in csv or tsv form 3. Meet bl.ocks.org, I think you’ll be great friends
  • 18. Using this bl.ocks example: http://bl.ocks.org/mbostock/3887118 We will produce this in Jupyter: 4. A simple D3 scatterplot in Jupyter iris_scatterplot.ipynb
  • 19. 5. A bar chart with interactivity bar_chart_with_update.ipynb (hello again DOM!) watch out for the twist…
  • 20. 6. Networks, maps and more sigma_js_graph.ipynb temperature_histories.ipynb polyFill_d3.ipynb 3d_meshing.ipynb
  • 21. 7. D3_lib.py (if you want to) multiple_simple_examples.ipynb We have encapsulated a lot of the boilerplate for this method into an external python file, which in turn calls external files containing your css and js templates. This just helps to keep things visually clean. Feel free to use it as is and/or modify it to suit your needs.
  • 22. 8. Discussion: From sketch to notebook frame randomized_sketch.ipynb
  • 23. Data Science @ Stitch Fixbcoffey@stitchfix.com multithreaded.stitchfix.com Thank you!