Debian Codegraph @

bqplot/examples/Marks/Object Model/ScatterMega.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "import bqplot\n",
10 "import numpy as np\n",
11 "from ipywidgets import link, FloatSlider"
12 ]
13 },
14 {
15 "cell_type": "code",
16 "execution_count": null,
17 "metadata": {
18 "scrolled": false
19 },
20 "outputs": [],
21 "source": [
22 "N = 3 * int(1e5)\n",
23 "scale_x = bqplot.LinearScale(min=-5, max=5)\n",
24 "scale_y = bqplot.LinearScale(min=-5, max=5)\n",
25 "scale_size = bqplot.LinearScale(min=0, max=1)\n",
26 "\n",
27 "x = np.random.normal(5, 4, N)\n",
28 "y = np.random.normal(0, 3, N)\n",
29 "\n",
30 "size = np.random.rand(N) * 30 + 1\n",
31 "\n",
32 "scatter = bqplot.ScatterGL(x=x, y=y,\n",
33 " size=size,\n",
34 " colors=['red', 'green', 'yellow'],\n",
35 " scales={\n",
36 " 'x': scale_x, 'y': scale_y, \n",
37 " 'size': scale_size\n",
38 " },\n",
39 " default_size=20,\n",
40 " marker='circle'\n",
41 " )\n",
42 "\n",
43 "ax_x = bqplot.Axis(scale=scale_x, grid_lines='solid', label='X')\n",
44 "ax_y = bqplot.Axis(scale=scale_y, orientation='vertical', tick_format='0.2f', grid_lines='solid', label='Y')\n",
45 "\n",
46 "fig = bqplot.Figure(marks=[scatter], axes=[ax_x, ax_y], title='Scatter GL', legend_location='bottom-right', animation_duration=2000)\n",
47 "\n",
48 "scatter.stroke = 'black'\n",
49 "\n",
50 "fig"
51 ]
52 },
53 {
54 "cell_type": "code",
55 "execution_count": null,
56 "metadata": {},
57 "outputs": [],
58 "source": [
59 "N = int(1e4)\n",
60 "with scatter.hold_sync():\n",
61 " scatter.x = np.random.normal(5, 4, N)\n",
62 " scatter.y = np.random.normal(0, 3, N)"
63 ]
64 },
65 {
66 "cell_type": "code",
67 "execution_count": null,
68 "metadata": {},
69 "outputs": [],
70 "source": [
71 "scatter.colors = ['blue']"
72 ]
73 },
74 {
75 "cell_type": "code",
76 "execution_count": null,
77 "metadata": {},
78 "outputs": [],
79 "source": [
80 "scatter.colors = ['blue', 'orange']"
81 ]
82 },
83 {
84 "cell_type": "code",
85 "execution_count": null,
86 "metadata": {},
87 "outputs": [],
88 "source": [
89 "scatter.marker = 'cross'"
90 ]
91 },
92 {
93 "cell_type": "code",
94 "execution_count": null,
95 "metadata": {},
96 "outputs": [],
97 "source": [
98 "scatter.marker = 'triangle-up'"
99 ]
100 },
101 {
102 "cell_type": "code",
103 "execution_count": null,
104 "metadata": {},
105 "outputs": [],
106 "source": [
107 "scatter.marker = 'triangle-down'"
108 ]
109 },
110 {
111 "cell_type": "code",
112 "execution_count": null,
113 "metadata": {},
114 "outputs": [],
115 "source": [
116 "scatter.marker = 'square'"
117 ]
118 },
119 {
120 "cell_type": "code",
121 "execution_count": null,
122 "metadata": {},
123 "outputs": [],
124 "source": [
125 "N = 3 * int(1e4)\n",
126 "scale_x = bqplot.LinearScale(min=-5, max=5)\n",
127 "scale_y = bqplot.LinearScale(min=-5, max=5)\n",
128 "scale_size = bqplot.LinearScale(min=0, max=1)\n",
129 "scale_color = bqplot.ColorScale(min=0, max=1, scheme='RdYlGn')\n",
130 "\n",
131 "x = np.random.normal(5, 4, N)\n",
132 "y = np.random.normal(0, 3, N)\n",
133 "\n",
134 "size = np.random.rand(N) * 30 + 1\n",
135 "color = np.random.rand(N)\n",
136 "\n",
137 "scatter = bqplot.ScatterGL(x=x, y=y,\n",
138 " size=size,\n",
139 " color=color,\n",
140 " scales={\n",
141 " 'x': scale_x, 'y': scale_y, \n",
142 " 'size': scale_size, \n",
143 " 'color': scale_color\n",
144 " },\n",
145 " default_size=20,\n",
146 " marker='circle'\n",
147 " )\n",
148 "scatter.selected_style = {'fill': 'blue'}\n",
149 "\n",
150 "ax_x = bqplot.Axis(scale=scale_x, grid_lines='solid', label='X')\n",
151 "ax_y = bqplot.Axis(scale=scale_y, orientation='vertical', tick_format='0.2f', grid_lines='solid', label='Y')\n",
152 "\n",
153 "fig = bqplot.Figure(marks=[scatter], axes=[ax_x, ax_y], title='Scatter GL', legend_location='bottom-right', animation_duration=2000)\n",
154 "\n",
155 "panzoom = bqplot.interacts.PanZoom(scales={'x': [scale_x], 'y': [scale_y]})\n",
156 "fig.interaction = panzoom\n",
157 "\n",
158 "scatter.stroke = 'black'\n",
159 "\n",
160 "fig"
161 ]
162 },
163 {
164 "cell_type": "code",
165 "execution_count": null,
166 "metadata": {},
167 "outputs": [],
168 "source": [
169 "with scatter.hold_sync():\n",
170 " scatter.x = np.random.normal(5, 4, N)\n",
171 " scatter.y = np.random.normal(0, 3, N)\n",
172 " scatter.size = np.random.normal(20, 20, N)"
173 ]
174 },
175 {
176 "cell_type": "code",
177 "execution_count": null,
178 "metadata": {},
179 "outputs": [],
180 "source": [
181 "scatter.y = np.random.normal(0, 3, N)"
182 ]
183 },
184 {
185 "cell_type": "code",
186 "execution_count": null,
187 "metadata": {},
188 "outputs": [],
189 "source": [
190 "scatter.size = np.random.rand(N) * 50 + 1"
191 ]
192 },
193 {
194 "cell_type": "code",
195 "execution_count": null,
196 "metadata": {},
197 "outputs": [],
198 "source": [
199 "from ipywidgets import FloatSlider, link\n",
200 "\n",
201 "slider = FloatSlider(min=0.1, max=10., value=scale_color.max)\n",
202 "link((slider, 'value'), (scale_color, 'max'))\n",
203 "slider"
204 ]
205 },
206 {
207 "cell_type": "code",
208 "execution_count": null,
209 "metadata": {},
210 "outputs": [],
211 "source": [
212 "scatter.stroke = None"
213 ]
214 },
215 {
216 "cell_type": "code",
217 "execution_count": null,
218 "metadata": {},
219 "outputs": [],
220 "source": [
221 "scatter.stroke_width = 5"
222 ]
223 },
224 {
225 "cell_type": "code",
226 "execution_count": null,
227 "metadata": {},
228 "outputs": [],
229 "source": [
230 "scatter.x = np.random.normal(5, 4, N)"
231 ]
232 },
233 {
234 "cell_type": "markdown",
235 "metadata": {},
236 "source": [
237 "# Features"
238 ]
239 },
240 {
241 "cell_type": "code",
242 "execution_count": null,
243 "metadata": {
244 "input_collapsed": false
245 },
246 "outputs": [],
247 "source": [
248 "import numpy as np\n",
249 "import pandas as pd\n",
250 "from bqplot import (\n",
251 " Axis, ColorAxis, LinearScale, DateScale, DateColorScale, OrdinalScale,\n",
252 " OrdinalColorScale, ColorScale, ScatterGL, Lines, Figure, Tooltip, PanZoom\n",
253 ")\n",
254 "from ipywidgets import Label"
255 ]
256 },
257 {
258 "cell_type": "code",
259 "execution_count": null,
260 "metadata": {},
261 "outputs": [],
262 "source": [
263 "size = 100\n",
264 "price_data = pd.DataFrame(np.cumsum(np.random.randn(150, 2).dot([[1.0, -0.8], [-0.8, 1.0]]), axis=0) + size,\n",
265 " columns=['Security 1', 'Security 2'], index=pd.date_range(start='01-01-2007', periods=150))\n",
266 "\n",
267 "np.random.seed(0)\n",
268 "x_data = range(size)\n",
269 "y_data = np.cumsum(np.random.randn(size) * 100.0)\n",
270 "ord_keys = np.array(['A', 'B', 'C', 'D', 'E', 'F'])\n",
271 "ordinal_data = np.random.randint(5, size=size)"
272 ]
273 },
274 {
275 "cell_type": "code",
276 "execution_count": null,
277 "metadata": {
278 "input_collapsed": false
279 },
280 "outputs": [],
281 "source": [
282 "symbols = ['Security 1', 'Security 2']\n",
283 "\n",
284 "dates_all = price_data.index.values\n",
285 "dates_all_t = dates_all[1:]\n",
286 "sec1_levels = np.array(price_data[symbols[0]].values.flatten())\n",
287 "log_sec1 = np.log(sec1_levels)\n",
288 "sec1_returns = log_sec1\n",
289 "\n",
290 "sec2_levels = np.array(price_data[symbols[1]].values.flatten())"
291 ]
292 },
293 {
294 "cell_type": "markdown",
295 "metadata": {},
296 "source": [
297 "# Basic Scatter"
298 ]
299 },
300 {
301 "cell_type": "code",
302 "execution_count": null,
303 "metadata": {},
304 "outputs": [],
305 "source": [
306 "import bqplot\n",
307 "\n",
308 "sc_x = DateScale()\n",
309 "sc_y = LinearScale()\n",
310 "\n",
311 "scatt = ScatterGL(x=dates_all, y=sec2_levels, marker='square', stroke='black', scales={'x': sc_x, 'y': sc_y})\n",
312 "ax_x = Axis(scale=sc_x, label='Date')\n",
313 "ax_y = Axis(scale=sc_y, orientation='vertical', tick_format='0.0f', label='Security 2')\n",
314 "\n",
315 "fig = Figure(marks=[scatt], axes=[ax_x, ax_y], animation_duration=5000)\n",
316 "\n",
317 "panzoom = bqplot.interacts.PanZoom(scales={'x': [sc_x], 'y': [sc_y]})\n",
318 "fig.interaction = panzoom\n",
319 "\n",
320 "fig"
321 ]
322 },
323 {
324 "cell_type": "markdown",
325 "metadata": {},
326 "source": [
327 "## Changing the marker to each point of the scatter"
328 ]
329 },
330 {
331 "cell_type": "code",
332 "execution_count": null,
333 "metadata": {},
334 "outputs": [],
335 "source": [
336 "# Changing the marker as \n",
337 "sc_x = LinearScale()\n",
338 "sc_y = LinearScale()\n",
339 "\n",
340 "scatt = ScatterGL(x=x_data, y=y_data, names=np.arange(10),\n",
341 " scales={'x': sc_x, 'y': sc_y}, colors=['red'], marker='cross')\n",
342 "ax_x = Axis(scale=sc_x)\n",
343 "ax_y = Axis(scale=sc_y, orientation='vertical', tick_format='0.2f')\n",
344 "\n",
345 "Figure(marks=[scatt], axes=[ax_x, ax_y], padding_x=0.025)"
346 ]
347 },
348 {
349 "cell_type": "code",
350 "execution_count": null,
351 "metadata": {},
352 "outputs": [],
353 "source": [
354 "scatt.marker = 'square'"
355 ]
356 },
357 {
358 "cell_type": "markdown",
359 "metadata": {},
360 "source": [
361 "## Changing the opacity of each marker"
362 ]
363 },
364 {
365 "cell_type": "code",
366 "execution_count": null,
367 "metadata": {},
368 "outputs": [],
369 "source": [
370 "scatt.opacities = [0.3, 0.5, 1.]"
371 ]
372 },
373 {
374 "cell_type": "markdown",
375 "metadata": {},
376 "source": [
377 "# Representing additional dimensions of data"
378 ]
379 },
380 {
381 "cell_type": "markdown",
382 "metadata": {
383 "input_collapsed": false
384 },
385 "source": [
386 "## Linear Scale for Color Data"
387 ]
388 },
389 {
390 "cell_type": "code",
391 "execution_count": null,
392 "metadata": {},
393 "outputs": [],
394 "source": [
395 "sc_x = DateScale()\n",
396 "sc_y = LinearScale()\n",
397 "\n",
398 "sc_c1 = ColorScale()\n",
399 "scatter = ScatterGL(x=dates_all, y=sec2_levels, color=sec1_returns,\n",
400 " scales={'x': sc_x, 'y': sc_y, 'color': sc_c1}, \n",
401 " stroke='black')\n",
402 "\n",
403 "ax_y = Axis(label='Security 2', scale=sc_y, \n",
404 " orientation='vertical', side='left')\n",
405 "\n",
406 "ax_x = Axis(label='Date', scale=sc_x, num_ticks=10, label_location='end')\n",
407 "ax_c = ColorAxis(scale=sc_c1, tick_format='0.2%', label='Returns', orientation='vertical', side='right')\n",
408 "\n",
409 "m_chart = dict(top=50, bottom=70, left=50, right=100)\n",
410 "\n",
411 "Figure(axes=[ax_x, ax_c, ax_y], marks=[scatter], fig_margin=m_chart,\n",
412 " title='Scatter of Security 2 vs Dates')"
413 ]
414 },
415 {
416 "cell_type": "code",
417 "execution_count": null,
418 "metadata": {},
419 "outputs": [],
420 "source": [
421 "## setting the fill to be empty\n",
422 "scatter.stroke = None\n",
423 "scatter.fill = False"
424 ]
425 },
426 {
427 "cell_type": "code",
428 "execution_count": null,
429 "metadata": {},
430 "outputs": [],
431 "source": [
432 "## Setting the fill back\n",
433 "scatter.stroke = 'black'\n",
434 "scatter.fill = True"
435 ]
436 },
437 {
438 "cell_type": "code",
439 "execution_count": null,
440 "metadata": {},
441 "outputs": [],
442 "source": [
443 "## Changing the color to a different variable\n",
444 "scatter.color = sec2_levels\n",
445 "ax_c.tick_format = '0.0f'\n",
446 "ax_c.label = 'Security 2'"
447 ]
448 },
449 {
450 "cell_type": "code",
451 "execution_count": null,
452 "metadata": {},
453 "outputs": [],
454 "source": [
455 "## Changing the range of the color scale\n",
456 "sc_c1.colors = ['blue', 'green', 'orange']"
457 ]
458 },
459 {
460 "cell_type": "markdown",
461 "metadata": {},
462 "source": [
463 "## Date Scale for Color Data"
464 ]
465 },
466 {
467 "cell_type": "code",
468 "execution_count": null,
469 "metadata": {},
470 "outputs": [],
471 "source": [
472 "sc_x = LinearScale()\n",
473 "sc_y = LinearScale()\n",
474 "\n",
475 "sc_c1 = DateColorScale(scheme='Reds')\n",
476 "scatter = ScatterGL(x=sec2_levels, y=sec1_levels, color=dates_all,\n",
477 " scales={'x': sc_x, 'y': sc_y, 'color': sc_c1}, default_size=128,\n",
478 " stroke='black')\n",
479 "\n",
480 "ax_y = Axis(label='Security 1 Level', scale=sc_y, orientation='vertical', side='left')\n",
481 "\n",
482 "ax_x = Axis(label='Security 2', scale=sc_x)\n",
483 "ax_c = ColorAxis(scale=sc_c1, label='Date', num_ticks=5)\n",
484 "\n",
485 "m_chart = dict(top=50, bottom=80, left=50, right=50)\n",
486 "Figure(axes=[ax_x, ax_c, ax_y], marks=[scatter], fig_margin=m_chart)"
487 ]
488 },
489 {
490 "cell_type": "markdown",
491 "metadata": {
492 "input_collapsed": false
493 },
494 "source": [
495 "## Setting size and opacity based on data"
496 ]
497 },
498 {
499 "cell_type": "code",
500 "execution_count": null,
501 "metadata": {
502 "input_collapsed": false
503 },
504 "outputs": [],
505 "source": [
506 "sc_x = LinearScale()\n",
507 "sc_y = LinearScale()\n",
508 "sc_y2 = LinearScale()\n",
509 "\n",
510 "sc_size = LinearScale()\n",
511 "sc_opacity = LinearScale()\n",
512 "\n",
513 "\n",
514 "scatter2 = ScatterGL(x=sec2_levels, y=sec1_levels, size=sec1_returns,\n",
515 " scales={'x': sc_x, 'y': sc_y, 'size': sc_size}, \n",
516 " default_size=128, colors=['orangered'], stroke='black')\n",
517 "\n",
518 "ax_y = Axis(label='Security 1', scale=sc_y, orientation='vertical', side='left')\n",
519 "ax_x = Axis(label='Security 2', scale=sc_x)\n",
520 "\n",
521 "Figure(axes=[ax_x, ax_y], marks=[scatter2])"
522 ]
523 },
524 {
525 "cell_type": "code",
526 "execution_count": null,
527 "metadata": {},
528 "outputs": [],
529 "source": [
530 "## Changing the opacity of the scatter\n",
531 "scatter2.opacities = [0.5, 0.3, 0.1]"
532 ]
533 },
534 {
535 "cell_type": "code",
536 "execution_count": null,
537 "metadata": {},
538 "outputs": [],
539 "source": [
540 "## Resetting the opacity and setting the opacity according to the date\n",
541 "scatter2.opacities = [1.0]"
542 ]
543 },
544 {
545 "cell_type": "markdown",
546 "metadata": {},
547 "source": [
548 "## Rotation scale"
549 ]
550 },
551 {
552 "cell_type": "code",
553 "execution_count": null,
554 "metadata": {},
555 "outputs": [],
556 "source": [
557 "sc_x = LinearScale()\n",
558 "sc_y = LinearScale()\n",
559 "sc_e = LinearScale()\n",
560 "sc_c = ColorScale(scheme='Reds')\n",
561 "x1 = np.linspace(-1, 1, 250)\n",
562 "y1 = np.linspace(-1, 1, 250)\n",
563 "x, y = np.meshgrid(x1,y1)\n",
564 "x, y = x.flatten(), y.flatten()\n",
565 "rot = x**2 + y**2\n",
566 "color=x-y\n",
567 "scatter = ScatterGL(scales={'x': sc_x, 'y': sc_y, 'color': sc_c, 'rotation': sc_e}, \n",
568 " x=x, y=y, rotation=rot, color=color,\n",
569 " stroke=\"black\", stroke_width=20, default_size=200, \n",
570 " marker='arrow', default_skew=0.5)\n",
571 "\n",
572 "fig = Figure(marks=[scatter], animation_duration=1000)\n",
573 "\n",
574 "panzoom = PanZoom(scales={'x': [sc_x], 'y': [sc_y]})\n",
575 "fig.interaction = panzoom\n",
576 "\n",
577 "fig"
578 ]
579 },
580 {
581 "cell_type": "code",
582 "execution_count": null,
583 "metadata": {},
584 "outputs": [],
585 "source": [
586 "scatter.rotation = 1.0 / (x ** 2 + y ** 2 + 1)"
587 ]
588 },
589 {
590 "cell_type": "code",
591 "execution_count": null,
592 "metadata": {},
593 "outputs": [],
594 "source": [
595 "scatter.rotation = x**2 + y**2"
596 ]
597 }
598 ],
599 "metadata": {
600 "kernelspec": {
601 "display_name": "Python 3",
602 "language": "python",
603 "name": "python3"
604 },
605 "language_info": {
606 "codemirror_mode": {
607 "name": "ipython",
608 "version": 3
609 },
610 "file_extension": ".py",
611 "mimetype": "text/x-python",
612 "name": "python",
613 "nbconvert_exporter": "python",
614 "pygments_lexer": "ipython3",
615 "version": "3.8.2"
616 }
617 },
618 "nbformat": 4,
619 "nbformat_minor": 2
620 }