Debian Codegraph @

bqplot/examples/Marks/Pyplot/Pie.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {
7 "scrolled": true
8 },
9 "outputs": [],
10 "source": [
11 "import numpy as np\n",
12 "import bqplot.pyplot as plt\n",
13 "import string"
14 ]
15 },
16 {
17 "cell_type": "markdown",
18 "metadata": {},
19 "source": [
20 "## Basic Pie Chart"
21 ]
22 },
23 {
24 "cell_type": "code",
25 "execution_count": null,
26 "metadata": {
27 "scrolled": false
28 },
29 "outputs": [],
30 "source": [
31 "data = np.random.rand(3)\n",
32 "\n",
33 "fig = plt.figure(animation_duration=1000)\n",
34 "pie = plt.pie(data, display_labels='outside', labels=list(string.ascii_uppercase))\n",
35 "fig"
36 ]
37 },
38 {
39 "cell_type": "markdown",
40 "metadata": {},
41 "source": [
42 "### Update Data"
43 ]
44 },
45 {
46 "cell_type": "code",
47 "execution_count": null,
48 "metadata": {
49 "scrolled": true
50 },
51 "outputs": [],
52 "source": [
53 "n = np.random.randint(1, 10)\n",
54 "pie.sizes = np.random.rand(n)"
55 ]
56 },
57 {
58 "cell_type": "markdown",
59 "metadata": {},
60 "source": [
61 "### Display Values"
62 ]
63 },
64 {
65 "cell_type": "code",
66 "execution_count": null,
67 "metadata": {
68 "scrolled": true
69 },
70 "outputs": [],
71 "source": [
72 "with pie.hold_sync():\n",
73 " pie.display_values = True\n",
74 " pie.values_format = '.1f'"
75 ]
76 },
77 {
78 "cell_type": "markdown",
79 "metadata": {},
80 "source": [
81 "### Enable sort "
82 ]
83 },
84 {
85 "cell_type": "code",
86 "execution_count": null,
87 "metadata": {
88 "scrolled": true
89 },
90 "outputs": [],
91 "source": [
92 "pie.sort = True"
93 ]
94 },
95 {
96 "cell_type": "markdown",
97 "metadata": {},
98 "source": [
99 "### Set different styles for selected slices"
100 ]
101 },
102 {
103 "cell_type": "code",
104 "execution_count": null,
105 "metadata": {
106 "scrolled": true
107 },
108 "outputs": [],
109 "source": [
110 "pie.selected_style = {'opacity': 1, 'stroke': 'white', 'stroke-width': 2}\n",
111 "pie.unselected_style = {'opacity': 0.2}\n",
112 "pie.selected = [1]"
113 ]
114 },
115 {
116 "cell_type": "code",
117 "execution_count": null,
118 "metadata": {
119 "scrolled": true
120 },
121 "outputs": [],
122 "source": [
123 "pie.selected = None"
124 ]
125 },
126 {
127 "cell_type": "markdown",
128 "metadata": {},
129 "source": [
130 "For more on piechart interactions, see the [Mark Interactions notebook](../Interactions/Mark Interactions.ipynb)"
131 ]
132 },
133 {
134 "cell_type": "markdown",
135 "metadata": {},
136 "source": [
137 "### Modify label styling"
138 ]
139 },
140 {
141 "cell_type": "code",
142 "execution_count": null,
143 "metadata": {
144 "scrolled": true
145 },
146 "outputs": [],
147 "source": [
148 "pie.label_color = 'Red'\n",
149 "pie.font_size = '20px'\n",
150 "pie.font_weight = 'bold'"
151 ]
152 },
153 {
154 "cell_type": "markdown",
155 "metadata": {},
156 "source": [
157 "### Update pie shape and style"
158 ]
159 },
160 {
161 "cell_type": "code",
162 "execution_count": null,
163 "metadata": {
164 "scrolled": false
165 },
166 "outputs": [],
167 "source": [
168 "fig1 = plt.figure(animation_duration=1000)\n",
169 "pie1 = plt.pie(np.random.rand(6), inner_radius=0.05)\n",
170 "fig1"
171 ]
172 },
173 {
174 "cell_type": "markdown",
175 "metadata": {},
176 "source": [
177 "### Change pie dimensions"
178 ]
179 },
180 {
181 "cell_type": "code",
182 "execution_count": null,
183 "metadata": {
184 "scrolled": true
185 },
186 "outputs": [],
187 "source": [
188 "# As of now, the radius sizes are absolute, in pixels\n",
189 "with pie1.hold_sync():\n",
190 " pie1.radius = 150\n",
191 " pie1.inner_radius = 100"
192 ]
193 },
194 {
195 "cell_type": "code",
196 "execution_count": null,
197 "metadata": {
198 "scrolled": true
199 },
200 "outputs": [],
201 "source": [
202 "# Angles are in radians, 0 being the top vertical\n",
203 "with pie1.hold_sync():\n",
204 " pie1.start_angle = -90\n",
205 " pie1.end_angle = 90"
206 ]
207 },
208 {
209 "cell_type": "markdown",
210 "metadata": {},
211 "source": [
212 "### Move the pie around\n",
213 "`x` and `y` attributes control the position of the pie in the figure.\n",
214 "If no scales are passed for `x` and `y`, they are taken in absolute \n",
215 "figure coordinates, between 0 and 1."
216 ]
217 },
218 {
219 "cell_type": "code",
220 "execution_count": null,
221 "metadata": {
222 "scrolled": true
223 },
224 "outputs": [],
225 "source": [
226 "pie1.y = 0.1\n",
227 "pie1.x = 0.6\n",
228 "pie1.radius = 180"
229 ]
230 },
231 {
232 "cell_type": "markdown",
233 "metadata": {},
234 "source": [
235 "### Change slice styles\n",
236 "Pie slice colors cycle through the `colors` and `opacities` attribute, as the `Lines` Mark."
237 ]
238 },
239 {
240 "cell_type": "code",
241 "execution_count": null,
242 "metadata": {
243 "scrolled": false
244 },
245 "outputs": [],
246 "source": [
247 "pie1.stroke = 'brown'\n",
248 "pie1.colors = ['orange', 'darkviolet']\n",
249 "pie1.opacities = [.1, 1]\n",
250 "fig1"
251 ]
252 },
253 {
254 "cell_type": "markdown",
255 "metadata": {},
256 "source": [
257 "## Represent an additional dimension using Color"
258 ]
259 },
260 {
261 "cell_type": "markdown",
262 "metadata": {},
263 "source": [
264 "The `Pie` allows for its colors to be determined by data, that is passed to the `color` attribute. \n",
265 "A `ColorScale` with the desired color scheme must also be passed."
266 ]
267 },
268 {
269 "cell_type": "code",
270 "execution_count": null,
271 "metadata": {
272 "scrolled": true
273 },
274 "outputs": [],
275 "source": [
276 "from bqplot import ColorScale, ColorAxis\n",
277 "\n",
278 "n = 7\n",
279 "size_data = np.random.rand(n)\n",
280 "color_data = np.random.randn(n)\n",
281 "\n",
282 "fig2 = plt.figure()\n",
283 "plt.scales(scales={'color': ColorScale(scheme='Reds')})\n",
284 "pie2 = plt.pie(size_data, color=color_data)\n",
285 "fig2"
286 ]
287 }
288 ],
289 "metadata": {
290 "kernelspec": {
291 "display_name": "Python 3",
292 "language": "python",
293 "name": "python3"
294 },
295 "language_info": {
296 "codemirror_mode": {
297 "name": "ipython",
298 "version": 3
299 },
300 "file_extension": ".py",
301 "mimetype": "text/x-python",
302 "name": "python",
303 "nbconvert_exporter": "python",
304 "pygments_lexer": "ipython3",
305 "version": "3.7.6"
306 }
307 },
308 "nbformat": 4,
309 "nbformat_minor": 1
310 }