├── README.md
├── BAR CHART IN D3.ipynb
├── .ipynb_checkpoints
├── BAR CHART IN D3-checkpoint.ipynb
└── SCATTER PLOT-checkpoint.ipynb
└── SCATTER PLOT.ipynb
/README.md:
--------------------------------------------------------------------------------
1 | # D3-in-Jupyter-Notebook
2 |
3 |
4 | This Blog is going to introduce D3 and how it can be used in Juypter Notebook . The blog is going to be on the basis of the details from the PyData ,NewYork, a talk on visualization by Brian Coffey
5 |
6 | The link the blog : https://medium.com/@stallonejacob/d3-in-jupyter-notebook-685d6dca75c8
7 |
--------------------------------------------------------------------------------
/BAR CHART IN D3.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# BAR CHART IN D3"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "## INTRODUCTION "
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "This is code continuation that is based on the blog : \n",
22 | " \n",
23 | " \n",
24 | "The notebook is a simple example of us D3 and how to make updates.\n",
25 | "\n",
26 | "The link to bl.ock is here : https://bl.ocks.org/mbostock/3885304\n",
27 | "\n",
28 | "Please refer to the blog for the explanation of this note book ."
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 11,
34 | "metadata": {
35 | "collapsed": true
36 | },
37 | "outputs": [],
38 | "source": [
39 | "from IPython.core.display import display, HTML\n",
40 | "from string import Template\n",
41 | "import pandas as pd\n",
42 | "import json, random"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": 12,
48 | "metadata": {},
49 | "outputs": [
50 | {
51 | "data": {
52 | "text/html": [
53 | ""
54 | ],
55 | "text/plain": [
56 | ""
57 | ]
58 | },
59 | "execution_count": 12,
60 | "metadata": {},
61 | "output_type": "execute_result"
62 | }
63 | ],
64 | "source": [
65 | "# Get the D3 host locally. \n",
66 | "HTML('')"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": 13,
72 | "metadata": {
73 | "collapsed": true
74 | },
75 | "outputs": [],
76 | "source": [
77 | "#HTML templet\n",
78 | "html_template = Template('''\n",
79 | "\n",
80 | "\n",
81 | "\n",
82 | "''')"
83 | ]
84 | },
85 | {
86 | "cell_type": "code",
87 | "execution_count": 14,
88 | "metadata": {
89 | "collapsed": true
90 | },
91 | "outputs": [],
92 | "source": [
93 | "# Css templet\n",
94 | "css_text = '''\n",
95 | "\n",
96 | ".bar {\n",
97 | " fill: steelblue;\n",
98 | "}\n",
99 | "\n",
100 | ".bar:hover {\n",
101 | " fill: brown;\n",
102 | "}\n",
103 | "\n",
104 | ".axis {\n",
105 | " font: 10px sans-serif;\n",
106 | "}\n",
107 | "\n",
108 | ".axis path,\n",
109 | ".axis line {\n",
110 | " fill: none;\n",
111 | " stroke: #000;\n",
112 | " shape-rendering: crispEdges;\n",
113 | "}\n",
114 | "\n",
115 | ".x.axis path {\n",
116 | " display: none;\n",
117 | "}\n",
118 | "\n",
119 | "'''"
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": 15,
125 | "metadata": {
126 | "collapsed": true
127 | },
128 | "outputs": [],
129 | "source": [
130 | "#Java script templet\n",
131 | "js_text_template = Template('''\n",
132 | "\n",
133 | "var margin = {top: 20, right: 20, bottom: 30, left: 40},\n",
134 | " width = 500 - margin.left - margin.right,\n",
135 | " height = 300 - margin.top - margin.bottom;\n",
136 | "\n",
137 | "var x = d3.scale.ordinal()\n",
138 | " .rangeRoundBands([0, width], .1);\n",
139 | "\n",
140 | "var y = d3.scale.linear()\n",
141 | " .range([height, 0]);\n",
142 | "\n",
143 | "var xAxis = d3.svg.axis()\n",
144 | " .scale(x)\n",
145 | " .orient(\"bottom\");\n",
146 | "\n",
147 | "var yAxis = d3.svg.axis()\n",
148 | " .scale(y)\n",
149 | " .orient(\"left\");\n",
150 | "\n",
151 | "var svg = d3.select(\"#graph-div\").append(\"svg\")\n",
152 | " .attr(\"width\", width + margin.left + margin.right)\n",
153 | " .attr(\"height\", height + margin.top + margin.bottom)\n",
154 | " .append(\"g\")\n",
155 | " .attr(\"transform\", \"translate(\" + margin.left + \",\" + margin.top + \")\");\n",
156 | "\n",
157 | "var data = $data ;\n",
158 | "\n",
159 | " x.domain(data.map(function(d) { return d.letter; }));\n",
160 | " y.domain([0, d3.max(data, function(d) { return d.y; })]);\n",
161 | "\n",
162 | " svg.append(\"g\")\n",
163 | " .attr(\"class\", \"x axis\")\n",
164 | " .attr(\"transform\", \"translate(0,\" + height + \")\")\n",
165 | " .call(xAxis);\n",
166 | "\n",
167 | " svg.append(\"g\")\n",
168 | " .attr(\"class\", \"y axis\")\n",
169 | " .call(yAxis);\n",
170 | "\n",
171 | " svg.selectAll(\".bar\")\n",
172 | " .data(data)\n",
173 | " .enter().append(\"rect\")\n",
174 | " .attr(\"class\", \"bar\")\n",
175 | " .attr(\"x\", function(d) { return x(d.letter); })\n",
176 | " .attr(\"width\", x.rangeBand())\n",
177 | " .attr(\"y\", function(d) { return y(d.y); })\n",
178 | " .attr(\"height\", function(d) { return height - y(d.y); });\n",
179 | "\n",
180 | "''')"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": 16,
186 | "metadata": {
187 | "collapsed": true
188 | },
189 | "outputs": [],
190 | "source": [
191 | "js_text_template_2 = Template('''\n",
192 | "\n",
193 | "var bars = svg.selectAll(\".bar\").data($data);\n",
194 | " \n",
195 | "bars\n",
196 | " .transition()\n",
197 | " .attr(\"y\", function(d) { return y(d.y); })\n",
198 | " .attr(\"height\", function(d) { return height - y(d.y); });\n",
199 | "\n",
200 | "''')"
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": 17,
206 | "metadata": {},
207 | "outputs": [
208 | {
209 | "data": {
210 | "text/html": [
211 | "\n",
212 | "\n",
225 | "
\n",
226 | " \n",
227 | " \n",
228 | " | \n",
229 | " letter | \n",
230 | " y | \n",
231 | "
\n",
232 | " \n",
233 | " \n",
234 | " \n",
235 | " | 0 | \n",
236 | " A | \n",
237 | " 1 | \n",
238 | "
\n",
239 | " \n",
240 | " | 1 | \n",
241 | " B | \n",
242 | " 1 | \n",
243 | "
\n",
244 | " \n",
245 | " | 2 | \n",
246 | " C | \n",
247 | " 1 | \n",
248 | "
\n",
249 | " \n",
250 | " | 3 | \n",
251 | " D | \n",
252 | " 1 | \n",
253 | "
\n",
254 | " \n",
255 | "
\n",
256 | "
"
257 | ],
258 | "text/plain": [
259 | " letter y\n",
260 | "0 A 1\n",
261 | "1 B 1\n",
262 | "2 C 1\n",
263 | "3 D 1"
264 | ]
265 | },
266 | "execution_count": 17,
267 | "metadata": {},
268 | "output_type": "execute_result"
269 | }
270 | ],
271 | "source": [
272 | "data = pd.DataFrame({'letter': ['A','B','C','D'], 'y': [1,1,1,1]})\n",
273 | "data.head()"
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "execution_count": 18,
279 | "metadata": {},
280 | "outputs": [
281 | {
282 | "data": {
283 | "text/html": [
284 | "\n",
285 | "\n",
311 | "\n",
312 | "\n"
362 | ],
363 | "text/plain": [
364 | ""
365 | ]
366 | },
367 | "execution_count": 18,
368 | "metadata": {},
369 | "output_type": "execute_result"
370 | }
371 | ],
372 | "source": [
373 | "js_text = js_text_template.substitute({'data': json.dumps(data.to_dict(orient='records'))})\n",
374 | "HTML(html_template.substitute({'css_text': css_text, 'js_text': js_text}))"
375 | ]
376 | },
377 | {
378 | "cell_type": "code",
379 | "execution_count": 19,
380 | "metadata": {},
381 | "outputs": [
382 | {
383 | "data": {
384 | "text/html": [
385 | ""
395 | ],
396 | "text/plain": [
397 | ""
398 | ]
399 | },
400 | "execution_count": 19,
401 | "metadata": {},
402 | "output_type": "execute_result"
403 | }
404 | ],
405 | "source": [
406 | "data['y'] = [random.uniform(0,1) for d in data['y']]\n",
407 | "js_text = js_text_template_2.substitute({'data': json.dumps(data.to_dict(orient='records'))})\n",
408 | "HTML('')"
409 | ]
410 | }
411 | ],
412 | "metadata": {
413 | "anaconda-cloud": {},
414 | "kernelspec": {
415 | "display_name": "Python [default]",
416 | "language": "python",
417 | "name": "python2"
418 | },
419 | "language_info": {
420 | "codemirror_mode": {
421 | "name": "ipython",
422 | "version": 2
423 | },
424 | "file_extension": ".py",
425 | "mimetype": "text/x-python",
426 | "name": "python",
427 | "nbconvert_exporter": "python",
428 | "pygments_lexer": "ipython2",
429 | "version": "2.7.13"
430 | },
431 | "toc": {
432 | "nav_menu": {},
433 | "number_sections": true,
434 | "sideBar": true,
435 | "skip_h1_title": false,
436 | "toc_cell": false,
437 | "toc_position": {},
438 | "toc_section_display": "block",
439 | "toc_window_display": false
440 | }
441 | },
442 | "nbformat": 4,
443 | "nbformat_minor": 2
444 | }
445 |
--------------------------------------------------------------------------------
/.ipynb_checkpoints/BAR CHART IN D3-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# BAR CHART IN D3"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "## INTRODUCTION "
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "This is code continuation that is based on the blog : \n",
22 | " \n",
23 | " \n",
24 | "The notebook is a simple example of us D3 and how to make updates.\n",
25 | "\n",
26 | "The link to bl.ock is here : https://bl.ocks.org/mbostock/3885304\n",
27 | "\n",
28 | "Please refer to the blog for the explanation of this note book ."
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 11,
34 | "metadata": {
35 | "collapsed": true
36 | },
37 | "outputs": [],
38 | "source": [
39 | "from IPython.core.display import display, HTML\n",
40 | "from string import Template\n",
41 | "import pandas as pd\n",
42 | "import json, random"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": 12,
48 | "metadata": {},
49 | "outputs": [
50 | {
51 | "data": {
52 | "text/html": [
53 | ""
54 | ],
55 | "text/plain": [
56 | ""
57 | ]
58 | },
59 | "execution_count": 12,
60 | "metadata": {},
61 | "output_type": "execute_result"
62 | }
63 | ],
64 | "source": [
65 | "# Get the D3 host locally. \n",
66 | "HTML('')"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": 13,
72 | "metadata": {
73 | "collapsed": true
74 | },
75 | "outputs": [],
76 | "source": [
77 | "#HTML templet\n",
78 | "html_template = Template('''\n",
79 | "\n",
80 | "\n",
81 | "\n",
82 | "''')"
83 | ]
84 | },
85 | {
86 | "cell_type": "code",
87 | "execution_count": 14,
88 | "metadata": {
89 | "collapsed": true
90 | },
91 | "outputs": [],
92 | "source": [
93 | "# Css templet\n",
94 | "css_text = '''\n",
95 | "\n",
96 | ".bar {\n",
97 | " fill: steelblue;\n",
98 | "}\n",
99 | "\n",
100 | ".bar:hover {\n",
101 | " fill: brown;\n",
102 | "}\n",
103 | "\n",
104 | ".axis {\n",
105 | " font: 10px sans-serif;\n",
106 | "}\n",
107 | "\n",
108 | ".axis path,\n",
109 | ".axis line {\n",
110 | " fill: none;\n",
111 | " stroke: #000;\n",
112 | " shape-rendering: crispEdges;\n",
113 | "}\n",
114 | "\n",
115 | ".x.axis path {\n",
116 | " display: none;\n",
117 | "}\n",
118 | "\n",
119 | "'''"
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": 15,
125 | "metadata": {
126 | "collapsed": true
127 | },
128 | "outputs": [],
129 | "source": [
130 | "#Java script templet\n",
131 | "js_text_template = Template('''\n",
132 | "\n",
133 | "var margin = {top: 20, right: 20, bottom: 30, left: 40},\n",
134 | " width = 500 - margin.left - margin.right,\n",
135 | " height = 300 - margin.top - margin.bottom;\n",
136 | "\n",
137 | "var x = d3.scale.ordinal()\n",
138 | " .rangeRoundBands([0, width], .1);\n",
139 | "\n",
140 | "var y = d3.scale.linear()\n",
141 | " .range([height, 0]);\n",
142 | "\n",
143 | "var xAxis = d3.svg.axis()\n",
144 | " .scale(x)\n",
145 | " .orient(\"bottom\");\n",
146 | "\n",
147 | "var yAxis = d3.svg.axis()\n",
148 | " .scale(y)\n",
149 | " .orient(\"left\");\n",
150 | "\n",
151 | "var svg = d3.select(\"#graph-div\").append(\"svg\")\n",
152 | " .attr(\"width\", width + margin.left + margin.right)\n",
153 | " .attr(\"height\", height + margin.top + margin.bottom)\n",
154 | " .append(\"g\")\n",
155 | " .attr(\"transform\", \"translate(\" + margin.left + \",\" + margin.top + \")\");\n",
156 | "\n",
157 | "var data = $data ;\n",
158 | "\n",
159 | " x.domain(data.map(function(d) { return d.letter; }));\n",
160 | " y.domain([0, d3.max(data, function(d) { return d.y; })]);\n",
161 | "\n",
162 | " svg.append(\"g\")\n",
163 | " .attr(\"class\", \"x axis\")\n",
164 | " .attr(\"transform\", \"translate(0,\" + height + \")\")\n",
165 | " .call(xAxis);\n",
166 | "\n",
167 | " svg.append(\"g\")\n",
168 | " .attr(\"class\", \"y axis\")\n",
169 | " .call(yAxis);\n",
170 | "\n",
171 | " svg.selectAll(\".bar\")\n",
172 | " .data(data)\n",
173 | " .enter().append(\"rect\")\n",
174 | " .attr(\"class\", \"bar\")\n",
175 | " .attr(\"x\", function(d) { return x(d.letter); })\n",
176 | " .attr(\"width\", x.rangeBand())\n",
177 | " .attr(\"y\", function(d) { return y(d.y); })\n",
178 | " .attr(\"height\", function(d) { return height - y(d.y); });\n",
179 | "\n",
180 | "''')"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": 16,
186 | "metadata": {
187 | "collapsed": true
188 | },
189 | "outputs": [],
190 | "source": [
191 | "js_text_template_2 = Template('''\n",
192 | "\n",
193 | "var bars = svg.selectAll(\".bar\").data($data);\n",
194 | " \n",
195 | "bars\n",
196 | " .transition()\n",
197 | " .attr(\"y\", function(d) { return y(d.y); })\n",
198 | " .attr(\"height\", function(d) { return height - y(d.y); });\n",
199 | "\n",
200 | "''')"
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": 17,
206 | "metadata": {},
207 | "outputs": [
208 | {
209 | "data": {
210 | "text/html": [
211 | "\n",
212 | "\n",
225 | "
\n",
226 | " \n",
227 | " \n",
228 | " | \n",
229 | " letter | \n",
230 | " y | \n",
231 | "
\n",
232 | " \n",
233 | " \n",
234 | " \n",
235 | " | 0 | \n",
236 | " A | \n",
237 | " 1 | \n",
238 | "
\n",
239 | " \n",
240 | " | 1 | \n",
241 | " B | \n",
242 | " 1 | \n",
243 | "
\n",
244 | " \n",
245 | " | 2 | \n",
246 | " C | \n",
247 | " 1 | \n",
248 | "
\n",
249 | " \n",
250 | " | 3 | \n",
251 | " D | \n",
252 | " 1 | \n",
253 | "
\n",
254 | " \n",
255 | "
\n",
256 | "
"
257 | ],
258 | "text/plain": [
259 | " letter y\n",
260 | "0 A 1\n",
261 | "1 B 1\n",
262 | "2 C 1\n",
263 | "3 D 1"
264 | ]
265 | },
266 | "execution_count": 17,
267 | "metadata": {},
268 | "output_type": "execute_result"
269 | }
270 | ],
271 | "source": [
272 | "data = pd.DataFrame({'letter': ['A','B','C','D'], 'y': [1,1,1,1]})\n",
273 | "data.head()"
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "execution_count": 18,
279 | "metadata": {},
280 | "outputs": [
281 | {
282 | "data": {
283 | "text/html": [
284 | "\n",
285 | "\n",
311 | "\n",
312 | "\n"
362 | ],
363 | "text/plain": [
364 | ""
365 | ]
366 | },
367 | "execution_count": 18,
368 | "metadata": {},
369 | "output_type": "execute_result"
370 | }
371 | ],
372 | "source": [
373 | "js_text = js_text_template.substitute({'data': json.dumps(data.to_dict(orient='records'))})\n",
374 | "HTML(html_template.substitute({'css_text': css_text, 'js_text': js_text}))"
375 | ]
376 | },
377 | {
378 | "cell_type": "code",
379 | "execution_count": 19,
380 | "metadata": {},
381 | "outputs": [
382 | {
383 | "data": {
384 | "text/html": [
385 | ""
395 | ],
396 | "text/plain": [
397 | ""
398 | ]
399 | },
400 | "execution_count": 19,
401 | "metadata": {},
402 | "output_type": "execute_result"
403 | }
404 | ],
405 | "source": [
406 | "data['y'] = [random.uniform(0,1) for d in data['y']]\n",
407 | "js_text = js_text_template_2.substitute({'data': json.dumps(data.to_dict(orient='records'))})\n",
408 | "HTML('')"
409 | ]
410 | }
411 | ],
412 | "metadata": {
413 | "anaconda-cloud": {},
414 | "kernelspec": {
415 | "display_name": "Python [default]",
416 | "language": "python",
417 | "name": "python2"
418 | },
419 | "language_info": {
420 | "codemirror_mode": {
421 | "name": "ipython",
422 | "version": 2
423 | },
424 | "file_extension": ".py",
425 | "mimetype": "text/x-python",
426 | "name": "python",
427 | "nbconvert_exporter": "python",
428 | "pygments_lexer": "ipython2",
429 | "version": "2.7.13"
430 | },
431 | "toc": {
432 | "nav_menu": {},
433 | "number_sections": true,
434 | "sideBar": true,
435 | "skip_h1_title": false,
436 | "toc_cell": false,
437 | "toc_position": {},
438 | "toc_section_display": "block",
439 | "toc_window_display": false
440 | }
441 | },
442 | "nbformat": 4,
443 | "nbformat_minor": 2
444 | }
445 |
--------------------------------------------------------------------------------
/SCATTER PLOT.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# IRIS DATA: SIMPLE SCATTER PLOT USING D3 IN JUYPTER NOTEBOOK"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "## Introduction"
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "This code is based on the blog explanation. The blog explains the various steps involved. Blog Link :\n",
22 | "\n",
23 | "\n",
24 | "Now let us use the famous Iris data set to represent a scatter plot.\n",
25 | "We will use the bl.ocks template from here ,to construct and customize the scatterplot in order to learn more about it .The link to the data set is here\n",
26 | "(https://gist.githubusercontent.com/mbostock/3887118/raw/2e68ffbeb23fe4dadd9b0f6bca62e9def6ee9e17/data.tsv) and it is in the tsv format.\n",
27 | "\n",
28 | "Please refer to the blog for the explanation of this note book ."
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 55,
34 | "metadata": {
35 | "collapsed": true
36 | },
37 | "outputs": [],
38 | "source": [
39 | "# load the Preliminaries\n",
40 | "from IPython.core.display import display, HTML\n",
41 | "from string import Template\n",
42 | "import pandas as pd\n",
43 | "import json, random"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": 56,
49 | "metadata": {},
50 | "outputs": [
51 | {
52 | "data": {
53 | "text/html": [
54 | ""
55 | ],
56 | "text/plain": [
57 | ""
58 | ]
59 | },
60 | "execution_count": 56,
61 | "metadata": {},
62 | "output_type": "execute_result"
63 | }
64 | ],
65 | "source": [
66 | "# Get the D3 host locally. \n",
67 | "HTML('')"
68 | ]
69 | },
70 | {
71 | "cell_type": "code",
72 | "execution_count": 47,
73 | "metadata": {},
74 | "outputs": [
75 | {
76 | "data": {
77 | "text/html": [
78 | "\n",
79 | "\n",
92 | "
\n",
93 | " \n",
94 | " \n",
95 | " | \n",
96 | " sepalLength | \n",
97 | " sepalWidth | \n",
98 | " petalLength | \n",
99 | " petalWidth | \n",
100 | " species | \n",
101 | "
\n",
102 | " \n",
103 | " \n",
104 | " \n",
105 | " | 0 | \n",
106 | " 5.1 | \n",
107 | " 3.5 | \n",
108 | " 1.4 | \n",
109 | " 0.2 | \n",
110 | " setosa | \n",
111 | "
\n",
112 | " \n",
113 | " | 1 | \n",
114 | " 4.9 | \n",
115 | " 3.0 | \n",
116 | " 1.4 | \n",
117 | " 0.2 | \n",
118 | " setosa | \n",
119 | "
\n",
120 | " \n",
121 | " | 2 | \n",
122 | " 4.7 | \n",
123 | " 3.2 | \n",
124 | " 1.3 | \n",
125 | " 0.2 | \n",
126 | " setosa | \n",
127 | "
\n",
128 | " \n",
129 | " | 3 | \n",
130 | " 4.6 | \n",
131 | " 3.1 | \n",
132 | " 1.5 | \n",
133 | " 0.2 | \n",
134 | " setosa | \n",
135 | "
\n",
136 | " \n",
137 | " | 4 | \n",
138 | " 5.0 | \n",
139 | " 3.6 | \n",
140 | " 1.4 | \n",
141 | " 0.2 | \n",
142 | " setosa | \n",
143 | "
\n",
144 | " \n",
145 | "
\n",
146 | "
"
147 | ],
148 | "text/plain": [
149 | " sepalLength sepalWidth petalLength petalWidth species\n",
150 | "0 5.1 3.5 1.4 0.2 setosa\n",
151 | "1 4.9 3.0 1.4 0.2 setosa\n",
152 | "2 4.7 3.2 1.3 0.2 setosa\n",
153 | "3 4.6 3.1 1.5 0.2 setosa\n",
154 | "4 5.0 3.6 1.4 0.2 setosa"
155 | ]
156 | },
157 | "execution_count": 47,
158 | "metadata": {},
159 | "output_type": "execute_result"
160 | }
161 | ],
162 | "source": [
163 | "# get the data from the link and parse it to convert it into a datafram-it's in tsv format\n",
164 | "filename = 'https://gist.githubusercontent.com/mbostock/3887118/raw/2e68ffbeb23fe4dadd9b0f6bca62e9def6ee9e17/data.tsv'\n",
165 | "iris = pd.read_csv(filename,sep=\"\\t\")\n",
166 | "iris.head()"
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": 48,
172 | "metadata": {
173 | "collapsed": true
174 | },
175 | "outputs": [],
176 | "source": [
177 | "# D3 accepts dictionaries , so let's convert data frame into dictionaries."
178 | ]
179 | },
180 | {
181 | "cell_type": "code",
182 | "execution_count": 49,
183 | "metadata": {},
184 | "outputs": [
185 | {
186 | "data": {
187 | "text/plain": [
188 | "[{'petalLength': 1.4,\n",
189 | " 'petalWidth': 0.2,\n",
190 | " 'sepalLength': 5.1,\n",
191 | " 'sepalWidth': 3.5,\n",
192 | " 'species': 'setosa'},\n",
193 | " {'petalLength': 1.4,\n",
194 | " 'petalWidth': 0.2,\n",
195 | " 'sepalLength': 4.9,\n",
196 | " 'sepalWidth': 3.0,\n",
197 | " 'species': 'setosa'},\n",
198 | " {'petalLength': 1.3,\n",
199 | " 'petalWidth': 0.2,\n",
200 | " 'sepalLength': 4.7,\n",
201 | " 'sepalWidth': 3.2,\n",
202 | " 'species': 'setosa'},\n",
203 | " {'petalLength': 1.5,\n",
204 | " 'petalWidth': 0.2,\n",
205 | " 'sepalLength': 4.6,\n",
206 | " 'sepalWidth': 3.1,\n",
207 | " 'species': 'setosa'},\n",
208 | " {'petalLength': 1.4,\n",
209 | " 'petalWidth': 0.2,\n",
210 | " 'sepalLength': 5.0,\n",
211 | " 'sepalWidth': 3.6,\n",
212 | " 'species': 'setosa'}]"
213 | ]
214 | },
215 | "execution_count": 49,
216 | "metadata": {},
217 | "output_type": "execute_result"
218 | }
219 | ],
220 | "source": [
221 | "iris_array_of_dicts = iris.to_dict(orient='records')\n",
222 | "iris_array_of_dicts[:5]"
223 | ]
224 | },
225 | {
226 | "cell_type": "markdown",
227 | "metadata": {},
228 | "source": [
229 | "## CSS,HTML,Javascript based on bl.ocks example"
230 | ]
231 | },
232 | {
233 | "cell_type": "markdown",
234 | "metadata": {},
235 | "source": [
236 | "Note that in the below css_text, we have removed the 'body' style reference from the original bl.ocks text. This is to avoid this style changing the rest of the notebook.`m\n",
237 | "\n",
238 | "You can get bl.ocks from here : https://bl.ocks.org/mbostock/3887118"
239 | ]
240 | },
241 | {
242 | "cell_type": "code",
243 | "execution_count": 51,
244 | "metadata": {
245 | "collapsed": true
246 | },
247 | "outputs": [],
248 | "source": [
249 | "css_text = '''\n",
250 | ".axis path,\n",
251 | ".axis line {\n",
252 | " fill: none;\n",
253 | " stroke: #000;\n",
254 | " shape-rendering: crispEdges;\n",
255 | "}\n",
256 | "\n",
257 | ".dot {\n",
258 | " stroke: #000;\n",
259 | "}\n",
260 | "'''"
261 | ]
262 | },
263 | {
264 | "cell_type": "markdown",
265 | "metadata": {},
266 | "source": [
267 | "Next, let’s copy the java script and make two changes that are :\n",
268 | "\n",
269 | "* The first set of changes is to the width and height of the image\n",
270 | "* The second change is simply to reference a different DOM element as the starting point. D3 will look for a specific DOM element to write things to. These block usually reference an external file like csv/tsv. The $ is going to be used in the templet engine to find and replace.\n",
271 | "\n",
272 | "\n",
273 | "Note : //* — is used as comments , so those lines will not be executed and below that you can see the changes that were made."
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "execution_count": 52,
279 | "metadata": {
280 | "collapsed": true
281 | },
282 | "outputs": [],
283 | "source": [
284 | "js_text_template = Template('''\n",
285 | "var margin = {top: 20, right: 20, bottom: 30, left: 40},\n",
286 | "// **** width = 960 - margin.left - margin.right, ****\n",
287 | "// **** height = 500 - margin.top - margin.bottom; ****\n",
288 | " width = 720 - margin.left - margin.right,\n",
289 | " height = 375 - margin.top - margin.bottom;\n",
290 | "\n",
291 | "var x = d3.scale.linear()\n",
292 | " .range([0, width]);\n",
293 | "\n",
294 | "var y = d3.scale.linear()\n",
295 | " .range([height, 0]);\n",
296 | "\n",
297 | "var color = d3.scale.category10();\n",
298 | "\n",
299 | "var xAxis = d3.svg.axis()\n",
300 | " .scale(x)\n",
301 | " .orient(\"bottom\");\n",
302 | "\n",
303 | "var yAxis = d3.svg.axis()\n",
304 | " .scale(y)\n",
305 | " .orient(\"left\");\n",
306 | "\n",
307 | "// **** var svg = d3.select(\"body\").append(\"svg\") ****\n",
308 | "var svg = d3.select(\"#$graphdiv\").append(\"svg\")\n",
309 | " .attr(\"width\", width + margin.left + margin.right)\n",
310 | " .attr(\"height\", height + margin.top + margin.bottom)\n",
311 | " .append(\"g\")\n",
312 | " .attr(\"transform\", \"translate(\" + margin.left + \",\" + margin.top + \")\");\n",
313 | "\n",
314 | "// **** d3.tsv(\"data.tsv\", function(error, data) { ****\n",
315 | "// **** if (error) throw error; ****\n",
316 | "\n",
317 | "var data = $python_data ;\n",
318 | "\n",
319 | " data.forEach(function(d) {\n",
320 | " d.sepalLength = +d.sepalLength;\n",
321 | " d.sepalWidth = +d.sepalWidth;\n",
322 | " });\n",
323 | "\n",
324 | " x.domain(d3.extent(data, function(d) { return d.sepalWidth; })).nice();\n",
325 | " y.domain(d3.extent(data, function(d) { return d.sepalLength; })).nice();\n",
326 | "\n",
327 | " svg.append(\"g\")\n",
328 | " .attr(\"class\", \"x axis\")\n",
329 | " .attr(\"transform\", \"translate(0,\" + height + \")\")\n",
330 | " .call(xAxis)\n",
331 | " .append(\"text\")\n",
332 | " .attr(\"class\", \"label\")\n",
333 | " .attr(\"x\", width)\n",
334 | " .attr(\"y\", -6)\n",
335 | " .style(\"text-anchor\", \"end\")\n",
336 | " .text(\"Sepal Width (cm)\");\n",
337 | "\n",
338 | " svg.append(\"g\")\n",
339 | " .attr(\"class\", \"y axis\")\n",
340 | " .call(yAxis)\n",
341 | " .append(\"text\")\n",
342 | " .attr(\"class\", \"label\")\n",
343 | " .attr(\"transform\", \"rotate(-90)\")\n",
344 | " .attr(\"y\", 6)\n",
345 | " .attr(\"dy\", \".71em\")\n",
346 | " .style(\"text-anchor\", \"end\")\n",
347 | " .text(\"Sepal Length (cm)\")\n",
348 | "\n",
349 | " svg.selectAll(\".dot\")\n",
350 | " .data(data)\n",
351 | " .enter().append(\"circle\")\n",
352 | " .attr(\"class\", \"dot\")\n",
353 | " .attr(\"r\", 3.5)\n",
354 | " .attr(\"cx\", function(d) { return x(d.sepalWidth); })\n",
355 | " .attr(\"cy\", function(d) { return y(d.sepalLength); })\n",
356 | " .style(\"fill\", function(d) { return color(d.species); });\n",
357 | "\n",
358 | " var legend = svg.selectAll(\".legend\")\n",
359 | " .data(color.domain())\n",
360 | " .enter().append(\"g\")\n",
361 | " .attr(\"class\", \"legend\")\n",
362 | " .attr(\"transform\", function(d, i) { return \"translate(0,\" + i * 20 + \")\"; });\n",
363 | "\n",
364 | " legend.append(\"rect\")\n",
365 | " .attr(\"x\", width - 18)\n",
366 | " .attr(\"width\", 18)\n",
367 | " .attr(\"height\", 18)\n",
368 | " .style(\"fill\", color);\n",
369 | "\n",
370 | " legend.append(\"text\")\n",
371 | " .attr(\"x\", width - 24)\n",
372 | " .attr(\"y\", 9)\n",
373 | " .attr(\"dy\", \".35em\")\n",
374 | " .style(\"text-anchor\", \"end\")\n",
375 | " .text(function(d) { return d; });\n",
376 | "\n",
377 | "// **** }); ****\n",
378 | "\n",
379 | "''')"
380 | ]
381 | },
382 | {
383 | "cell_type": "code",
384 | "execution_count": 53,
385 | "metadata": {
386 | "collapsed": true
387 | },
388 | "outputs": [],
389 | "source": [
390 | "# Now let’s make a template for the html string\n",
391 | "html_template = Template('''\n",
392 | "\n",
393 | "\n",
394 | "\n",
395 | "''')\n"
396 | ]
397 | },
398 | {
399 | "cell_type": "markdown",
400 | "metadata": {},
401 | "source": [
402 | "You can notice I have put css text for the style which we created earlier.Then we use div instead of a body and use that id to reference later .First we have the template engine for the java script where we give that data and create a connection for graphdive by referencing the id .Now for the next step ,add the css and the java script to the HTML template engine which will combine it to execute the output(the scatter plot)"
403 | ]
404 | },
405 | {
406 | "cell_type": "code",
407 | "execution_count": 54,
408 | "metadata": {},
409 | "outputs": [
410 | {
411 | "data": {
412 | "text/html": [
413 | "\n",
414 | "\n",
426 | "\n",
427 | "\n"
523 | ],
524 | "text/plain": [
525 | ""
526 | ]
527 | },
528 | "execution_count": 54,
529 | "metadata": {},
530 | "output_type": "execute_result"
531 | }
532 | ],
533 | "source": [
534 | "js_text = js_text_template.substitute({'python_data': json.dumps(iris_array_of_dicts),\n",
535 | " 'graphdiv': 'graph-div'})\n",
536 | "HTML(html_template.substitute({'css_text': css_text, 'js_text': js_text}))"
537 | ]
538 | }
539 | ],
540 | "metadata": {
541 | "anaconda-cloud": {},
542 | "kernelspec": {
543 | "display_name": "Python [default]",
544 | "language": "python",
545 | "name": "python2"
546 | },
547 | "language_info": {
548 | "codemirror_mode": {
549 | "name": "ipython",
550 | "version": 2
551 | },
552 | "file_extension": ".py",
553 | "mimetype": "text/x-python",
554 | "name": "python",
555 | "nbconvert_exporter": "python",
556 | "pygments_lexer": "ipython2",
557 | "version": "2.7.13"
558 | },
559 | "toc": {
560 | "nav_menu": {},
561 | "number_sections": true,
562 | "sideBar": true,
563 | "skip_h1_title": false,
564 | "toc_cell": false,
565 | "toc_position": {},
566 | "toc_section_display": "block",
567 | "toc_window_display": false
568 | }
569 | },
570 | "nbformat": 4,
571 | "nbformat_minor": 2
572 | }
573 |
--------------------------------------------------------------------------------
/.ipynb_checkpoints/SCATTER PLOT-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# IRIS DATA: SIMPLE SCATTER PLOT USING D3 IN JUYPTER NOTEBOOK"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "## Introduction"
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "This code is based on the blog explanation. The blog explains the various steps involved. Blog Link :\n",
22 | "\n",
23 | "\n",
24 | "Now let us use the famous Iris data set to represent a scatter plot.\n",
25 | "We will use the bl.ocks template from here ,to construct and customize the scatterplot in order to learn more about it .The link to the data set is here\n",
26 | "(https://gist.githubusercontent.com/mbostock/3887118/raw/2e68ffbeb23fe4dadd9b0f6bca62e9def6ee9e17/data.tsv) and it is in the tsv format.\n",
27 | "\n",
28 | "Please refer to the blog for the explanation of this note book ."
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 55,
34 | "metadata": {
35 | "collapsed": true
36 | },
37 | "outputs": [],
38 | "source": [
39 | "# load the Preliminaries\n",
40 | "from IPython.core.display import display, HTML\n",
41 | "from string import Template\n",
42 | "import pandas as pd\n",
43 | "import json, random"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": 56,
49 | "metadata": {},
50 | "outputs": [
51 | {
52 | "data": {
53 | "text/html": [
54 | ""
55 | ],
56 | "text/plain": [
57 | ""
58 | ]
59 | },
60 | "execution_count": 56,
61 | "metadata": {},
62 | "output_type": "execute_result"
63 | }
64 | ],
65 | "source": [
66 | "# Get the D3 host locally. \n",
67 | "HTML('')"
68 | ]
69 | },
70 | {
71 | "cell_type": "code",
72 | "execution_count": 47,
73 | "metadata": {},
74 | "outputs": [
75 | {
76 | "data": {
77 | "text/html": [
78 | "\n",
79 | "\n",
92 | "
\n",
93 | " \n",
94 | " \n",
95 | " | \n",
96 | " sepalLength | \n",
97 | " sepalWidth | \n",
98 | " petalLength | \n",
99 | " petalWidth | \n",
100 | " species | \n",
101 | "
\n",
102 | " \n",
103 | " \n",
104 | " \n",
105 | " | 0 | \n",
106 | " 5.1 | \n",
107 | " 3.5 | \n",
108 | " 1.4 | \n",
109 | " 0.2 | \n",
110 | " setosa | \n",
111 | "
\n",
112 | " \n",
113 | " | 1 | \n",
114 | " 4.9 | \n",
115 | " 3.0 | \n",
116 | " 1.4 | \n",
117 | " 0.2 | \n",
118 | " setosa | \n",
119 | "
\n",
120 | " \n",
121 | " | 2 | \n",
122 | " 4.7 | \n",
123 | " 3.2 | \n",
124 | " 1.3 | \n",
125 | " 0.2 | \n",
126 | " setosa | \n",
127 | "
\n",
128 | " \n",
129 | " | 3 | \n",
130 | " 4.6 | \n",
131 | " 3.1 | \n",
132 | " 1.5 | \n",
133 | " 0.2 | \n",
134 | " setosa | \n",
135 | "
\n",
136 | " \n",
137 | " | 4 | \n",
138 | " 5.0 | \n",
139 | " 3.6 | \n",
140 | " 1.4 | \n",
141 | " 0.2 | \n",
142 | " setosa | \n",
143 | "
\n",
144 | " \n",
145 | "
\n",
146 | "
"
147 | ],
148 | "text/plain": [
149 | " sepalLength sepalWidth petalLength petalWidth species\n",
150 | "0 5.1 3.5 1.4 0.2 setosa\n",
151 | "1 4.9 3.0 1.4 0.2 setosa\n",
152 | "2 4.7 3.2 1.3 0.2 setosa\n",
153 | "3 4.6 3.1 1.5 0.2 setosa\n",
154 | "4 5.0 3.6 1.4 0.2 setosa"
155 | ]
156 | },
157 | "execution_count": 47,
158 | "metadata": {},
159 | "output_type": "execute_result"
160 | }
161 | ],
162 | "source": [
163 | "# get the data from the link and parse it to convert it into a datafram-it's in tsv format\n",
164 | "filename = 'https://gist.githubusercontent.com/mbostock/3887118/raw/2e68ffbeb23fe4dadd9b0f6bca62e9def6ee9e17/data.tsv'\n",
165 | "iris = pd.read_csv(filename,sep=\"\\t\")\n",
166 | "iris.head()"
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": 48,
172 | "metadata": {
173 | "collapsed": true
174 | },
175 | "outputs": [],
176 | "source": [
177 | "# D3 accepts dictionaries , so let's convert data frame into dictionaries."
178 | ]
179 | },
180 | {
181 | "cell_type": "code",
182 | "execution_count": 49,
183 | "metadata": {},
184 | "outputs": [
185 | {
186 | "data": {
187 | "text/plain": [
188 | "[{'petalLength': 1.4,\n",
189 | " 'petalWidth': 0.2,\n",
190 | " 'sepalLength': 5.1,\n",
191 | " 'sepalWidth': 3.5,\n",
192 | " 'species': 'setosa'},\n",
193 | " {'petalLength': 1.4,\n",
194 | " 'petalWidth': 0.2,\n",
195 | " 'sepalLength': 4.9,\n",
196 | " 'sepalWidth': 3.0,\n",
197 | " 'species': 'setosa'},\n",
198 | " {'petalLength': 1.3,\n",
199 | " 'petalWidth': 0.2,\n",
200 | " 'sepalLength': 4.7,\n",
201 | " 'sepalWidth': 3.2,\n",
202 | " 'species': 'setosa'},\n",
203 | " {'petalLength': 1.5,\n",
204 | " 'petalWidth': 0.2,\n",
205 | " 'sepalLength': 4.6,\n",
206 | " 'sepalWidth': 3.1,\n",
207 | " 'species': 'setosa'},\n",
208 | " {'petalLength': 1.4,\n",
209 | " 'petalWidth': 0.2,\n",
210 | " 'sepalLength': 5.0,\n",
211 | " 'sepalWidth': 3.6,\n",
212 | " 'species': 'setosa'}]"
213 | ]
214 | },
215 | "execution_count": 49,
216 | "metadata": {},
217 | "output_type": "execute_result"
218 | }
219 | ],
220 | "source": [
221 | "iris_array_of_dicts = iris.to_dict(orient='records')\n",
222 | "iris_array_of_dicts[:5]"
223 | ]
224 | },
225 | {
226 | "cell_type": "markdown",
227 | "metadata": {},
228 | "source": [
229 | "## CSS,HTML,Javascript based on bl.ocks example"
230 | ]
231 | },
232 | {
233 | "cell_type": "markdown",
234 | "metadata": {},
235 | "source": [
236 | "Note that in the below css_text, we have removed the 'body' style reference from the original bl.ocks text. This is to avoid this style changing the rest of the notebook.`m\n",
237 | "\n",
238 | "You can get bl.ocks from here : https://bl.ocks.org/mbostock/3887118"
239 | ]
240 | },
241 | {
242 | "cell_type": "code",
243 | "execution_count": 51,
244 | "metadata": {
245 | "collapsed": true
246 | },
247 | "outputs": [],
248 | "source": [
249 | "css_text = '''\n",
250 | ".axis path,\n",
251 | ".axis line {\n",
252 | " fill: none;\n",
253 | " stroke: #000;\n",
254 | " shape-rendering: crispEdges;\n",
255 | "}\n",
256 | "\n",
257 | ".dot {\n",
258 | " stroke: #000;\n",
259 | "}\n",
260 | "'''"
261 | ]
262 | },
263 | {
264 | "cell_type": "markdown",
265 | "metadata": {},
266 | "source": [
267 | "Next, let’s copy the java script and make two changes that are :\n",
268 | "\n",
269 | "* The first set of changes is to the width and height of the image\n",
270 | "* The second change is simply to reference a different DOM element as the starting point. D3 will look for a specific DOM element to write things to. These block usually reference an external file like csv/tsv. The $ is going to be used in the templet engine to find and replace.\n",
271 | "\n",
272 | "\n",
273 | "Note : //* — is used as comments , so those lines will not be executed and below that you can see the changes that were made."
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "execution_count": 52,
279 | "metadata": {
280 | "collapsed": true
281 | },
282 | "outputs": [],
283 | "source": [
284 | "js_text_template = Template('''\n",
285 | "var margin = {top: 20, right: 20, bottom: 30, left: 40},\n",
286 | "// **** width = 960 - margin.left - margin.right, ****\n",
287 | "// **** height = 500 - margin.top - margin.bottom; ****\n",
288 | " width = 720 - margin.left - margin.right,\n",
289 | " height = 375 - margin.top - margin.bottom;\n",
290 | "\n",
291 | "var x = d3.scale.linear()\n",
292 | " .range([0, width]);\n",
293 | "\n",
294 | "var y = d3.scale.linear()\n",
295 | " .range([height, 0]);\n",
296 | "\n",
297 | "var color = d3.scale.category10();\n",
298 | "\n",
299 | "var xAxis = d3.svg.axis()\n",
300 | " .scale(x)\n",
301 | " .orient(\"bottom\");\n",
302 | "\n",
303 | "var yAxis = d3.svg.axis()\n",
304 | " .scale(y)\n",
305 | " .orient(\"left\");\n",
306 | "\n",
307 | "// **** var svg = d3.select(\"body\").append(\"svg\") ****\n",
308 | "var svg = d3.select(\"#$graphdiv\").append(\"svg\")\n",
309 | " .attr(\"width\", width + margin.left + margin.right)\n",
310 | " .attr(\"height\", height + margin.top + margin.bottom)\n",
311 | " .append(\"g\")\n",
312 | " .attr(\"transform\", \"translate(\" + margin.left + \",\" + margin.top + \")\");\n",
313 | "\n",
314 | "// **** d3.tsv(\"data.tsv\", function(error, data) { ****\n",
315 | "// **** if (error) throw error; ****\n",
316 | "\n",
317 | "var data = $python_data ;\n",
318 | "\n",
319 | " data.forEach(function(d) {\n",
320 | " d.sepalLength = +d.sepalLength;\n",
321 | " d.sepalWidth = +d.sepalWidth;\n",
322 | " });\n",
323 | "\n",
324 | " x.domain(d3.extent(data, function(d) { return d.sepalWidth; })).nice();\n",
325 | " y.domain(d3.extent(data, function(d) { return d.sepalLength; })).nice();\n",
326 | "\n",
327 | " svg.append(\"g\")\n",
328 | " .attr(\"class\", \"x axis\")\n",
329 | " .attr(\"transform\", \"translate(0,\" + height + \")\")\n",
330 | " .call(xAxis)\n",
331 | " .append(\"text\")\n",
332 | " .attr(\"class\", \"label\")\n",
333 | " .attr(\"x\", width)\n",
334 | " .attr(\"y\", -6)\n",
335 | " .style(\"text-anchor\", \"end\")\n",
336 | " .text(\"Sepal Width (cm)\");\n",
337 | "\n",
338 | " svg.append(\"g\")\n",
339 | " .attr(\"class\", \"y axis\")\n",
340 | " .call(yAxis)\n",
341 | " .append(\"text\")\n",
342 | " .attr(\"class\", \"label\")\n",
343 | " .attr(\"transform\", \"rotate(-90)\")\n",
344 | " .attr(\"y\", 6)\n",
345 | " .attr(\"dy\", \".71em\")\n",
346 | " .style(\"text-anchor\", \"end\")\n",
347 | " .text(\"Sepal Length (cm)\")\n",
348 | "\n",
349 | " svg.selectAll(\".dot\")\n",
350 | " .data(data)\n",
351 | " .enter().append(\"circle\")\n",
352 | " .attr(\"class\", \"dot\")\n",
353 | " .attr(\"r\", 3.5)\n",
354 | " .attr(\"cx\", function(d) { return x(d.sepalWidth); })\n",
355 | " .attr(\"cy\", function(d) { return y(d.sepalLength); })\n",
356 | " .style(\"fill\", function(d) { return color(d.species); });\n",
357 | "\n",
358 | " var legend = svg.selectAll(\".legend\")\n",
359 | " .data(color.domain())\n",
360 | " .enter().append(\"g\")\n",
361 | " .attr(\"class\", \"legend\")\n",
362 | " .attr(\"transform\", function(d, i) { return \"translate(0,\" + i * 20 + \")\"; });\n",
363 | "\n",
364 | " legend.append(\"rect\")\n",
365 | " .attr(\"x\", width - 18)\n",
366 | " .attr(\"width\", 18)\n",
367 | " .attr(\"height\", 18)\n",
368 | " .style(\"fill\", color);\n",
369 | "\n",
370 | " legend.append(\"text\")\n",
371 | " .attr(\"x\", width - 24)\n",
372 | " .attr(\"y\", 9)\n",
373 | " .attr(\"dy\", \".35em\")\n",
374 | " .style(\"text-anchor\", \"end\")\n",
375 | " .text(function(d) { return d; });\n",
376 | "\n",
377 | "// **** }); ****\n",
378 | "\n",
379 | "''')"
380 | ]
381 | },
382 | {
383 | "cell_type": "code",
384 | "execution_count": 53,
385 | "metadata": {
386 | "collapsed": true
387 | },
388 | "outputs": [],
389 | "source": [
390 | "# Now let’s make a template for the html string\n",
391 | "html_template = Template('''\n",
392 | "\n",
393 | "\n",
394 | "\n",
395 | "''')\n"
396 | ]
397 | },
398 | {
399 | "cell_type": "markdown",
400 | "metadata": {},
401 | "source": [
402 | "You can notice I have put css text for the style which we created earlier.Then we use div instead of a body and use that id to reference later .First we have the template engine for the java script where we give that data and create a connection for graphdive by referencing the id .Now for the next step ,add the css and the java script to the HTML template engine which will combine it to execute the output(the scatter plot)"
403 | ]
404 | },
405 | {
406 | "cell_type": "code",
407 | "execution_count": 54,
408 | "metadata": {},
409 | "outputs": [
410 | {
411 | "data": {
412 | "text/html": [
413 | "\n",
414 | "\n",
426 | "\n",
427 | "\n"
523 | ],
524 | "text/plain": [
525 | ""
526 | ]
527 | },
528 | "execution_count": 54,
529 | "metadata": {},
530 | "output_type": "execute_result"
531 | }
532 | ],
533 | "source": [
534 | "js_text = js_text_template.substitute({'python_data': json.dumps(iris_array_of_dicts),\n",
535 | " 'graphdiv': 'graph-div'})\n",
536 | "HTML(html_template.substitute({'css_text': css_text, 'js_text': js_text}))"
537 | ]
538 | }
539 | ],
540 | "metadata": {
541 | "anaconda-cloud": {},
542 | "kernelspec": {
543 | "display_name": "Python [default]",
544 | "language": "python",
545 | "name": "python2"
546 | },
547 | "language_info": {
548 | "codemirror_mode": {
549 | "name": "ipython",
550 | "version": 2
551 | },
552 | "file_extension": ".py",
553 | "mimetype": "text/x-python",
554 | "name": "python",
555 | "nbconvert_exporter": "python",
556 | "pygments_lexer": "ipython2",
557 | "version": "2.7.13"
558 | },
559 | "toc": {
560 | "nav_menu": {},
561 | "number_sections": true,
562 | "sideBar": true,
563 | "skip_h1_title": false,
564 | "toc_cell": false,
565 | "toc_position": {},
566 | "toc_section_display": "block",
567 | "toc_window_display": false
568 | }
569 | },
570 | "nbformat": 4,
571 | "nbformat_minor": 2
572 | }
573 |
--------------------------------------------------------------------------------