Debian Codegraph @

bqplot/examples/Basic Plotting/Pyplot.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "from __future__ import print_function\n",
10 "import ipywidgets as widgets\n",
11 "from bqplot import pyplot as plt\n",
12 "from bqplot import topo_load\n",
13 "from bqplot.interacts import panzoom\n",
14 "import numpy as np\n",
15 "import pandas as pd\n",
16 "import datetime as dt"
17 ]
18 },
19 {
20 "cell_type": "code",
21 "execution_count": null,
22 "metadata": {},
23 "outputs": [],
24 "source": [
25 "# initializing data to be plotted\n",
26 "np.random.seed(0)\n",
27 "size = 100\n",
28 "y_data = np.cumsum(np.random.randn(size) * 100.0)\n",
29 "y_data_2 = np.cumsum(np.random.randn(size))\n",
30 "y_data_3 = np.cumsum(np.random.randn(size) * 100.)\n",
31 "\n",
32 "x = np.linspace(0.0, 10.0, size)"
33 ]
34 },
35 {
36 "cell_type": "code",
37 "execution_count": null,
38 "metadata": {},
39 "outputs": [],
40 "source": [
41 "price_data = pd.DataFrame(np.cumsum(np.random.randn(150, 2).dot([[0.5, 0.8], [0.8, 1.0]]), axis=0) + 100,\n",
42 " columns=['Security 1', 'Security 2'],\n",
43 " index=pd.date_range(start='01-01-2007', periods=150))\n",
44 "\n",
45 "symbol = 'Security 1'\n",
46 "dates_all = price_data.index.values\n",
47 "final_prices = price_data[symbol].values.flatten()"
48 ]
49 },
50 {
51 "cell_type": "code",
52 "execution_count": null,
53 "metadata": {},
54 "outputs": [],
55 "source": [
56 "price_data.index.names = ['date']"
57 ]
58 },
59 {
60 "cell_type": "markdown",
61 "metadata": {},
62 "source": [
63 "## Simple Plots"
64 ]
65 },
66 {
67 "cell_type": "markdown",
68 "metadata": {},
69 "source": [
70 "### Line Chart"
71 ]
72 },
73 {
74 "cell_type": "code",
75 "execution_count": null,
76 "metadata": {},
77 "outputs": [],
78 "source": [
79 "plt.figure()\n",
80 "plt.plot(x, y_data)\n",
81 "plt.xlabel('Time')\n",
82 "plt.show()"
83 ]
84 },
85 {
86 "cell_type": "code",
87 "execution_count": null,
88 "metadata": {},
89 "outputs": [],
90 "source": [
91 "_ = plt.ylabel('Stock Price')"
92 ]
93 },
94 {
95 "cell_type": "code",
96 "execution_count": null,
97 "metadata": {},
98 "outputs": [],
99 "source": [
100 "# Setting the title for the current figure\n",
101 "plt.title('Brownian Increments')"
102 ]
103 },
104 {
105 "cell_type": "code",
106 "execution_count": null,
107 "metadata": {},
108 "outputs": [],
109 "source": [
110 "plt.figure()\n",
111 "plt.plot('Security 1', data=price_data)\n",
112 "plt.show()"
113 ]
114 },
115 {
116 "cell_type": "markdown",
117 "metadata": {},
118 "source": [
119 "### Scatter Plot"
120 ]
121 },
122 {
123 "cell_type": "code",
124 "execution_count": null,
125 "metadata": {},
126 "outputs": [],
127 "source": [
128 "plt.figure(title='Scatter Plot with colors')\n",
129 "plt.scatter(y_data_2, y_data_3, color=y_data, stroke='black')\n",
130 "plt.show()"
131 ]
132 },
133 {
134 "cell_type": "markdown",
135 "metadata": {},
136 "source": [
137 "### Horizontal and Vertical Lines"
138 ]
139 },
140 {
141 "cell_type": "code",
142 "execution_count": null,
143 "metadata": {},
144 "outputs": [],
145 "source": [
146 "## adding a horizontal line at y=0\n",
147 "plt.hline(0)\n",
148 "plt.show()"
149 ]
150 },
151 {
152 "cell_type": "code",
153 "execution_count": null,
154 "metadata": {},
155 "outputs": [],
156 "source": [
157 "## adding a vertical line at x=4 with stroke_width and colors being passed.\n",
158 "plt.vline(4., stroke_width=2, colors=['orangered'])\n",
159 "plt.show()"
160 ]
161 },
162 {
163 "cell_type": "code",
164 "execution_count": null,
165 "metadata": {},
166 "outputs": [],
167 "source": [
168 "plt.figure()\n",
169 "plt.scatter('Security 1', 'Security 2', color='date', data=price_data.reset_index(), stroke='black')\n",
170 "plt.show()"
171 ]
172 },
173 {
174 "cell_type": "markdown",
175 "metadata": {},
176 "source": [
177 "### Histogram"
178 ]
179 },
180 {
181 "cell_type": "code",
182 "execution_count": null,
183 "metadata": {},
184 "outputs": [],
185 "source": [
186 "plt.figure()\n",
187 "plt.hist(y_data, colors=['OrangeRed'])\n",
188 "plt.show()"
189 ]
190 },
191 {
192 "cell_type": "code",
193 "execution_count": null,
194 "metadata": {},
195 "outputs": [],
196 "source": [
197 "plt.figure()\n",
198 "plt.hist('Security 1', data=price_data, colors=['MediumSeaGreen'])\n",
199 "plt.xlabel('Hello')\n",
200 "plt.show()"
201 ]
202 },
203 {
204 "cell_type": "markdown",
205 "metadata": {},
206 "source": [
207 "### Bar Chart"
208 ]
209 },
210 {
211 "cell_type": "code",
212 "execution_count": null,
213 "metadata": {},
214 "outputs": [],
215 "source": [
216 "plt.figure()\n",
217 "bar_x=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U']\n",
218 "plt.bar(bar_x, y_data_3)\n",
219 "plt.show()"
220 ]
221 },
222 {
223 "cell_type": "code",
224 "execution_count": null,
225 "metadata": {},
226 "outputs": [],
227 "source": [
228 "plt.figure()\n",
229 "plt.bar('date', 'Security 2', data=price_data.reset_index()[:10])\n",
230 "plt.show()"
231 ]
232 },
233 {
234 "cell_type": "markdown",
235 "metadata": {},
236 "source": [
237 "### Pie Chart"
238 ]
239 },
240 {
241 "cell_type": "code",
242 "execution_count": null,
243 "metadata": {},
244 "outputs": [],
245 "source": [
246 "plt.figure()\n",
247 "d = abs(y_data_2[:5])\n",
248 "plt.pie(d)\n",
249 "plt.show()"
250 ]
251 },
252 {
253 "cell_type": "code",
254 "execution_count": null,
255 "metadata": {},
256 "outputs": [],
257 "source": [
258 "plt.figure()\n",
259 "plt.pie('Security 2', color='Security 1', data=price_data[:4])\n",
260 "plt.show()"
261 ]
262 },
263 {
264 "cell_type": "markdown",
265 "metadata": {},
266 "source": [
267 "### OHLC"
268 ]
269 },
270 {
271 "cell_type": "code",
272 "execution_count": null,
273 "metadata": {},
274 "outputs": [],
275 "source": [
276 "dates = np.arange(dt.datetime(2014, 1, 2), dt.datetime(2014, 1, 30), dt.timedelta(days=1))\n",
277 "\n",
278 "prices = np.array([[ 187.21 , 187.4 , 185.2 , 185.53 ],\n",
279 " [ 185.83 , 187.35 , 185.3 , 186.64 ],\n",
280 " [ 187.15 , 187.355 , 185.3 , 186. ],\n",
281 " [ 186.39 , 190.35 , 186.38 , 189.71 ],\n",
282 " [ 189.33 , 189.4175, 187.26 , 187.97 ],\n",
283 " [ 189.02 , 189.5 , 186.55 , 187.38 ],\n",
284 " [ 188.31 , 188.57 , 186.28 , 187.26 ],\n",
285 " [ 186.26 , 186.95 , 183.86 , 184.16 ],\n",
286 " [ 185.06 , 186.428 , 183.8818, 185.92 ],\n",
287 " [ 185.82 , 188.65 , 185.49 , 187.74 ],\n",
288 " [ 187.53 , 188.99 , 186.8 , 188.76 ],\n",
289 " [ 188.04 , 190.81 , 187.86 , 190.09 ],\n",
290 " [ 190.23 , 190.39 , 186.79 , 188.43 ],\n",
291 " [ 181.28 , 183.5 , 179.67 , 182.25 ],\n",
292 " [ 181.43 , 183.72 , 180.71 , 182.73 ],\n",
293 " [ 181.25 , 182.8141, 179.64 , 179.64 ],\n",
294 " [ 179.605 , 179.65 , 177.66 , 177.9 ],\n",
295 " [ 178.05 , 178.45 , 176.16 , 176.85 ],\n",
296 " [ 175.98 , 178.53 , 175.89 , 176.4 ],\n",
297 " [ 177.17 , 177.86 , 176.36 , 177.36 ]])\n",
298 "\n",
299 "plt.figure()\n",
300 "plt.ohlc(dates, prices)\n",
301 "plt.show()"
302 ]
303 },
304 {
305 "cell_type": "markdown",
306 "metadata": {},
307 "source": [
308 "### Boxplot"
309 ]
310 },
311 {
312 "cell_type": "code",
313 "execution_count": null,
314 "metadata": {},
315 "outputs": [],
316 "source": [
317 "plt.figure()\n",
318 "plt.boxplot(np.arange(10), np.random.randn(10, 100))\n",
319 "plt.show()"
320 ]
321 },
322 {
323 "cell_type": "markdown",
324 "metadata": {},
325 "source": [
326 "### Map"
327 ]
328 },
329 {
330 "cell_type": "code",
331 "execution_count": null,
332 "metadata": {},
333 "outputs": [],
334 "source": [
335 "plt.figure()\n",
336 "plt.geo(map_data='WorldMap')\n",
337 "plt.show()"
338 ]
339 },
340 {
341 "cell_type": "markdown",
342 "metadata": {},
343 "source": [
344 "### Heatmap"
345 ]
346 },
347 {
348 "cell_type": "code",
349 "execution_count": null,
350 "metadata": {},
351 "outputs": [],
352 "source": [
353 "plt.figure(padding_y=0)\n",
354 "plt.heatmap(x * x[:, np.newaxis])\n",
355 "plt.show()"
356 ]
357 },
358 {
359 "cell_type": "markdown",
360 "metadata": {},
361 "source": [
362 "### GridHeatMap"
363 ]
364 },
365 {
366 "cell_type": "code",
367 "execution_count": null,
368 "metadata": {},
369 "outputs": [],
370 "source": [
371 "plt.figure(padding_y=0)\n",
372 "plt.gridheatmap(x[:10] * x[:10, np.newaxis])\n",
373 "plt.show()"
374 ]
375 },
376 {
377 "cell_type": "markdown",
378 "metadata": {},
379 "source": [
380 "### Plotting Dates"
381 ]
382 },
383 {
384 "cell_type": "code",
385 "execution_count": null,
386 "metadata": {},
387 "outputs": [],
388 "source": [
389 "plt.figure()\n",
390 "plt.plot(dates_all, final_prices)\n",
391 "plt.show()"
392 ]
393 },
394 {
395 "cell_type": "markdown",
396 "metadata": {},
397 "source": [
398 "### Editing existing axes properties"
399 ]
400 },
401 {
402 "cell_type": "code",
403 "execution_count": null,
404 "metadata": {},
405 "outputs": [],
406 "source": [
407 "## adding grid lines and changing the side of the axis in the figure above\n",
408 "_ = plt.axes(options={'x': {'grid_lines': 'solid'}, 'y': {'side': 'right', 'grid_lines': 'dashed'}})"
409 ]
410 },
411 {
412 "cell_type": "markdown",
413 "metadata": {},
414 "source": [
415 "## Advanced Usage"
416 ]
417 },
418 {
419 "cell_type": "markdown",
420 "metadata": {},
421 "source": [
422 "### Multiple Marks on the same Figure"
423 ]
424 },
425 {
426 "cell_type": "code",
427 "execution_count": null,
428 "metadata": {},
429 "outputs": [],
430 "source": [
431 "plt.figure()\n",
432 "plt.plot(x, y_data_3, colors=['orangered'])\n",
433 "plt.scatter(x, y_data, stroke='black')\n",
434 "plt.show()"
435 ]
436 },
437 {
438 "cell_type": "markdown",
439 "metadata": {},
440 "source": [
441 "### Using marker strings in Line Chart"
442 ]
443 },
444 {
445 "cell_type": "code",
446 "execution_count": null,
447 "metadata": {},
448 "outputs": [],
449 "source": [
450 "mark_x = np.arange(10)\n",
451 "plt.figure(title='Using Marker Strings')\n",
452 "plt.plot(mark_x, 3 * mark_x + 5, 'g-.s') # color=green, line_style=dash_dotted, marker=square\n",
453 "plt.plot(mark_x ** 2, 'm:d') # color=magenta, line_style=None, marker=diamond\n",
454 "plt.show()"
455 ]
456 },
457 {
458 "cell_type": "markdown",
459 "metadata": {
460 "collapsed": true
461 },
462 "source": [
463 "### Partially changing the scales"
464 ]
465 },
466 {
467 "cell_type": "code",
468 "execution_count": null,
469 "metadata": {},
470 "outputs": [],
471 "source": [
472 "plt.figure()\n",
473 "plt.plot(x, y_data)\n",
474 "\n",
475 "## preserving the x scale and changing the y scale\n",
476 "plt.scales(scales={'x': plt.Keep})\n",
477 "plt.plot(x, y_data_2, colors=['orange'], axes_options={'y': {'side': 'right', 'color': 'orange',\n",
478 " 'grid_lines': 'none'}})\n",
479 "plt.show()"
480 ]
481 },
482 {
483 "cell_type": "markdown",
484 "metadata": {},
485 "source": [
486 "### Adding a label to the chart"
487 ]
488 },
489 {
490 "cell_type": "code",
491 "execution_count": null,
492 "metadata": {},
493 "outputs": [],
494 "source": [
495 "plt.figure()\n",
496 "line = plt.plot(dates_all, final_prices)\n",
497 "plt.show()"
498 ]
499 },
500 {
501 "cell_type": "code",
502 "execution_count": null,
503 "metadata": {},
504 "outputs": [],
505 "source": [
506 "## adds the label to the figure created above\n",
507 "_ = plt.label(['Pie Day'], x=[np.datetime64('2007-03-14')], y=[final_prices.mean()], scales=line.scales,\n",
508 " colors=['orangered'])"
509 ]
510 },
511 {
512 "cell_type": "markdown",
513 "metadata": {},
514 "source": [
515 "### Changing context figure"
516 ]
517 },
518 {
519 "cell_type": "code",
520 "execution_count": null,
521 "metadata": {},
522 "outputs": [],
523 "source": [
524 "plt.figure(1)\n",
525 "plt.plot(x,y_data_3)\n",
526 "plt.show()"
527 ]
528 },
529 {
530 "cell_type": "code",
531 "execution_count": null,
532 "metadata": {},
533 "outputs": [],
534 "source": [
535 "plt.figure(2)\n",
536 "plt.plot(x[:20],y_data_3[:20])\n",
537 "plt.show()"
538 ]
539 },
540 {
541 "cell_type": "markdown",
542 "metadata": {},
543 "source": [
544 "### Re-editing first figure"
545 ]
546 },
547 {
548 "cell_type": "code",
549 "execution_count": null,
550 "metadata": {},
551 "outputs": [],
552 "source": [
553 "## adds the new line to the first figure\n",
554 "fig = plt.figure(1, title='New title')\n",
555 "plt.plot(x,y_data, colors=['orangered'])\n",
556 "fig"
557 ]
558 },
559 {
560 "cell_type": "markdown",
561 "metadata": {},
562 "source": [
563 "### Viewing the properties of the figure"
564 ]
565 },
566 {
567 "cell_type": "code",
568 "execution_count": null,
569 "metadata": {},
570 "outputs": [],
571 "source": [
572 "marks = plt.current_figure().marks\n",
573 "marks[0].get_state()"
574 ]
575 },
576 {
577 "cell_type": "markdown",
578 "metadata": {},
579 "source": [
580 "### Showing a second view of the first figure"
581 ]
582 },
583 {
584 "cell_type": "code",
585 "execution_count": null,
586 "metadata": {},
587 "outputs": [],
588 "source": [
589 "plt.show()"
590 ]
591 },
592 {
593 "cell_type": "markdown",
594 "metadata": {},
595 "source": [
596 "### Clearing the figure"
597 ]
598 },
599 {
600 "cell_type": "code",
601 "execution_count": null,
602 "metadata": {},
603 "outputs": [],
604 "source": [
605 "### Clearing the figure above\n",
606 "plt.clear()"
607 ]
608 },
609 {
610 "cell_type": "markdown",
611 "metadata": {},
612 "source": [
613 "### Deleting a figure and all its views. "
614 ]
615 },
616 {
617 "cell_type": "code",
618 "execution_count": null,
619 "metadata": {},
620 "outputs": [],
621 "source": [
622 "plt.show(2)"
623 ]
624 },
625 {
626 "cell_type": "code",
627 "execution_count": null,
628 "metadata": {},
629 "outputs": [],
630 "source": [
631 "plt.close(2)"
632 ]
633 },
634 {
635 "cell_type": "markdown",
636 "metadata": {},
637 "source": [
638 "## Interactions in Pyplot"
639 ]
640 },
641 {
642 "cell_type": "markdown",
643 "metadata": {},
644 "source": [
645 "### Brush Selector"
646 ]
647 },
648 {
649 "cell_type": "code",
650 "execution_count": null,
651 "metadata": {},
652 "outputs": [],
653 "source": [
654 "fig = plt.figure()\n",
655 "plt.scatter(y_data_2, y_data_3, colors=['orange'], stroke='black')\n",
656 "\n",
657 "label = widgets.Label()\n",
658 "\n",
659 "def callback(name, value):\n",
660 " label.value = str(value)\n",
661 "## click and drag on the figure to see the selector\n",
662 "plt.brush_selector(callback)\n",
663 "\n",
664 "widgets.VBox([fig, label])"
665 ]
666 },
667 {
668 "cell_type": "markdown",
669 "metadata": {},
670 "source": [
671 "### Fast Interval Selector"
672 ]
673 },
674 {
675 "cell_type": "code",
676 "execution_count": null,
677 "metadata": {},
678 "outputs": [],
679 "source": [
680 "fig = plt.figure()\n",
681 "n = 100\n",
682 "plt.plot(np.arange(n), y_data_3)\n",
683 "label = widgets.Label()\n",
684 "\n",
685 "def callback(name, value):\n",
686 " label.value = str(value)\n",
687 "\n",
688 "## click on the figure to activate the selector\n",
689 "plt.int_selector(callback)\n",
690 "\n",
691 "widgets.VBox([fig, label])"
692 ]
693 },
694 {
695 "cell_type": "markdown",
696 "metadata": {},
697 "source": [
698 "### Brush Interval Selector with call back on brushing"
699 ]
700 },
701 {
702 "cell_type": "code",
703 "execution_count": null,
704 "metadata": {},
705 "outputs": [],
706 "source": [
707 "# click and drag on chart to make a selection\n",
708 "def callback(name, value):\n",
709 " label.value = 'Brushing: ' + str(value)\n",
710 "_ = plt.brush_int_selector(callback, 'brushing')"
711 ]
712 }
713 ],
714 "metadata": {
715 "anaconda-cloud": {},
716 "kernelspec": {
717 "display_name": "Python 3",
718 "language": "python",
719 "name": "python3"
720 },
721 "language_info": {
722 "codemirror_mode": {
723 "name": "ipython",
724 "version": 3
725 },
726 "file_extension": ".py",
727 "mimetype": "text/x-python",
728 "name": "python",
729 "nbconvert_exporter": "python",
730 "pygments_lexer": "ipython3",
731 "version": "3.7.6"
732 }
733 },
734 "nbformat": 4,
735 "nbformat_minor": 2
736 }