Debian Codegraph @

bqplot/examples/Tutorials/Object Model.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Object Model"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "`bqplot` is based on **Grammar of Graphics** paradigm. The *Object Model* in `bqplot` gives the user the full flexibility to build custom plots. This means the API is verbose but fully customizable.\n",
15 "\n",
16 "The following are the steps to build a `Figure` in `bqplot` using the Object Model:\n",
17 "\n",
18 "* Build the scales for `x` and `y` quantities using the `Scale` classes (Scales map the data into pixels in the figure)\n",
19 "* Build the marks using the `Mark` classes. Marks represent the core plotting objects (lines, scatter, bars, pies etc.). Marks take the scale objects created in step 1 as arguments\n",
20 "* Build the `axes` for `x` and `y` scales\n",
21 "* Finally create a figure using `Figure` class. `Figure` takes marks and axes as inputs. `Figure` object is a widget (it inherits from `DOMWidget`) and can be rendered like any other jupyter widget\n",
22 "\n",
23 "Let's look a simple example to understand these concepts:"
24 ]
25 },
26 {
27 "cell_type": "code",
28 "execution_count": null,
29 "metadata": {},
30 "outputs": [],
31 "source": [
32 "from bqplot import (LinearScale, Axis, Figure, OrdinalScale, \n",
33 " LinearScale, Bars, Lines, Scatter)"
34 ]
35 },
36 {
37 "cell_type": "code",
38 "execution_count": null,
39 "metadata": {},
40 "outputs": [],
41 "source": [
42 "# first, let's create two vectors x and y to plot using a Lines mark\n",
43 "import numpy as np\n",
44 "x = np.linspace(-10, 10, 100)\n",
45 "y = np.sin(x)\n",
46 "\n",
47 "# 1. Create the scales\n",
48 "xs = LinearScale()\n",
49 "ys = LinearScale()\n",
50 "\n",
51 "# 2. Create the axes for x and y\n",
52 "xax = Axis(scale=xs, label='X')\n",
53 "yax = Axis(scale=ys, orientation='vertical', label='Y')\n",
54 "\n",
55 "# 3. Create a Lines mark by passing in the scales\n",
56 "# note that Lines object is stored in `line` which can be used later to update the plot\n",
57 "line = Lines(x=x, y=y, scales={'x': xs, 'y': ys})\n",
58 "\n",
59 "# 4. Create a Figure object by assembling marks and axes\n",
60 "fig = Figure(marks=[line], axes=[xax, yax], title='Simple Line Chart')\n",
61 "\n",
62 "# 5. Render the figure using display or just as is\n",
63 "fig"
64 ]
65 },
66 {
67 "cell_type": "markdown",
68 "metadata": {},
69 "source": [
70 "For creating other marks (like scatter, pie, bars, etc.), only step 3 needs to be changed. Lets look a simple example to create a bar chart:"
71 ]
72 },
73 {
74 "cell_type": "code",
75 "execution_count": null,
76 "metadata": {},
77 "outputs": [],
78 "source": [
79 "# first, let's create two vectors x and y to plot a bar chart\n",
80 "x = list('ABCDE')\n",
81 "y = np.random.rand(5)\n",
82 "\n",
83 "# 1. Create the scales\n",
84 "xs = OrdinalScale() # note the use of ordinal scale to represent categorical data\n",
85 "ys = LinearScale()\n",
86 "\n",
87 "# 2. Create the axes for x and y\n",
88 "xax = Axis(scale=xs, label='X', grid_lines='none') # no grid lines needed for x\n",
89 "yax = Axis(scale=ys, orientation='vertical', label='Y', tick_format='.0%') # note the use of tick_format to format ticks\n",
90 "\n",
91 "# 3. Create a Bars mark by passing in the scales\n",
92 "# note that Bars object is stored in `bar` object which can be used later to update the plot\n",
93 "bar = Bars(x=x, y=y, scales={'x': xs, 'y': ys}, padding=.2)\n",
94 "\n",
95 "# 4. Create a Figure object by assembling marks and axes\n",
96 "Figure(marks=[bar], axes=[xax, yax], title='Simple Bar Chart')"
97 ]
98 },
99 {
100 "cell_type": "markdown",
101 "metadata": {},
102 "source": [
103 "Multiple marks can be rendered in a figure. It's as easy as passing a list of marks when constructing the `Figure` object"
104 ]
105 },
106 {
107 "cell_type": "code",
108 "execution_count": null,
109 "metadata": {},
110 "outputs": [],
111 "source": [
112 "# first, let's create two vectors x and y\n",
113 "import numpy as np\n",
114 "x = np.linspace(-10, 10, 25)\n",
115 "y = 3 * x + 5\n",
116 "y_noise = y + 10 * np.random.randn(25) # add some random noise to y\n",
117 "\n",
118 "# 1. Create the scales\n",
119 "xs = LinearScale()\n",
120 "ys = LinearScale()\n",
121 "\n",
122 "# 2. Create the axes for x and y\n",
123 "xax = Axis(scale=xs, label='X')\n",
124 "yax = Axis(scale=ys, orientation='vertical', label='Y')\n",
125 "\n",
126 "# 3. Create a Lines and Scatter marks by passing in the scales\n",
127 "# additional attributes (stroke_width, colors etc.) can be passed as attributes to the mark objects as needed\n",
128 "line = Lines(x=x, y=y, scales={'x': xs, 'y': ys}, colors=['green'], stroke_width=3)\n",
129 "scatter = Scatter(x=x, y=y_noise, scales={'x': xs, 'y': ys}, colors=['red'], stroke='black')\n",
130 "\n",
131 "# 4. Create a Figure object by assembling marks and axes\n",
132 "# pass both the marks (line and scatter) as a list to the marks attribute\n",
133 "Figure(marks=[line, scatter], axes=[xax, yax], title='Scatter and Line')"
134 ]
135 },
136 {
137 "cell_type": "markdown",
138 "metadata": {},
139 "source": [
140 "This introduction should be sufficient to get started with the *Object Model*. For detailed examples of rendering individual marks (with all their attributes) please look at the *Object Model* [examples](../Marks/Object Model)"
141 ]
142 }
143 ],
144 "metadata": {
145 "kernelspec": {
146 "display_name": "Python 3",
147 "language": "python",
148 "name": "python3"
149 },
150 "language_info": {
151 "codemirror_mode": {
152 "name": "ipython",
153 "version": 3
154 },
155 "file_extension": ".py",
156 "mimetype": "text/x-python",
157 "name": "python",
158 "nbconvert_exporter": "python",
159 "pygments_lexer": "ipython3",
160 "version": "3.7.6"
161 }
162 },
163 "nbformat": 4,
164 "nbformat_minor": 1
165 }