| 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 |
"import bqplot.pyplot as plt\n", |
| 11 |
"from bqplot import CATEGORY10, ColorScale" |
| 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 |
"plt.figure(title='Bar Chart')\n", |
| 43 |
"plt.bar(np.arange(10), np.random.rand(10))\n", |
| 44 |
"plt.show()" |
| 45 |
] |
| 46 |
}, |
| 47 |
{ |
| 48 |
"cell_type": "markdown", |
| 49 |
"metadata": {}, |
| 50 |
"source": [ |
| 51 |
"## Horizontal Bar Chart" |
| 52 |
] |
| 53 |
}, |
| 54 |
{ |
| 55 |
"cell_type": "markdown", |
| 56 |
"metadata": {}, |
| 57 |
"source": [ |
| 58 |
"To generate a horizontal bar chart, pass `orientation='horizontal'` to the bar." |
| 59 |
] |
| 60 |
}, |
| 61 |
{ |
| 62 |
"cell_type": "code", |
| 63 |
"execution_count": null, |
| 64 |
"metadata": {}, |
| 65 |
"outputs": [], |
| 66 |
"source": [ |
| 67 |
"plt.figure(title='Horizontal Bar Chart')\n", |
| 68 |
"plt.bar(np.arange(10), np.random.uniform(-1, 1, 10), orientation='horizontal')\n", |
| 69 |
"plt.show()" |
| 70 |
] |
| 71 |
}, |
| 72 |
{ |
| 73 |
"cell_type": "markdown", |
| 74 |
"metadata": {}, |
| 75 |
"source": [ |
| 76 |
"## Changing the reference value from which the Bars are drawn" |
| 77 |
] |
| 78 |
}, |
| 79 |
{ |
| 80 |
"cell_type": "code", |
| 81 |
"execution_count": null, |
| 82 |
"metadata": {}, |
| 83 |
"outputs": [], |
| 84 |
"source": [ |
| 85 |
"fig = plt.figure()\n", |
| 86 |
"# assign the output of the plt.bar to a mark object\n", |
| 87 |
"bar = plt.bar(x_data[:20], np.abs(y_data_2[:20]), base=1.0)\n", |
| 88 |
"# render the figure\n", |
| 89 |
"fig" |
| 90 |
] |
| 91 |
}, |
| 92 |
{ |
| 93 |
"cell_type": "code", |
| 94 |
"execution_count": null, |
| 95 |
"metadata": {}, |
| 96 |
"outputs": [], |
| 97 |
"source": [ |
| 98 |
"# changing the base\n", |
| 99 |
"bar.base = 2.0" |
| 100 |
] |
| 101 |
}, |
| 102 |
{ |
| 103 |
"cell_type": "code", |
| 104 |
"execution_count": null, |
| 105 |
"metadata": {}, |
| 106 |
"outputs": [], |
| 107 |
"source": [ |
| 108 |
"bar.align = 'right'" |
| 109 |
] |
| 110 |
}, |
| 111 |
{ |
| 112 |
"cell_type": "markdown", |
| 113 |
"metadata": {}, |
| 114 |
"source": [ |
| 115 |
"## Bar Chart Properties" |
| 116 |
] |
| 117 |
}, |
| 118 |
{ |
| 119 |
"cell_type": "code", |
| 120 |
"execution_count": null, |
| 121 |
"metadata": {}, |
| 122 |
"outputs": [], |
| 123 |
"source": [ |
| 124 |
"# Increasing the spacing between the bars using padding property\n", |
| 125 |
"fig = plt.figure()\n", |
| 126 |
"bar = plt.bar(x_data[:20], y_data[:20], padding=0.3)\n", |
| 127 |
"fig" |
| 128 |
] |
| 129 |
}, |
| 130 |
{ |
| 131 |
"cell_type": "code", |
| 132 |
"execution_count": null, |
| 133 |
"metadata": {}, |
| 134 |
"outputs": [], |
| 135 |
"source": [ |
| 136 |
"# changing basic properties like stroke and opacity\n", |
| 137 |
"bar.stroke = 'red'\n", |
| 138 |
"bar.opacities = [0.5, 0.2]" |
| 139 |
] |
| 140 |
}, |
| 141 |
{ |
| 142 |
"cell_type": "code", |
| 143 |
"execution_count": null, |
| 144 |
"metadata": {}, |
| 145 |
"outputs": [], |
| 146 |
"source": [ |
| 147 |
"bar.orientation = 'horizontal'\n", |
| 148 |
"fig.axes[0].orientation = 'vertical'\n", |
| 149 |
"fig.axes[1].orientation = 'horizontal'" |
| 150 |
] |
| 151 |
}, |
| 152 |
{ |
| 153 |
"cell_type": "markdown", |
| 154 |
"metadata": {}, |
| 155 |
"source": [ |
| 156 |
"## Stacked Bar Chart for 2-d data" |
| 157 |
] |
| 158 |
}, |
| 159 |
{ |
| 160 |
"cell_type": "code", |
| 161 |
"execution_count": null, |
| 162 |
"metadata": {}, |
| 163 |
"outputs": [], |
| 164 |
"source": [ |
| 165 |
"fig = plt.figure()\n", |
| 166 |
"bar = plt.bar(x_data, [y_data[:20], y_data_2[:20]], \n", |
| 167 |
" padding=0.2,\n", |
| 168 |
" colors=CATEGORY10)\n", |
| 169 |
"fig" |
| 170 |
] |
| 171 |
}, |
| 172 |
{ |
| 173 |
"cell_type": "markdown", |
| 174 |
"metadata": {}, |
| 175 |
"source": [ |
| 176 |
"## Grouped Bar Chart" |
| 177 |
] |
| 178 |
}, |
| 179 |
{ |
| 180 |
"cell_type": "code", |
| 181 |
"execution_count": null, |
| 182 |
"metadata": {}, |
| 183 |
"outputs": [], |
| 184 |
"source": [ |
| 185 |
"bar.type = 'grouped' # equivalent to saying\n", |
| 186 |
"# plt.bar(x_data, y_data, padding=0.2, type='grouped')" |
| 187 |
] |
| 188 |
}, |
| 189 |
{ |
| 190 |
"cell_type": "code", |
| 191 |
"execution_count": null, |
| 192 |
"metadata": {}, |
| 193 |
"outputs": [], |
| 194 |
"source": [ |
| 195 |
"fig = plt.figure()\n", |
| 196 |
"bar = plt.bar(x_data, [y_data[:20], y_data_2[:20]], \n", |
| 197 |
" padding=0.2, \n", |
| 198 |
" colors=CATEGORY10, \n", |
| 199 |
" orientation='horizontal')\n", |
| 200 |
"fig" |
| 201 |
] |
| 202 |
}, |
| 203 |
{ |
| 204 |
"cell_type": "code", |
| 205 |
"execution_count": null, |
| 206 |
"metadata": {}, |
| 207 |
"outputs": [], |
| 208 |
"source": [ |
| 209 |
"bar.type = 'grouped'" |
| 210 |
] |
| 211 |
}, |
| 212 |
{ |
| 213 |
"cell_type": "markdown", |
| 214 |
"metadata": {}, |
| 215 |
"source": [ |
| 216 |
"## Modifying color mode" |
| 217 |
] |
| 218 |
}, |
| 219 |
{ |
| 220 |
"cell_type": "code", |
| 221 |
"execution_count": null, |
| 222 |
"metadata": {}, |
| 223 |
"outputs": [], |
| 224 |
"source": [ |
| 225 |
"## Color mode has 2 values. 'group' and 'element'. \n", |
| 226 |
"## 'group' means for every x all bars have same color.\n", |
| 227 |
"## 'element' means for every dimension of y, all bars have same color.\n", |
| 228 |
"fig = plt.figure()\n", |
| 229 |
"bar = plt.bar(x_data, [y_data[:20], y_data_2[:20]], padding=0.2, colors=CATEGORY10, color_mode='group')\n", |
| 230 |
"fig" |
| 231 |
] |
| 232 |
}, |
| 233 |
{ |
| 234 |
"cell_type": "code", |
| 235 |
"execution_count": null, |
| 236 |
"metadata": {}, |
| 237 |
"outputs": [], |
| 238 |
"source": [ |
| 239 |
"## for 1-d array for Y.\n", |
| 240 |
"fig = plt.figure()\n", |
| 241 |
"bar = plt.bar(x_data, y_data[:20], padding=0.2, color_mode='element', \n", |
| 242 |
" labels=['Values'], display_legend=True)\n", |
| 243 |
"fig" |
| 244 |
] |
| 245 |
}, |
| 246 |
{ |
| 247 |
"cell_type": "markdown", |
| 248 |
"metadata": { |
| 249 |
"collapsed": true |
| 250 |
}, |
| 251 |
"source": [ |
| 252 |
"## Representing additional dimension using Color" |
| 253 |
] |
| 254 |
}, |
| 255 |
{ |
| 256 |
"cell_type": "code", |
| 257 |
"execution_count": null, |
| 258 |
"metadata": {}, |
| 259 |
"outputs": [], |
| 260 |
"source": [ |
| 261 |
"# In this example, the color is just the magnitude of the y data\n", |
| 262 |
"fig = plt.figure()\n", |
| 263 |
"# add a 'reds' color scale \n", |
| 264 |
"plt.scales(scales={'color': ColorScale(scheme='Reds')})\n", |
| 265 |
"\n", |
| 266 |
"bar = plt.bar(x_data[:20], y_data[:20], color=np.abs(y_data[:20]), padding=0.2)\n", |
| 267 |
"\n", |
| 268 |
"# give enough bottom margin to accommodate the color axis\n", |
| 269 |
"fig.fig_margin = dict(top=50, bottom=80, left=50, right=50)\n", |
| 270 |
"\n", |
| 271 |
"fig" |
| 272 |
] |
| 273 |
}, |
| 274 |
{ |
| 275 |
"cell_type": "markdown", |
| 276 |
"metadata": {}, |
| 277 |
"source": [ |
| 278 |
"## Adding color for 2-d data" |
| 279 |
] |
| 280 |
}, |
| 281 |
{ |
| 282 |
"cell_type": "code", |
| 283 |
"execution_count": null, |
| 284 |
"metadata": {}, |
| 285 |
"outputs": [], |
| 286 |
"source": [ |
| 287 |
"# By default color is applied along the axis=1\n", |
| 288 |
"fig = plt.figure()\n", |
| 289 |
"plt.scales(scales={'color': ColorScale(scheme='Reds')})\n", |
| 290 |
"\n", |
| 291 |
"y_vals = [y_data[:20], y_data_2[:20], y_data_3[:20] / 100.0]\n", |
| 292 |
"color_data = np.mean(y_vals, axis=1)\n", |
| 293 |
"\n", |
| 294 |
"bar = plt.bar(x_data, y_vals, color=color_data, padding=0.2,\n", |
| 295 |
" labels=['Dim 1', 'Dim 2', 'Dim 3'], display_legend=True)\n", |
| 296 |
"\n", |
| 297 |
"fig.fig_margin = dict(top=50, bottom=80, left=50, right=50)\n", |
| 298 |
"fig" |
| 299 |
] |
| 300 |
}, |
| 301 |
{ |
| 302 |
"cell_type": "code", |
| 303 |
"execution_count": null, |
| 304 |
"metadata": {}, |
| 305 |
"outputs": [], |
| 306 |
"source": [ |
| 307 |
"# Applying color along the axis=0\n", |
| 308 |
"fig = plt.figure()\n", |
| 309 |
"plt.scales(scales={'color': ColorScale(mid=0.)})\n", |
| 310 |
"\n", |
| 311 |
"y_vals = [y_data[:20], y_data_2[:20], y_data_3[:20] / 100.0]\n", |
| 312 |
"color_data = np.mean(y_vals, axis=0)\n", |
| 313 |
"\n", |
| 314 |
"bar = plt.bar(x_data, y_vals, color=color_data, padding=0.2, \n", |
| 315 |
" color_mode='group', stroke='orange')\n", |
| 316 |
"fig.fig_margin = dict(top=50, bottom=80, left=50, right=50)\n", |
| 317 |
"fig" |
| 318 |
] |
| 319 |
} |
| 320 |
], |
| 321 |
"metadata": { |
| 322 |
"kernelspec": { |
| 323 |
"display_name": "Python 3", |
| 324 |
"language": "python", |
| 325 |
"name": "python3" |
| 326 |
}, |
| 327 |
"language_info": { |
| 328 |
"codemirror_mode": { |
| 329 |
"name": "ipython", |
| 330 |
"version": 3 |
| 331 |
}, |
| 332 |
"file_extension": ".py", |
| 333 |
"mimetype": "text/x-python", |
| 334 |
"name": "python", |
| 335 |
"nbconvert_exporter": "python", |
| 336 |
"pygments_lexer": "ipython3", |
| 337 |
"version": "3.7.6" |
| 338 |
} |
| 339 |
}, |
| 340 |
"nbformat": 4, |
| 341 |
"nbformat_minor": 1 |
| 342 |
} |