Debian Codegraph @

bqplot/examples/Advanced Plotting/Animations.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "### Animations of marks and axes can be enabled by setting 'animation_duration' property of the figure"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "## Line Animations"
15 ]
16 },
17 {
18 "cell_type": "code",
19 "execution_count": null,
20 "metadata": {},
21 "outputs": [],
22 "source": [
23 "import numpy as np\n",
24 "import bqplot.pyplot as plt\n",
25 "from bqplot import LinearScale, Axis, Lines, Bars, Figure"
26 ]
27 },
28 {
29 "cell_type": "code",
30 "execution_count": null,
31 "metadata": {},
32 "outputs": [],
33 "source": [
34 "axes_options = {'x': {'label': 'x'}, 'y': {'label': 'y'}}"
35 ]
36 },
37 {
38 "cell_type": "code",
39 "execution_count": null,
40 "metadata": {},
41 "outputs": [],
42 "source": [
43 "x = np.arange(100)\n",
44 "y = np.cumsum(np.random.randn(2, 100), axis=1) #two random walks\n",
45 "\n",
46 "fig = plt.figure(animation_duration=1000)\n",
47 "lines = plt.plot(x=x, y=y, colors=['red', 'green'], axes_options=axes_options)\n",
48 "fig"
49 ]
50 },
51 {
52 "cell_type": "code",
53 "execution_count": null,
54 "metadata": {},
55 "outputs": [],
56 "source": [
57 "# update data of the line mark\n",
58 "lines.y = np.cumsum(np.random.randn(2, 100), axis=1)"
59 ]
60 },
61 {
62 "cell_type": "markdown",
63 "metadata": {},
64 "source": [
65 "## Scatter Animations"
66 ]
67 },
68 {
69 "cell_type": "code",
70 "execution_count": null,
71 "metadata": {},
72 "outputs": [],
73 "source": [
74 "fig = plt.figure(animation_duration=1000)\n",
75 "x, y = np.random.rand(2, 20)\n",
76 "scatt = plt.scatter(x, y, colors=['blue'], axes_options=axes_options)\n",
77 "fig"
78 ]
79 },
80 {
81 "cell_type": "code",
82 "execution_count": null,
83 "metadata": {},
84 "outputs": [],
85 "source": [
86 "#data updates\n",
87 "scatt.x = np.random.rand(20) * 10\n",
88 "scatt.y = np.random.rand(20)"
89 ]
90 },
91 {
92 "cell_type": "markdown",
93 "metadata": {
94 "collapsed": true
95 },
96 "source": [
97 "## Pie Animations"
98 ]
99 },
100 {
101 "cell_type": "code",
102 "execution_count": null,
103 "metadata": {},
104 "outputs": [],
105 "source": [
106 "data = np.random.rand(6)\n",
107 "\n",
108 "fig = plt.figure(animation_duration=1000)\n",
109 "pie = plt.pie(data, radius=180, sort=False, display_labels='outside', display_values=True,\n",
110 " values_format='.0%', labels=list('ABCDEFGHIJ'))\n",
111 "fig"
112 ]
113 },
114 {
115 "cell_type": "code",
116 "execution_count": null,
117 "metadata": {},
118 "outputs": [],
119 "source": [
120 "pie.sizes = np.random.rand(8)"
121 ]
122 },
123 {
124 "cell_type": "code",
125 "execution_count": null,
126 "metadata": {},
127 "outputs": [],
128 "source": [
129 "pie.sort = True"
130 ]
131 },
132 {
133 "cell_type": "code",
134 "execution_count": null,
135 "metadata": {},
136 "outputs": [],
137 "source": [
138 "#make pie a donut\n",
139 "with pie.hold_sync():\n",
140 " pie.radius = 180\n",
141 " pie.inner_radius = 120"
142 ]
143 },
144 {
145 "cell_type": "markdown",
146 "metadata": {},
147 "source": [
148 "## Bar animations"
149 ]
150 },
151 {
152 "cell_type": "code",
153 "execution_count": null,
154 "metadata": {},
155 "outputs": [],
156 "source": [
157 "n = 10\n",
158 "x = list('ABCDEFGHIJ')\n",
159 "y1, y2 = np.random.rand(2, n)"
160 ]
161 },
162 {
163 "cell_type": "code",
164 "execution_count": null,
165 "metadata": {},
166 "outputs": [],
167 "source": [
168 "fig = plt.figure(animation_duration=1000)\n",
169 "bar = plt.bar(x, [y1, y2], padding=0.2, type='grouped')\n",
170 "fig"
171 ]
172 },
173 {
174 "cell_type": "code",
175 "execution_count": null,
176 "metadata": {},
177 "outputs": [],
178 "source": [
179 "y1, y2 = np.random.rand(2, n)\n",
180 "bar.y = [y1, y2]"
181 ]
182 },
183 {
184 "cell_type": "markdown",
185 "metadata": {},
186 "source": [
187 "## Multiple Mark Animations (using the object model)"
188 ]
189 },
190 {
191 "cell_type": "code",
192 "execution_count": null,
193 "metadata": {},
194 "outputs": [],
195 "source": [
196 "xs = LinearScale()\n",
197 "ys1 = LinearScale()\n",
198 "ys2 = LinearScale()\n",
199 "\n",
200 "x = np.arange(20)\n",
201 "y = np.cumsum(np.random.randn(20))\n",
202 "y1 = np.random.rand(20)\n",
203 "\n",
204 "line = Lines(x=x, y=y, scales={'x': xs, 'y': ys1}, colors=['magenta'], marker='square')\n",
205 "bar = Bars(x=x, y=y1, scales={'x': xs, 'y': ys2}, colorpadding=0.2, colors=['steelblue'])\n",
206 "\n",
207 "xax = Axis(scale=xs, label='x', grid_lines='solid')\n",
208 "yax1 = Axis(scale=ys1, orientation='vertical', tick_format='0.1f', label='y', grid_lines='solid')\n",
209 "yax2 = Axis(scale=ys2, orientation='vertical', side='right', tick_format='0.0%', label='y1', grid_lines='none')\n",
210 "\n",
211 "Figure(marks=[bar, line], axes=[xax, yax1, yax2], animation_duration=1000)"
212 ]
213 },
214 {
215 "cell_type": "code",
216 "execution_count": null,
217 "metadata": {},
218 "outputs": [],
219 "source": [
220 "# update mark data\n",
221 "line.y = np.cumsum(np.random.randn(20))\n",
222 "bar.y = np.random.rand(20)"
223 ]
224 }
225 ],
226 "metadata": {
227 "anaconda-cloud": {},
228 "kernelspec": {
229 "display_name": "Python 3",
230 "language": "python",
231 "name": "python3"
232 },
233 "language_info": {
234 "codemirror_mode": {
235 "name": "ipython",
236 "version": 3
237 },
238 "file_extension": ".py",
239 "mimetype": "text/x-python",
240 "name": "python",
241 "nbconvert_exporter": "python",
242 "pygments_lexer": "ipython3",
243 "version": "3.7.6"
244 }
245 },
246 "nbformat": 4,
247 "nbformat_minor": 1
248 }