Debian Codegraph @

bqplot/examples/Marks/Pyplot/Lines.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "### Introduction\n",
8 "\n",
9 "The `Lines` object provides the following features:\n",
10 "\n",
11 "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",
12 "2. Ability to style the line object in different ways, by setting different attributes such as the `colors`, `line_style`, `stroke_width` etc.\n",
13 "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"
14 ]
15 },
16 {
17 "cell_type": "markdown",
18 "metadata": {},
19 "source": [
20 "The `Lines` object has the following attributes"
21 ]
22 },
23 {
24 "cell_type": "markdown",
25 "metadata": {},
26 "source": [
27 " | Attribute | Description | Default Value |\n",
28 "|:-:|---|:-:|\n",
29 "| `colors` | Sets the color of each line, takes as input a list of any RGB, HEX, or HTML color name | `CATEGORY10` |\n",
30 "| `opacities` | Controls the opacity of each line, takes as input a real number between 0 and 1 | `1.0` |\n",
31 "| `stroke_width` | Real number which sets the width of all paths | `2.0` |\n",
32 "| `line_style` | Specifies whether a line is solid, dashed, dotted or both dashed and dotted | `'solid'` |\n",
33 "| `interpolation` | Sets the type of interpolation between two points | `'linear'` |\n",
34 "| `marker` | Specifies the shape of the marker inserted at each data point | `None` |\n",
35 "| `marker_size` | Controls the size of the marker, takes as input a non-negative integer | `64` |\n",
36 "|`close_path`| Controls whether to close the paths or not | `False` |\n",
37 "|`fill`| Specifies in which way the paths are filled. Can be set to one of `{'none', 'bottom', 'top', 'inside'}`| `None` |\n",
38 "|`fill_colors`| `List` that specifies the `fill` colors of each path | `[]` | \n",
39 "| **Data Attribute** | **Description** | **Default Value** |\n",
40 "|`x` |abscissas of the data points | `array([])` |\n",
41 "|`y` |ordinates of the data points | `array([])` |\n",
42 "|`color` | Data according to which the `Lines` will be colored. Setting it to `None` defaults the choice of colors to the `colors` attribute | `None` |"
43 ]
44 },
45 {
46 "cell_type": "markdown",
47 "metadata": {},
48 "source": [
49 "## pyplot's plot method can be used to plot lines with meaningful defaults"
50 ]
51 },
52 {
53 "cell_type": "code",
54 "execution_count": null,
55 "metadata": {},
56 "outputs": [],
57 "source": [
58 "import numpy as np\n",
59 "from pandas import date_range\n",
60 "import bqplot.pyplot as plt\n",
61 "from bqplot import ColorScale"
62 ]
63 },
64 {
65 "cell_type": "code",
66 "execution_count": null,
67 "metadata": {},
68 "outputs": [],
69 "source": [
70 "security_1 = np.cumsum(np.random.randn(150)) + 100.\n",
71 "security_2 = np.cumsum(np.random.randn(150)) + 100."
72 ]
73 },
74 {
75 "cell_type": "markdown",
76 "metadata": {},
77 "source": [
78 "## Basic Line Chart"
79 ]
80 },
81 {
82 "cell_type": "code",
83 "execution_count": null,
84 "metadata": {},
85 "outputs": [],
86 "source": [
87 "fig = plt.figure(title='Security 1')\n",
88 "axes_options = {'x': {'label': 'Index'}, 'y': {'label': 'Price'}}\n",
89 "# x values default to range of values when not specified\n",
90 "line = plt.plot(security_1, axes_options=axes_options)\n",
91 "fig"
92 ]
93 },
94 {
95 "cell_type": "markdown",
96 "metadata": {},
97 "source": [
98 "**We can explore the different attributes by changing each of them for the plot above:**"
99 ]
100 },
101 {
102 "cell_type": "code",
103 "execution_count": null,
104 "metadata": {},
105 "outputs": [],
106 "source": [
107 "line.colors = ['DarkOrange']"
108 ]
109 },
110 {
111 "cell_type": "markdown",
112 "metadata": {},
113 "source": [
114 "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."
115 ]
116 },
117 {
118 "cell_type": "code",
119 "execution_count": null,
120 "metadata": {},
121 "outputs": [],
122 "source": [
123 "# The opacity allows us to display the Line while featuring other Marks that may be on the Figure\n",
124 "line.opacities = [.5]"
125 ]
126 },
127 {
128 "cell_type": "code",
129 "execution_count": null,
130 "metadata": {},
131 "outputs": [],
132 "source": [
133 "line.stroke_width = 2.5"
134 ]
135 },
136 {
137 "cell_type": "markdown",
138 "metadata": {},
139 "source": [
140 "To switch to an area chart, set the `fill` attribute, and control the look with `fill_opacities` and `fill_colors`."
141 ]
142 },
143 {
144 "cell_type": "code",
145 "execution_count": null,
146 "metadata": {},
147 "outputs": [],
148 "source": [
149 "line.fill = 'bottom'\n",
150 "line.fill_opacities = [0.2]"
151 ]
152 },
153 {
154 "cell_type": "code",
155 "execution_count": null,
156 "metadata": {},
157 "outputs": [],
158 "source": [
159 "line.line_style = 'dashed'"
160 ]
161 },
162 {
163 "cell_type": "code",
164 "execution_count": null,
165 "metadata": {},
166 "outputs": [],
167 "source": [
168 "line.interpolation = 'basis'"
169 ]
170 },
171 {
172 "cell_type": "markdown",
173 "metadata": {},
174 "source": [
175 "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."
176 ]
177 },
178 {
179 "cell_type": "code",
180 "execution_count": null,
181 "metadata": {},
182 "outputs": [],
183 "source": [
184 "line.marker = 'triangle-down'"
185 ]
186 },
187 {
188 "cell_type": "markdown",
189 "metadata": {},
190 "source": [
191 "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."
192 ]
193 },
194 {
195 "cell_type": "markdown",
196 "metadata": {
197 "collapsed": true
198 },
199 "source": [
200 "## Plotting a Time-Series"
201 ]
202 },
203 {
204 "cell_type": "markdown",
205 "metadata": {},
206 "source": [
207 "The `DateScale` allows us to plot time series as a `Lines` plot conveniently with most `date` formats."
208 ]
209 },
210 {
211 "cell_type": "code",
212 "execution_count": null,
213 "metadata": {},
214 "outputs": [],
215 "source": [
216 "# Here we define the dates we would like to use\n",
217 "dates = date_range(start='01-01-2007', periods=150)"
218 ]
219 },
220 {
221 "cell_type": "code",
222 "execution_count": null,
223 "metadata": {},
224 "outputs": [],
225 "source": [
226 "fig = plt.figure(title='Time Series')\n",
227 "axes_options = {'x': {'label': 'Date'}, 'y': {'label': 'Security 1'}}\n",
228 "time_series = plt.plot(dates, security_1, \n",
229 " axes_options=axes_options)\n",
230 "fig"
231 ]
232 },
233 {
234 "cell_type": "markdown",
235 "metadata": {},
236 "source": [
237 "## Plotting multiples sets of data"
238 ]
239 },
240 {
241 "cell_type": "markdown",
242 "metadata": {},
243 "source": [
244 "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."
245 ]
246 },
247 {
248 "cell_type": "code",
249 "execution_count": null,
250 "metadata": {},
251 "outputs": [],
252 "source": [
253 "dates_new = date_range(start='06-01-2007', periods=150)"
254 ]
255 },
256 {
257 "cell_type": "markdown",
258 "metadata": {},
259 "source": [
260 "We pass each data set as an element of a `list`"
261 ]
262 },
263 {
264 "cell_type": "code",
265 "execution_count": null,
266 "metadata": {},
267 "outputs": [],
268 "source": [
269 "fig = plt.figure()\n",
270 "axes_options = {'x': {'label': 'Date'}, 'y': {'label': 'Price'}}\n",
271 "line = plt.plot(dates, [security_1, security_2], \n",
272 " labels=['Security 1', 'Security 2'],\n",
273 " axes_options=axes_options,\n",
274 " display_legend=True)\n",
275 "fig"
276 ]
277 },
278 {
279 "cell_type": "markdown",
280 "metadata": {},
281 "source": [
282 "Similarly, we can also pass multiple `x`-values for multiple sets of `y`-values"
283 ]
284 },
285 {
286 "cell_type": "code",
287 "execution_count": null,
288 "metadata": {},
289 "outputs": [],
290 "source": [
291 "line.x, line.y = [dates, dates_new], [security_1, security_2]"
292 ]
293 },
294 {
295 "cell_type": "markdown",
296 "metadata": {},
297 "source": [
298 "### Coloring Lines according to data"
299 ]
300 },
301 {
302 "cell_type": "markdown",
303 "metadata": {},
304 "source": [
305 "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."
306 ]
307 },
308 {
309 "cell_type": "code",
310 "execution_count": null,
311 "metadata": {},
312 "outputs": [],
313 "source": [
314 "fig = plt.figure()\n",
315 "axes_options = {'x': {'label': 'Date'}, \n",
316 " 'y': {'label': 'Security 1'},\n",
317 " 'color' : {'visible': False}}\n",
318 "# add a custom color scale to color the lines\n",
319 "plt.scales(scales={'color': ColorScale(colors=['Red', 'Green'])})"
320 ]
321 },
322 {
323 "cell_type": "code",
324 "execution_count": null,
325 "metadata": {},
326 "outputs": [],
327 "source": [
328 "dates_color = date_range(start='06-01-2007', periods=150)"
329 ]
330 },
331 {
332 "cell_type": "code",
333 "execution_count": null,
334 "metadata": {},
335 "outputs": [],
336 "source": [
337 "securities = 100. + np.cumsum(np.random.randn(150, 10), axis=0)\n",
338 "# we generate 10 random price series and 10 random positions\n",
339 "positions = np.random.randint(0, 2, size=10)"
340 ]
341 },
342 {
343 "cell_type": "code",
344 "execution_count": null,
345 "metadata": {},
346 "outputs": [],
347 "source": [
348 "# We pass the color scale and the color data to the plot method\n",
349 "line = plt.plot(dates_color, securities.T, color=positions, \n",
350 " axes_options=axes_options)\n",
351 "fig"
352 ]
353 },
354 {
355 "cell_type": "markdown",
356 "metadata": {},
357 "source": [
358 "We can also reset the colors of the Line to their defaults by setting the `color` attribute to `None`."
359 ]
360 },
361 {
362 "cell_type": "code",
363 "execution_count": null,
364 "metadata": {},
365 "outputs": [],
366 "source": [
367 "line.color = None"
368 ]
369 },
370 {
371 "cell_type": "markdown",
372 "metadata": {},
373 "source": [
374 "## Patches"
375 ]
376 },
377 {
378 "cell_type": "markdown",
379 "metadata": {},
380 "source": [
381 "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`"
382 ]
383 },
384 {
385 "cell_type": "code",
386 "execution_count": null,
387 "metadata": {},
388 "outputs": [],
389 "source": [
390 "fig = plt.figure(animation_duration=1000)\n",
391 "patch = plt.plot([],[],\n",
392 " fill_colors=['orange', 'blue', 'red'],\n",
393 " fill='inside',\n",
394 " axes_options={'x': {'visible': False}, 'y': {'visible': False}},\n",
395 " stroke_width=10,\n",
396 " close_path=True,\n",
397 " display_legend=True)\n",
398 "\n",
399 "patch.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",
400 "patch.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",
401 "fig"
402 ]
403 },
404 {
405 "cell_type": "code",
406 "execution_count": null,
407 "metadata": {},
408 "outputs": [],
409 "source": [
410 "patch.opacities = [0.1, 0.2]"
411 ]
412 },
413 {
414 "cell_type": "code",
415 "execution_count": null,
416 "metadata": {},
417 "outputs": [],
418 "source": [
419 "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]]"
420 ]
421 },
422 {
423 "cell_type": "code",
424 "execution_count": null,
425 "metadata": {},
426 "outputs": [],
427 "source": [
428 "patch.close_path = False"
429 ]
430 }
431 ],
432 "metadata": {
433 "anaconda-cloud": {},
434 "kernelspec": {
435 "display_name": "Python 3",
436 "language": "python",
437 "name": "python3"
438 },
439 "language_info": {
440 "codemirror_mode": {
441 "name": "ipython",
442 "version": 3
443 },
444 "file_extension": ".py",
445 "mimetype": "text/x-python",
446 "name": "python",
447 "nbconvert_exporter": "python",
448 "pygments_lexer": "ipython3",
449 "version": "3.7.6"
450 }
451 },
452 "nbformat": 4,
453 "nbformat_minor": 1
454 }