Debian Codegraph @

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

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "import numpy as np\n",
10 "from bqplot import (OrdinalScale, LinearScale, Bars, \n",
11 " Figure, Axis, ColorScale, ColorAxis, CATEGORY10)"
12 ]
13 },
14 {
15 "cell_type": "code",
16 "execution_count": null,
17 "metadata": {},
18 "outputs": [],
19 "source": [
20 "size = 100\n",
21 "np.random.seed(0)\n",
22 "\n",
23 "x_data = range(size)\n",
24 "y_data = np.random.randn(size)\n",
25 "y_data_2 = np.random.randn(size)\n",
26 "y_data_3 = np.cumsum(np.random.randn(size) * 100.)"
27 ]
28 },
29 {
30 "cell_type": "markdown",
31 "metadata": {},
32 "source": [
33 "## Basic Bar Chart"
34 ]
35 },
36 {
37 "cell_type": "code",
38 "execution_count": null,
39 "metadata": {},
40 "outputs": [],
41 "source": [
42 "x_ord = OrdinalScale()\n",
43 "y_sc = LinearScale()\n",
44 "\n",
45 "bar = Bars(x=np.arange(10), y=np.random.rand(10), scales={'x': x_ord, 'y': y_sc})\n",
46 "ax_x = Axis(scale=x_ord)\n",
47 "ax_y = Axis(scale=y_sc, tick_format='0.2f', orientation='vertical')\n",
48 "\n",
49 "Figure(marks=[bar], axes=[ax_x, ax_y], padding_x=0.025, padding_y=0.025)"
50 ]
51 },
52 {
53 "cell_type": "markdown",
54 "metadata": {},
55 "source": [
56 "## Horizontal Bar Chart"
57 ]
58 },
59 {
60 "cell_type": "markdown",
61 "metadata": {},
62 "source": [
63 "To generate a horizontal bar chart, pass `orientation='horizontal'` to the bar."
64 ]
65 },
66 {
67 "cell_type": "code",
68 "execution_count": null,
69 "metadata": {},
70 "outputs": [],
71 "source": [
72 "x_ord = OrdinalScale()\n",
73 "y_sc = LinearScale()\n",
74 "\n",
75 "bar = Bars(x=np.arange(10), y=np.arange(-5, 5), scales={'x': x_ord, 'y': y_sc},\n",
76 " orientation='horizontal')\n",
77 "ax_x = Axis(scale=x_ord, orientation='vertical')\n",
78 "ax_y = Axis(scale=y_sc, tick_format='0.2f')\n",
79 "\n",
80 "Figure(marks=[bar], axes=[ax_x, ax_y], padding_x=0.025, padding_y=0.025)"
81 ]
82 },
83 {
84 "cell_type": "markdown",
85 "metadata": {},
86 "source": [
87 "## Changing the reference value from which the Bars are drawn"
88 ]
89 },
90 {
91 "cell_type": "code",
92 "execution_count": null,
93 "metadata": {},
94 "outputs": [],
95 "source": [
96 "x_ord = LinearScale()\n",
97 "y_sc = LinearScale()\n",
98 "\n",
99 "bar = Bars(x=x_data[:20], y=np.abs(y_data_2[:20]), scales={'x': x_ord, 'y': y_sc}, base=1.0)\n",
100 "ax_x = Axis(scale=x_ord)\n",
101 "ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n",
102 "\n",
103 "Figure(marks=[bar], axes=[ax_x, ax_y])"
104 ]
105 },
106 {
107 "cell_type": "code",
108 "execution_count": null,
109 "metadata": {},
110 "outputs": [],
111 "source": [
112 "# changing the base\n",
113 "bar.base = 2.0"
114 ]
115 },
116 {
117 "cell_type": "code",
118 "execution_count": null,
119 "metadata": {},
120 "outputs": [],
121 "source": [
122 "bar.align = 'right'"
123 ]
124 },
125 {
126 "cell_type": "markdown",
127 "metadata": {},
128 "source": [
129 "## Bar Chart Properties"
130 ]
131 },
132 {
133 "cell_type": "code",
134 "execution_count": null,
135 "metadata": {},
136 "outputs": [],
137 "source": [
138 "# Increasing the spacing between the bars\n",
139 "x_ord = OrdinalScale()\n",
140 "y_sc = LinearScale()\n",
141 "\n",
142 "bar = Bars(x=x_data[:20], y=y_data[:20], scales={'x': x_ord, 'y': y_sc}, padding=0.2)\n",
143 "ax_x = Axis(scale=x_ord)\n",
144 "ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n",
145 "\n",
146 "Figure(marks=[bar], axes=[ax_x, ax_y])"
147 ]
148 },
149 {
150 "cell_type": "code",
151 "execution_count": null,
152 "metadata": {},
153 "outputs": [],
154 "source": [
155 "# add bar labels\n",
156 "bar.label_display = True\n",
157 "bar.label_display_format = '.1f'\n",
158 "bar.label_font_style = {'fill': 'pink', 'font-size': '10px'}"
159 ]
160 },
161 {
162 "cell_type": "code",
163 "execution_count": null,
164 "metadata": {},
165 "outputs": [],
166 "source": [
167 "# changing basic properties like stroke and opacity\n",
168 "bar.stroke = 'orange'\n",
169 "bar.opacities = [0.5, 0.2]"
170 ]
171 },
172 {
173 "cell_type": "code",
174 "execution_count": null,
175 "metadata": {},
176 "outputs": [],
177 "source": [
178 "bar.orientation = 'horizontal'\n",
179 "ax_x.orientation = 'vertical'\n",
180 "ax_y.orientation = 'horizontal'"
181 ]
182 },
183 {
184 "cell_type": "markdown",
185 "metadata": {},
186 "source": [
187 "## Stacked Bar Chart for 2-d data"
188 ]
189 },
190 {
191 "cell_type": "code",
192 "execution_count": null,
193 "metadata": {},
194 "outputs": [],
195 "source": [
196 "x_ord = OrdinalScale()\n",
197 "y_sc = LinearScale()\n",
198 "\n",
199 "bar = Bars(x=x_data, y=[y_data[:20], y_data_2[:20]], \n",
200 " scales={'x': x_ord, 'y': y_sc}, padding=0.2,\n",
201 " colors=CATEGORY10)\n",
202 "ax_x = Axis(scale=x_ord)\n",
203 "ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n",
204 "\n",
205 "Figure(marks=[bar], axes=[ax_x, ax_y])"
206 ]
207 },
208 {
209 "cell_type": "markdown",
210 "metadata": {},
211 "source": [
212 "## Grouped Bar Chart"
213 ]
214 },
215 {
216 "cell_type": "code",
217 "execution_count": null,
218 "metadata": {},
219 "outputs": [],
220 "source": [
221 "bar.type = 'grouped' # equivalent to saying \n",
222 "# bar = Bars(x=x_data, y=y_data, scales={'x': x_ord, 'y': y_sc}, padding=0.2, type='grouped')"
223 ]
224 },
225 {
226 "cell_type": "code",
227 "execution_count": null,
228 "metadata": {},
229 "outputs": [],
230 "source": [
231 "x_ord = OrdinalScale()\n",
232 "y_sc = LinearScale()\n",
233 "\n",
234 "bar = Bars(x=x_data, y=[y_data[:20], y_data_2[:20]], \n",
235 " scales={'x': x_ord, 'y': y_sc}, padding=0.2, \n",
236 " colors=CATEGORY10, type='stacked', orientation='horizontal')\n",
237 "ax_x = Axis(scale=x_ord, orientation='vertical')\n",
238 "ax_y = Axis(scale=y_sc, tick_format='0.2f')\n",
239 "\n",
240 "Figure(marks=[bar], axes=[ax_x, ax_y])"
241 ]
242 },
243 {
244 "cell_type": "code",
245 "execution_count": null,
246 "metadata": {},
247 "outputs": [],
248 "source": [
249 "bar.type = 'grouped'"
250 ]
251 },
252 {
253 "cell_type": "markdown",
254 "metadata": {},
255 "source": [
256 "## Modifying color mode"
257 ]
258 },
259 {
260 "cell_type": "code",
261 "execution_count": null,
262 "metadata": {},
263 "outputs": [],
264 "source": [
265 "## Color mode has 3 values. 'group', 'element' and 'no_group'. \n",
266 "## 'group' means colors are assigned per group.\n",
267 "## 'element' means colors are assigned per group element.\n",
268 "## 'no_group' means means colors are assigned per bar, discarding the fact that there are groups or stacks\n",
269 "x_ord = OrdinalScale()\n",
270 "y_sc = LinearScale()\n",
271 "\n",
272 "bar = Bars(x=x_data, y=[y_data[:20], y_data_2[:20]],\n",
273 " scales={'x': x_ord, 'y': y_sc}, padding=0.2, \n",
274 " colors=CATEGORY10, color_mode='group')\n",
275 "ax_x = Axis(scale=x_ord)\n",
276 "ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n",
277 "\n",
278 "Figure(marks=[bar], axes=[ax_x, ax_y])"
279 ]
280 },
281 {
282 "cell_type": "code",
283 "execution_count": null,
284 "metadata": {},
285 "outputs": [],
286 "source": [
287 "bar.color_mode = 'element'"
288 ]
289 },
290 {
291 "cell_type": "code",
292 "execution_count": null,
293 "metadata": {},
294 "outputs": [],
295 "source": [
296 "bar.color_mode = 'no_group'"
297 ]
298 },
299 {
300 "cell_type": "code",
301 "execution_count": null,
302 "metadata": {},
303 "outputs": [],
304 "source": [
305 "## for 1-d array for Y.\n",
306 "x_ord = OrdinalScale()\n",
307 "y_sc = LinearScale()\n",
308 "\n",
309 "bar = Bars(x=x_data, y=y_data[:20], scales={'x': x_ord, 'y': y_sc}, padding=0.2,\n",
310 " color_mode='element', labels=['Values'], \n",
311 " display_legend=True)\n",
312 "ax_x = Axis(scale=x_ord)\n",
313 "ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n",
314 "\n",
315 "Figure(marks=[bar], axes=[ax_x, ax_y])"
316 ]
317 },
318 {
319 "cell_type": "markdown",
320 "metadata": {
321 "collapsed": true
322 },
323 "source": [
324 "## Representing additional dimension using Color"
325 ]
326 },
327 {
328 "cell_type": "code",
329 "execution_count": null,
330 "metadata": {},
331 "outputs": [],
332 "source": [
333 "# In this example, the color is just the magnitude of the y data\n",
334 "x_ord = OrdinalScale()\n",
335 "y_sc = LinearScale()\n",
336 "col_sc = ColorScale(scheme='Reds')\n",
337 "\n",
338 "bar = Bars(x=x_data[:20], y=y_data[:20], color=np.abs(y_data[:20]),\n",
339 " scales={'x': x_ord, 'y': y_sc, 'color': col_sc}, padding=0.2)\n",
340 "ax_x = Axis(scale=x_ord)\n",
341 "ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n",
342 "ax_c = ColorAxis(scale=col_sc, tick_format='0.2f')\n",
343 "\n",
344 "margin = dict(top=50, bottom=80, left=50, right=50)\n",
345 "\n",
346 "Figure(marks=[bar], axes=[ax_x, ax_y, ax_c], fig_margin=margin)"
347 ]
348 },
349 {
350 "cell_type": "markdown",
351 "metadata": {},
352 "source": [
353 "## Adding color for 2-d data"
354 ]
355 },
356 {
357 "cell_type": "code",
358 "execution_count": null,
359 "metadata": {},
360 "outputs": [],
361 "source": [
362 "# By default color is applied along the axis=1\n",
363 "x_ord = OrdinalScale()\n",
364 "y_sc = LinearScale()\n",
365 "col_sc = ColorScale(scheme='Reds')\n",
366 "\n",
367 "y_vals = [y_data[:20], y_data_2[:20], y_data_3[:20] / 100.0]\n",
368 "color_data = np.mean(y_vals, axis=1)\n",
369 "\n",
370 "bar = Bars(x=x_data, y=y_vals, color=color_data, \n",
371 " scales={'x': x_ord, 'y': y_sc, 'color': col_sc}, padding=0.2,\n",
372 " labels=['Dim 1', 'Dim 2', 'Dim 3'], display_legend=True)\n",
373 "ax_x = Axis(scale=x_ord)\n",
374 "ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n",
375 "ax_c = ColorAxis(scale=col_sc, tick_format='0.2f')\n",
376 "\n",
377 "margin = dict(top=50, bottom=80, left=50, right=50)\n",
378 "Figure(marks=[bar], axes=[ax_x, ax_y, ax_c], fig_margin=margin)"
379 ]
380 },
381 {
382 "cell_type": "code",
383 "execution_count": null,
384 "metadata": {},
385 "outputs": [],
386 "source": [
387 "# Applying color along the axis=0\n",
388 "x_ord = OrdinalScale()\n",
389 "y_sc = LinearScale()\n",
390 "col_sc = ColorScale(mid=0.0)\n",
391 "\n",
392 "y_vals = [y_data[:20], y_data_2[:20], y_data_3[:20] / 100.0]\n",
393 "color_data = np.mean(y_vals, axis=0)\n",
394 "\n",
395 "bar = Bars(x=x_data, y=y_vals, color=color_data, \n",
396 " scales={'x': x_ord, 'y': y_sc, 'color': col_sc},\n",
397 " padding=0.2, color_mode='group', stroke='orange')\n",
398 "ax_x = Axis(scale=x_ord)\n",
399 "ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n",
400 "ax_c = ColorAxis(scale=col_sc, tick_format='0.1f')\n",
401 "\n",
402 "margin = dict(top=50, bottom=80, left=50, right=50)\n",
403 "Figure(marks=[bar], axes=[ax_x, ax_y, ax_c], fig_margin=margin)"
404 ]
405 }
406 ],
407 "metadata": {
408 "kernelspec": {
409 "display_name": "Python 3",
410 "language": "python",
411 "name": "python3"
412 },
413 "language_info": {
414 "codemirror_mode": {
415 "name": "ipython",
416 "version": 3
417 },
418 "file_extension": ".py",
419 "mimetype": "text/x-python",
420 "name": "python",
421 "nbconvert_exporter": "python",
422 "pygments_lexer": "ipython3",
423 "version": "3.8.2"
424 }
425 },
426 "nbformat": 4,
427 "nbformat_minor": 2
428 }