Debian Codegraph @

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

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "## The `Lines` Mark\n",
8 "\n",
9 "`Lines` is a `Mark` object that is primarily used to visualize quantitative data. It works particularly well for continuous data, or when the shape of the data needs to be extracted."
10 ]
11 },
12 {
13 "cell_type": "markdown",
14 "metadata": {},
15 "source": [
16 "### Introduction\n",
17 "\n",
18 "The `Lines` object provides the following features:\n",
19 "\n",
20 "1. Ability to plot a single set or multiple sets of y-values as a function of a set or multiple sets of x-values\n",
21 "2. Ability to style the line object in different ways, by setting different attributes such as the `colors`, `line_style`, `stroke_width` etc.\n",
22 "3. Ability to specify a marker at each point passed to the line. The marker can be a shape which is at the data points between which the line is interpolated and can be set through the `markers` attribute"
23 ]
24 },
25 {
26 "cell_type": "markdown",
27 "metadata": {},
28 "source": [
29 "The `Lines` object has the following attributes"
30 ]
31 },
32 {
33 "cell_type": "markdown",
34 "metadata": {},
35 "source": [
36 " | Attribute | Description | Default Value |\n",
37 "|:-:|---|:-:|\n",
38 "| `colors` | Sets the color of each line, takes as input a list of any RGB, HEX, or HTML color name | `CATEGORY10` |\n",
39 "| `opacities` | Controls the opacity of each line, takes as input a real number between 0 and 1 | `1.0` |\n",
40 "| `stroke_width` | Real number which sets the width of all paths | `2.0` |\n",
41 "| `line_style` | Specifies whether a line is solid, dashed, dotted or both dashed and dotted | `'solid'` |\n",
42 "| `interpolation` | Sets the type of interpolation between two points | `'linear'` |\n",
43 "| `marker` | Specifies the shape of the marker inserted at each data point | `None` |\n",
44 "| `marker_size` | Controls the size of the marker, takes as input a non-negative integer | `64` |\n",
45 "|`close_path`| Controls whether to close the paths or not | `False` |\n",
46 "|`fill`| Specifies in which way the paths are filled. Can be set to one of `{'none', 'bottom', 'top', 'inside'}`| `None` |\n",
47 "|`fill_colors`| `List` that specifies the `fill` colors of each path | `[]` | \n",
48 "| **Data Attribute** | **Description** | **Default Value** |\n",
49 "|`x` |abscissas of the data points | `array([])` |\n",
50 "|`y` |ordinates of the data points | `array([])` |\n",
51 "|`color` | Data according to which the `Lines` will be colored. Setting it to `None` defaults the choice of colors to the `colors` attribute | `None` |"
52 ]
53 },
54 {
55 "cell_type": "markdown",
56 "metadata": {},
57 "source": [
58 "To explore more features, run the following lines of code:\n",
59 " \n",
60 "```python\n",
61 "from bqplot import Lines\n",
62 "?Lines\n",
63 "```\n",
64 "\n",
65 "or visit the [`Lines` documentation page](http://bqplot.readthedocs.io/en/stable/_generate/bqplot.marks.Lines.html#bqplot.marks.Lines)"
66 ]
67 },
68 {
69 "cell_type": "markdown",
70 "metadata": {},
71 "source": [
72 "**Let's explore these features one by one**"
73 ]
74 },
75 {
76 "cell_type": "markdown",
77 "metadata": {},
78 "source": [
79 "We begin by importing the modules that we will need in this example"
80 ]
81 },
82 {
83 "cell_type": "code",
84 "execution_count": null,
85 "metadata": {},
86 "outputs": [],
87 "source": [
88 "import numpy as np #For numerical programming and multi-dimensional arrays\n",
89 "from pandas import date_range #For date-rate generation\n",
90 "from bqplot import LinearScale, Lines, Axis, Figure, DateScale, ColorScale"
91 ]
92 },
93 {
94 "cell_type": "markdown",
95 "metadata": {},
96 "source": [
97 "**Random Data Generation**"
98 ]
99 },
100 {
101 "cell_type": "code",
102 "execution_count": null,
103 "metadata": {},
104 "outputs": [],
105 "source": [
106 "security_1 = np.cumsum(np.random.randn(150)) + 100.\n",
107 "security_2 = np.cumsum(np.random.randn(150)) + 100."
108 ]
109 },
110 {
111 "cell_type": "markdown",
112 "metadata": {},
113 "source": [
114 "## Basic Line Chart"
115 ]
116 },
117 {
118 "cell_type": "markdown",
119 "metadata": {},
120 "source": [
121 "Using the `bqplot`, object oriented API, we can generate a Line Chart with the following code snippet:"
122 ]
123 },
124 {
125 "cell_type": "code",
126 "execution_count": null,
127 "metadata": {},
128 "outputs": [],
129 "source": [
130 "sc_x = LinearScale()\n",
131 "sc_y = LinearScale()\n",
132 "\n",
133 "line = Lines(x=np.arange(len(security_1)), y=security_1,\n",
134 " scales={'x': sc_x, 'y': sc_y})\n",
135 "ax_x = Axis(scale=sc_x, label='Index')\n",
136 "ax_y = Axis(scale=sc_y, orientation='vertical', label='y-values of Security 1')\n",
137 "\n",
138 "Figure(marks=[line], axes=[ax_x, ax_y], title='Security 1')"
139 ]
140 },
141 {
142 "cell_type": "markdown",
143 "metadata": {},
144 "source": [
145 "The `x` attribute refers to the data represented horizontally, while the `y` attribute refers the data represented vertically. \n",
146 "\n",
147 "**We can explore the different attributes by changing each of them for the plot above:**"
148 ]
149 },
150 {
151 "cell_type": "code",
152 "execution_count": null,
153 "metadata": {},
154 "outputs": [],
155 "source": [
156 "line.colors = ['DarkOrange']"
157 ]
158 },
159 {
160 "cell_type": "markdown",
161 "metadata": {},
162 "source": [
163 "In a similar way, we can also change any attribute after the plot has been displayed to change the plot. Run each of the cells below, and try changing the attributes to explore the different features and how they affect the plot."
164 ]
165 },
166 {
167 "cell_type": "code",
168 "execution_count": null,
169 "metadata": {},
170 "outputs": [],
171 "source": [
172 "# The opacity allows us to display the Line while featuring other Marks that may be on the Figure\n",
173 "line.opacities = [.5]"
174 ]
175 },
176 {
177 "cell_type": "code",
178 "execution_count": null,
179 "metadata": {},
180 "outputs": [],
181 "source": [
182 "line.stroke_width = 2.5"
183 ]
184 },
185 {
186 "cell_type": "markdown",
187 "metadata": {},
188 "source": [
189 "To switch to an area chart, set the `fill` attribute, and control the look with `fill_opacities` and `fill_colors`."
190 ]
191 },
192 {
193 "cell_type": "code",
194 "execution_count": null,
195 "metadata": {},
196 "outputs": [],
197 "source": [
198 "line.fill = 'bottom'\n",
199 "line.fill_opacities = [0.2]"
200 ]
201 },
202 {
203 "cell_type": "code",
204 "execution_count": null,
205 "metadata": {},
206 "outputs": [],
207 "source": [
208 "line.line_style = 'dashed'"
209 ]
210 },
211 {
212 "cell_type": "code",
213 "execution_count": null,
214 "metadata": {},
215 "outputs": [],
216 "source": [
217 "line.interpolation = 'basis'"
218 ]
219 },
220 {
221 "cell_type": "markdown",
222 "metadata": {},
223 "source": [
224 "While a `Lines` plot allows the user to extract the general shape of the data being plotted, there may be a need to visualize discrete data points along with this shape. This is where the `markers` attribute comes in."
225 ]
226 },
227 {
228 "cell_type": "code",
229 "execution_count": null,
230 "metadata": {},
231 "outputs": [],
232 "source": [
233 "line.marker = 'triangle-down'"
234 ]
235 },
236 {
237 "cell_type": "markdown",
238 "metadata": {},
239 "source": [
240 "The `marker` attributes accepts the values `square`, `circle`, `cross`, `diamond`, `square`, `triangle-down`, `triangle-up`, `arrow`, `rectangle`, `ellipse`, `plus`, `crosshair`, `point`. Try changing the string above and re-running the cell to see how each `marker` type looks."
241 ]
242 },
243 {
244 "cell_type": "markdown",
245 "metadata": {
246 "collapsed": true
247 },
248 "source": [
249 "## Plotting a Time-Series"
250 ]
251 },
252 {
253 "cell_type": "markdown",
254 "metadata": {},
255 "source": [
256 "The `DateScale` allows us to plot time series as a `Lines` plot conveniently with most `date` formats."
257 ]
258 },
259 {
260 "cell_type": "code",
261 "execution_count": null,
262 "metadata": {},
263 "outputs": [],
264 "source": [
265 "# Here we define the dates we would like to use\n",
266 "dates = date_range(start='01-01-2007', periods=150)"
267 ]
268 },
269 {
270 "cell_type": "code",
271 "execution_count": null,
272 "metadata": {},
273 "outputs": [],
274 "source": [
275 "dt_x = DateScale()\n",
276 "sc_y = LinearScale()\n",
277 "\n",
278 "time_series = Lines(x=dates, y=security_1, scales={'x': dt_x, 'y': sc_y})\n",
279 "ax_x = Axis(scale=dt_x, label='Date')\n",
280 "ax_y = Axis(scale=sc_y, orientation='vertical', label='Security 1')\n",
281 "\n",
282 "Figure(marks=[time_series], axes=[ax_x, ax_y], title='A Time Series Plot')"
283 ]
284 },
285 {
286 "cell_type": "markdown",
287 "metadata": {},
288 "source": [
289 "## Plotting Multiples Sets of Data with `Lines`"
290 ]
291 },
292 {
293 "cell_type": "markdown",
294 "metadata": {},
295 "source": [
296 "The `Lines` mark allows the user to plot multiple `y`-values for a single `x`-value. This can be done by passing an `ndarray` or a list of the different `y`-values as the y-attribute of the `Lines` as shown below."
297 ]
298 },
299 {
300 "cell_type": "code",
301 "execution_count": null,
302 "metadata": {},
303 "outputs": [],
304 "source": [
305 "x_dt = DateScale()\n",
306 "y_sc = LinearScale()"
307 ]
308 },
309 {
310 "cell_type": "markdown",
311 "metadata": {},
312 "source": [
313 "We pass each data set as an element of a `list`. The colors attribute allows us to pass a specific color for each line."
314 ]
315 },
316 {
317 "cell_type": "code",
318 "execution_count": null,
319 "metadata": {},
320 "outputs": [],
321 "source": [
322 "dates_new = date_range(start='06-01-2007', periods=150)"
323 ]
324 },
325 {
326 "cell_type": "code",
327 "execution_count": null,
328 "metadata": {},
329 "outputs": [],
330 "source": [
331 "securities = np.cumsum(np.random.randn(150, 10), axis=0)\n",
332 "positions = np.random.randint(0, 2, size=10)"
333 ]
334 },
335 {
336 "cell_type": "code",
337 "execution_count": null,
338 "metadata": {},
339 "outputs": [],
340 "source": [
341 "# We pass the color scale and the color data to the lines\n",
342 "line = Lines(x=dates, y=[security_1, security_2], \n",
343 " scales={'x': x_dt, 'y': y_sc},\n",
344 " labels=['Security 1', 'Security 2'])"
345 ]
346 },
347 {
348 "cell_type": "code",
349 "execution_count": null,
350 "metadata": {},
351 "outputs": [],
352 "source": [
353 "ax_x = Axis(scale=x_dt, label='Date')\n",
354 "ax_y = Axis(scale=y_sc, orientation='vertical', label='Security 1')\n",
355 "\n",
356 "Figure(marks=[line], axes=[ax_x, ax_y], legend_location='top-left')"
357 ]
358 },
359 {
360 "cell_type": "markdown",
361 "metadata": {},
362 "source": [
363 "Similarly, we can also pass multiple `x`-values for multiple sets of `y`-values"
364 ]
365 },
366 {
367 "cell_type": "code",
368 "execution_count": null,
369 "metadata": {},
370 "outputs": [],
371 "source": [
372 "line.x, line.y = [dates, dates_new], [security_1, security_2]"
373 ]
374 },
375 {
376 "cell_type": "markdown",
377 "metadata": {},
378 "source": [
379 "### Coloring Lines according to data"
380 ]
381 },
382 {
383 "cell_type": "markdown",
384 "metadata": {},
385 "source": [
386 "The `color` attribute of a `Lines` mark can also be used to encode one more dimension of data. Suppose we have a portfolio of securities and we would like to color them based on whether we have bought or sold them. We can use the `color` attribute to encode this information."
387 ]
388 },
389 {
390 "cell_type": "code",
391 "execution_count": null,
392 "metadata": {},
393 "outputs": [],
394 "source": [
395 "x_dt = DateScale()\n",
396 "y_sc = LinearScale()\n",
397 "col_sc = ColorScale(colors=['Red', 'Green'])"
398 ]
399 },
400 {
401 "cell_type": "code",
402 "execution_count": null,
403 "metadata": {},
404 "outputs": [],
405 "source": [
406 "dates_color = date_range(start='06-01-2007', periods=150)"
407 ]
408 },
409 {
410 "cell_type": "code",
411 "execution_count": null,
412 "metadata": {},
413 "outputs": [],
414 "source": [
415 "securities = 100. + np.cumsum(np.random.randn(150, 10), axis=0)\n",
416 "positions = np.random.randint(0, 2, size=10)\n",
417 "# Here we generate 10 random price series and 10 random positions"
418 ]
419 },
420 {
421 "cell_type": "code",
422 "execution_count": null,
423 "metadata": {},
424 "outputs": [],
425 "source": [
426 "# We pass the color scale and the color data to the lines\n",
427 "line = Lines(x=dates_color, y=securities.T, \n",
428 " scales={'x': x_dt, 'y': y_sc, 'color': col_sc}, color=positions,\n",
429 " labels=['Security 1', 'Security 2'])"
430 ]
431 },
432 {
433 "cell_type": "code",
434 "execution_count": null,
435 "metadata": {},
436 "outputs": [],
437 "source": [
438 "ax_x = Axis(scale=x_dt, label='Date')\n",
439 "ax_y = Axis(scale=y_sc, orientation='vertical', label='Security 1')\n",
440 "\n",
441 "Figure(marks=[line], axes=[ax_x, ax_y], legend_location='top-left')"
442 ]
443 },
444 {
445 "cell_type": "markdown",
446 "metadata": {},
447 "source": [
448 "We can also reset the colors of the Line to their defaults by setting the `color` attribute to `None`."
449 ]
450 },
451 {
452 "cell_type": "code",
453 "execution_count": null,
454 "metadata": {},
455 "outputs": [],
456 "source": [
457 "line.color = None"
458 ]
459 },
460 {
461 "cell_type": "markdown",
462 "metadata": {},
463 "source": [
464 "## Patches"
465 ]
466 },
467 {
468 "cell_type": "markdown",
469 "metadata": {},
470 "source": [
471 "The `fill` attribute of the `Lines` mark allows us to fill a path in different ways, while the `fill_colors` attribute lets us control the color of the `fill`"
472 ]
473 },
474 {
475 "cell_type": "code",
476 "execution_count": null,
477 "metadata": {},
478 "outputs": [],
479 "source": [
480 "sc_x = LinearScale()\n",
481 "sc_y = LinearScale()\n",
482 "\n",
483 "patch = Lines(x=[[0, 2, 1.2, np.nan, np.nan, np.nan, np.nan], [0.5, 2.5, 1.7, np.nan, np.nan, np.nan, np.nan], [4,5,6, 6, 5, 4, 3]], \n",
484 " y=[[0, 0, 1 , np.nan, np.nan, np.nan, np.nan], [0.5, 0.5, -0.5, np.nan, np.nan, np.nan, np.nan], [1, 1.1, 1.2, 2.3, 2.2, 2.7, 1.0]],\n",
485 " fill_colors=['orange', 'blue', 'red'],\n",
486 " fill='inside',\n",
487 " stroke_width=10,\n",
488 " close_path=True,\n",
489 " scales={'x': sc_x, 'y': sc_y},\n",
490 " display_legend=True)\n",
491 "\n",
492 "Figure(marks=[patch], animation_duration=1000)"
493 ]
494 },
495 {
496 "cell_type": "code",
497 "execution_count": null,
498 "metadata": {},
499 "outputs": [],
500 "source": [
501 "patch.opacities = [0.1, 0.2]"
502 ]
503 },
504 {
505 "cell_type": "code",
506 "execution_count": null,
507 "metadata": {},
508 "outputs": [],
509 "source": [
510 "patch.x = [[2, 3, 3.2, np.nan, np.nan, np.nan, np.nan], [0.5, 2.5, 1.7, np.nan, np.nan, np.nan, np.nan], [4,5,6, 6, 5, 4, 3]]"
511 ]
512 },
513 {
514 "cell_type": "code",
515 "execution_count": null,
516 "metadata": {},
517 "outputs": [],
518 "source": [
519 "patch.close_path = False"
520 ]
521 }
522 ],
523 "metadata": {
524 "anaconda-cloud": {},
525 "kernelspec": {
526 "display_name": "Python 3",
527 "language": "python",
528 "name": "python3"
529 },
530 "language_info": {
531 "codemirror_mode": {
532 "name": "ipython",
533 "version": 3
534 },
535 "file_extension": ".py",
536 "mimetype": "text/x-python",
537 "name": "python",
538 "nbconvert_exporter": "python",
539 "pygments_lexer": "ipython3",
540 "version": "3.7.6"
541 }
542 },
543 "nbformat": 4,
544 "nbformat_minor": 1
545 }