Debian Codegraph @

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

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {
7 "collapsed": true,
8 "deletable": true,
9 "editable": true,
10 "scrolled": true
11 },
12 "outputs": [],
13 "source": [
14 "from bqplot import Pie, Figure\n",
15 "import numpy as np\n",
16 "import string"
17 ]
18 },
19 {
20 "cell_type": "markdown",
21 "metadata": {
22 "deletable": true,
23 "editable": true
24 },
25 "source": [
26 "## Basic Pie Chart"
27 ]
28 },
29 {
30 "cell_type": "code",
31 "execution_count": null,
32 "metadata": {
33 "collapsed": false,
34 "deletable": true,
35 "editable": true,
36 "scrolled": true
37 },
38 "outputs": [],
39 "source": [
40 "data = np.random.rand(3)\n",
41 "pie = Pie(sizes=data, display_labels='outside', labels=list(string.ascii_uppercase))\n",
42 "fig = Figure(marks=[pie], animation_duration=1000)\n",
43 "fig"
44 ]
45 },
46 {
47 "cell_type": "markdown",
48 "metadata": {
49 "deletable": true,
50 "editable": true
51 },
52 "source": [
53 "### Update Data"
54 ]
55 },
56 {
57 "cell_type": "code",
58 "execution_count": null,
59 "metadata": {
60 "collapsed": true,
61 "deletable": true,
62 "editable": true,
63 "scrolled": true
64 },
65 "outputs": [],
66 "source": [
67 "n = np.random.randint(1, 10)\n",
68 "pie.sizes = np.random.rand(n)"
69 ]
70 },
71 {
72 "cell_type": "markdown",
73 "metadata": {
74 "deletable": true,
75 "editable": true
76 },
77 "source": [
78 "### Display Values"
79 ]
80 },
81 {
82 "cell_type": "code",
83 "execution_count": null,
84 "metadata": {
85 "collapsed": true,
86 "deletable": true,
87 "editable": true,
88 "scrolled": true
89 },
90 "outputs": [],
91 "source": [
92 "with pie.hold_sync():\n",
93 " pie.display_values = True\n",
94 " pie.values_format = '.1f'"
95 ]
96 },
97 {
98 "cell_type": "markdown",
99 "metadata": {
100 "deletable": true,
101 "editable": true
102 },
103 "source": [
104 "### Enable sort "
105 ]
106 },
107 {
108 "cell_type": "code",
109 "execution_count": null,
110 "metadata": {
111 "collapsed": true,
112 "deletable": true,
113 "editable": true,
114 "scrolled": true
115 },
116 "outputs": [],
117 "source": [
118 "pie.sort = True"
119 ]
120 },
121 {
122 "cell_type": "markdown",
123 "metadata": {
124 "deletable": true,
125 "editable": true
126 },
127 "source": [
128 "### Set different styles for selected slices"
129 ]
130 },
131 {
132 "cell_type": "code",
133 "execution_count": null,
134 "metadata": {
135 "collapsed": true,
136 "deletable": true,
137 "editable": true,
138 "scrolled": true
139 },
140 "outputs": [],
141 "source": [
142 "pie.selected_style = {'opacity': 1, 'stroke': 'white', 'stroke-width': 2}\n",
143 "pie.unselected_style = {'opacity': 0.2}\n",
144 "pie.selected = [1]"
145 ]
146 },
147 {
148 "cell_type": "code",
149 "execution_count": null,
150 "metadata": {
151 "collapsed": true,
152 "deletable": true,
153 "editable": true,
154 "scrolled": true
155 },
156 "outputs": [],
157 "source": [
158 "pie.selected = None"
159 ]
160 },
161 {
162 "cell_type": "markdown",
163 "metadata": {
164 "deletable": true,
165 "editable": true
166 },
167 "source": [
168 "For more on piechart interactions, see the [Mark Interactions notebook](../Interactions/Mark Interactions.ipynb)"
169 ]
170 },
171 {
172 "cell_type": "markdown",
173 "metadata": {
174 "deletable": true,
175 "editable": true
176 },
177 "source": [
178 "### Modify label styling"
179 ]
180 },
181 {
182 "cell_type": "code",
183 "execution_count": null,
184 "metadata": {
185 "collapsed": true,
186 "deletable": true,
187 "editable": true,
188 "scrolled": true
189 },
190 "outputs": [],
191 "source": [
192 "pie.label_color = 'Red'\n",
193 "pie.font_size = '20px'\n",
194 "pie.font_weight = 'bold'"
195 ]
196 },
197 {
198 "cell_type": "markdown",
199 "metadata": {
200 "deletable": true,
201 "editable": true
202 },
203 "source": [
204 "### Update pie shape and style"
205 ]
206 },
207 {
208 "cell_type": "code",
209 "execution_count": null,
210 "metadata": {
211 "collapsed": false,
212 "deletable": true,
213 "editable": true,
214 "scrolled": true
215 },
216 "outputs": [],
217 "source": [
218 "pie1 = Pie(sizes=np.random.rand(6), inner_radius=0.05)\n",
219 "fig1 = Figure(marks=[pie1], animation_duration=1000)\n",
220 "fig1"
221 ]
222 },
223 {
224 "cell_type": "markdown",
225 "metadata": {
226 "deletable": true,
227 "editable": true
228 },
229 "source": [
230 "### Change pie dimensions"
231 ]
232 },
233 {
234 "cell_type": "code",
235 "execution_count": null,
236 "metadata": {
237 "collapsed": true,
238 "deletable": true,
239 "editable": true,
240 "scrolled": true
241 },
242 "outputs": [],
243 "source": [
244 "# As of now, the radius sizes are absolute, in pixels\n",
245 "with pie1.hold_sync():\n",
246 " pie1.radius = 150\n",
247 " pie1.inner_radius = 100"
248 ]
249 },
250 {
251 "cell_type": "code",
252 "execution_count": null,
253 "metadata": {
254 "collapsed": true,
255 "deletable": true,
256 "editable": true,
257 "scrolled": true
258 },
259 "outputs": [],
260 "source": [
261 "# Angles are in radians, 0 being the top vertical\n",
262 "with pie1.hold_sync():\n",
263 " pie1.start_angle = -90\n",
264 " pie1.end_angle = 90"
265 ]
266 },
267 {
268 "cell_type": "markdown",
269 "metadata": {
270 "deletable": true,
271 "editable": true
272 },
273 "source": [
274 "### Move the pie around\n",
275 "`x` and `y` attributes control the position of the pie in the figure.\n",
276 "If no scales are passed for `x` and `y`, they are taken in absolute \n",
277 "figure coordinates, between 0 and 1."
278 ]
279 },
280 {
281 "cell_type": "code",
282 "execution_count": null,
283 "metadata": {
284 "collapsed": true,
285 "deletable": true,
286 "editable": true,
287 "scrolled": true
288 },
289 "outputs": [],
290 "source": [
291 "pie1.y = 0.1\n",
292 "pie1.x = 0.6\n",
293 "pie1.radius = 180"
294 ]
295 },
296 {
297 "cell_type": "markdown",
298 "metadata": {
299 "deletable": true,
300 "editable": true
301 },
302 "source": [
303 "### Change slice styles\n",
304 "Pie slice colors cycle through the `colors` and `opacities` attribute, as the `Lines` Mark."
305 ]
306 },
307 {
308 "cell_type": "code",
309 "execution_count": null,
310 "metadata": {
311 "collapsed": false,
312 "deletable": true,
313 "editable": true,
314 "scrolled": true
315 },
316 "outputs": [],
317 "source": [
318 "pie1.stroke = 'brown'\n",
319 "pie1.colors = ['orange', 'darkviolet']\n",
320 "pie1.opacities = [.1, 1]\n",
321 "fig1"
322 ]
323 },
324 {
325 "cell_type": "markdown",
326 "metadata": {
327 "deletable": true,
328 "editable": true
329 },
330 "source": [
331 "## Represent an additional dimension using Color"
332 ]
333 },
334 {
335 "cell_type": "markdown",
336 "metadata": {
337 "deletable": true,
338 "editable": true
339 },
340 "source": [
341 "The `Pie` allows for its colors to be determined by data, that is passed to the `color` attribute. \n",
342 "A `ColorScale` with the desired color scheme must also be passed."
343 ]
344 },
345 {
346 "cell_type": "code",
347 "execution_count": null,
348 "metadata": {
349 "collapsed": false,
350 "deletable": true,
351 "editable": true,
352 "scrolled": true
353 },
354 "outputs": [],
355 "source": [
356 "from bqplot import ColorScale, ColorAxis\n",
357 "\n",
358 "Nslices = 7\n",
359 "size_data = np.random.rand(Nslices)\n",
360 "color_data = np.random.randn(Nslices)\n",
361 "\n",
362 "sc = ColorScale(scheme='Reds')\n",
363 "# The ColorAxis gives a visual representation of its ColorScale\n",
364 "ax = ColorAxis(scale=sc)\n",
365 "\n",
366 "pie2 = Pie(sizes=size_data, scales={'color': sc}, color=color_data)\n",
367 "Figure(marks=[pie2], axes=[ax])"
368 ]
369 },
370 {
371 "cell_type": "markdown",
372 "metadata": {
373 "deletable": true,
374 "editable": true
375 },
376 "source": [
377 "## Position the Pie using custom scales"
378 ]
379 },
380 {
381 "cell_type": "markdown",
382 "metadata": {
383 "deletable": true,
384 "editable": true
385 },
386 "source": [
387 "Pies can be positioned, via the `x` and `y` attributes, \n",
388 "using either absolute figure scales or custom 'x' or 'y' scales"
389 ]
390 },
391 {
392 "cell_type": "code",
393 "execution_count": null,
394 "metadata": {
395 "collapsed": false,
396 "deletable": true,
397 "editable": true,
398 "scrolled": true
399 },
400 "outputs": [],
401 "source": [
402 "from datetime import datetime\n",
403 "from bqplot.traits import convert_to_date\n",
404 "from bqplot import DateScale, LinearScale, Axis\n",
405 "\n",
406 "avg_precipitation_days = [(d/30., 1-d/30.) for d in [2, 3, 4, 6, 12, 17, 23, 22, 15, 4, 1, 1]]\n",
407 "temperatures = [9, 12, 16, 20, 22, 23, 22, 22, 22, 20, 15, 11]\n",
408 "\n",
409 "dates = [datetime(2010, k, 1) for k in range(1, 13)]\n",
410 "\n",
411 "sc_x = DateScale()\n",
412 "sc_y = LinearScale()\n",
413 "ax_x = Axis(scale=sc_x, label='Month', tick_format='%b')\n",
414 "ax_y = Axis(scale=sc_y, orientation='vertical', label='Average Temperature')\n",
415 "\n",
416 "pies = [Pie(sizes=precipit, x=date, y=temp,display_labels='none',\n",
417 " scales={'x': sc_x, 'y': sc_y}, radius=30., stroke='navy',\n",
418 " apply_clip=False, colors=['navy', 'navy'], opacities=[1, .1]) \n",
419 " for precipit, date, temp in zip(avg_precipitation_days, dates, temperatures)]\n",
420 "\n",
421 "Figure(title='Kathmandu Precipitation', marks=pies, axes=[ax_x, ax_y],\n",
422 " padding_x=.05, padding_y=.1)"
423 ]
424 }
425 ],
426 "metadata": {
427 "kernelspec": {
428 "display_name": "Python 3",
429 "language": "python",
430 "name": "python3"
431 },
432 "language_info": {
433 "codemirror_mode": {
434 "name": "ipython",
435 "version": 3
436 },
437 "file_extension": ".py",
438 "mimetype": "text/x-python",
439 "name": "python",
440 "nbconvert_exporter": "python",
441 "pygments_lexer": "ipython3",
442 "version": "3.6.4"
443 }
444 },
445 "nbformat": 4,
446 "nbformat_minor": 1
447 }