| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "code", |
| 5 |
"execution_count": null, |
| 6 |
"metadata": { |
| 7 |
"collapsed": true, |
| 8 |
"deletable": true, |
| 9 |
"editable": true |
| 10 |
}, |
| 11 |
"outputs": [], |
| 12 |
"source": [ |
| 13 |
"import numpy as np\n", |
| 14 |
"import bqplot.pyplot as plt" |
| 15 |
] |
| 16 |
}, |
| 17 |
{ |
| 18 |
"cell_type": "markdown", |
| 19 |
"metadata": { |
| 20 |
"deletable": true, |
| 21 |
"editable": true |
| 22 |
}, |
| 23 |
"source": [ |
| 24 |
"## Bins Mark\n", |
| 25 |
"\n", |
| 26 |
"This `Mark` is essentially the same as the `Hist` `Mark` from a user point of view, but is actually a `Bars` instance that bins sample data.\n", |
| 27 |
"\n", |
| 28 |
"The difference with `Hist` is that the binning is done in the backend, so it will work better for large data as it does not have to ship the whole data back and forth to the frontend." |
| 29 |
] |
| 30 |
}, |
| 31 |
{ |
| 32 |
"cell_type": "code", |
| 33 |
"execution_count": null, |
| 34 |
"metadata": { |
| 35 |
"collapsed": true, |
| 36 |
"deletable": true, |
| 37 |
"editable": true |
| 38 |
}, |
| 39 |
"outputs": [], |
| 40 |
"source": [ |
| 41 |
"# Create a sample of Gaussian draws\n", |
| 42 |
"np.random.seed(0)\n", |
| 43 |
"x_data = np.random.randn(1000)" |
| 44 |
] |
| 45 |
}, |
| 46 |
{ |
| 47 |
"cell_type": "markdown", |
| 48 |
"metadata": { |
| 49 |
"deletable": true, |
| 50 |
"editable": true |
| 51 |
}, |
| 52 |
"source": [ |
| 53 |
"Give the `Hist` mark the data you want to perform as the `sample` argument, and also give 'x' and 'y' scales." |
| 54 |
] |
| 55 |
}, |
| 56 |
{ |
| 57 |
"cell_type": "code", |
| 58 |
"execution_count": null, |
| 59 |
"metadata": { |
| 60 |
"collapsed": false, |
| 61 |
"deletable": true, |
| 62 |
"editable": true |
| 63 |
}, |
| 64 |
"outputs": [], |
| 65 |
"source": [ |
| 66 |
"fig = plt.figure(padding_y=0)\n", |
| 67 |
"hist = plt.bin(x_data, padding=0)\n", |
| 68 |
"fig" |
| 69 |
] |
| 70 |
}, |
| 71 |
{ |
| 72 |
"cell_type": "markdown", |
| 73 |
"metadata": { |
| 74 |
"deletable": true, |
| 75 |
"editable": true |
| 76 |
}, |
| 77 |
"source": [ |
| 78 |
"The midpoints of the resulting bins and their number of elements can be recovered via the read-only traits `x` and `y`:" |
| 79 |
] |
| 80 |
}, |
| 81 |
{ |
| 82 |
"cell_type": "code", |
| 83 |
"execution_count": null, |
| 84 |
"metadata": { |
| 85 |
"collapsed": false, |
| 86 |
"deletable": true, |
| 87 |
"editable": true |
| 88 |
}, |
| 89 |
"outputs": [], |
| 90 |
"source": [ |
| 91 |
"hist.x, hist.y" |
| 92 |
] |
| 93 |
}, |
| 94 |
{ |
| 95 |
"cell_type": "markdown", |
| 96 |
"metadata": { |
| 97 |
"deletable": true, |
| 98 |
"editable": true |
| 99 |
}, |
| 100 |
"source": [ |
| 101 |
"## Tuning the bins\n", |
| 102 |
"\n", |
| 103 |
"Under the hood, the `Bins` mark is really a `Bars` mark, with some additional magic to control the binning. The data in `sample` is binned into equal-width bins. The parameters controlling the binning are the following traits:\n", |
| 104 |
"\n", |
| 105 |
"- `bins` sets the number of bins. It is either a fixed integer (10 by default), or the name of a method to determine the number of bins in a smart way ('auto', 'fd', 'doane', 'scott', 'rice', 'sturges' or 'sqrt').\n", |
| 106 |
"\n", |
| 107 |
"- `min` and `max` set the range of the data (`sample`) to be binned\n", |
| 108 |
"\n", |
| 109 |
"- `density`, if set to `True`, normalizes the heights of the bars.\n", |
| 110 |
"\n", |
| 111 |
"For more information, see the documentation of `numpy`'s `histogram`" |
| 112 |
] |
| 113 |
}, |
| 114 |
{ |
| 115 |
"cell_type": "code", |
| 116 |
"execution_count": null, |
| 117 |
"metadata": { |
| 118 |
"collapsed": false, |
| 119 |
"deletable": true, |
| 120 |
"editable": true |
| 121 |
}, |
| 122 |
"outputs": [], |
| 123 |
"source": [ |
| 124 |
"fig = plt.figure(padding_y=0)\n", |
| 125 |
"hist = plt.bin(x_data, padding=0)\n", |
| 126 |
"fig" |
| 127 |
] |
| 128 |
}, |
| 129 |
{ |
| 130 |
"cell_type": "code", |
| 131 |
"execution_count": null, |
| 132 |
"metadata": { |
| 133 |
"collapsed": true, |
| 134 |
"deletable": true, |
| 135 |
"editable": true |
| 136 |
}, |
| 137 |
"outputs": [], |
| 138 |
"source": [ |
| 139 |
"# Changing the number of bins\n", |
| 140 |
"hist.bins = 'sqrt'" |
| 141 |
] |
| 142 |
}, |
| 143 |
{ |
| 144 |
"cell_type": "code", |
| 145 |
"execution_count": null, |
| 146 |
"metadata": { |
| 147 |
"collapsed": true, |
| 148 |
"deletable": true, |
| 149 |
"editable": true |
| 150 |
}, |
| 151 |
"outputs": [], |
| 152 |
"source": [ |
| 153 |
"# Changing the range\n", |
| 154 |
"hist.min = 0" |
| 155 |
] |
| 156 |
}, |
| 157 |
{ |
| 158 |
"cell_type": "markdown", |
| 159 |
"metadata": { |
| 160 |
"deletable": true, |
| 161 |
"editable": true |
| 162 |
}, |
| 163 |
"source": [ |
| 164 |
"## Histogram Styling\n", |
| 165 |
"\n", |
| 166 |
"The styling of `Hist` is identical to the one of `Bars`" |
| 167 |
] |
| 168 |
}, |
| 169 |
{ |
| 170 |
"cell_type": "code", |
| 171 |
"execution_count": null, |
| 172 |
"metadata": { |
| 173 |
"collapsed": false, |
| 174 |
"deletable": true, |
| 175 |
"editable": true |
| 176 |
}, |
| 177 |
"outputs": [], |
| 178 |
"source": [ |
| 179 |
"# Normalizing the count\n", |
| 180 |
"fig = plt.figure(padding_y=0)\n", |
| 181 |
"hist = plt.bin(x_data, density=True)\n", |
| 182 |
"fig" |
| 183 |
] |
| 184 |
}, |
| 185 |
{ |
| 186 |
"cell_type": "code", |
| 187 |
"execution_count": null, |
| 188 |
"metadata": { |
| 189 |
"collapsed": true, |
| 190 |
"deletable": true, |
| 191 |
"editable": true |
| 192 |
}, |
| 193 |
"outputs": [], |
| 194 |
"source": [ |
| 195 |
"# changing the color\n", |
| 196 |
"hist.colors=['orangered']" |
| 197 |
] |
| 198 |
}, |
| 199 |
{ |
| 200 |
"cell_type": "code", |
| 201 |
"execution_count": null, |
| 202 |
"metadata": { |
| 203 |
"collapsed": true, |
| 204 |
"deletable": true, |
| 205 |
"editable": true |
| 206 |
}, |
| 207 |
"outputs": [], |
| 208 |
"source": [ |
| 209 |
"# stroke and opacity update\n", |
| 210 |
"hist.stroke = 'orange'\n", |
| 211 |
"hist.opacities = [0.5] * len(hist.x)" |
| 212 |
] |
| 213 |
}, |
| 214 |
{ |
| 215 |
"cell_type": "code", |
| 216 |
"execution_count": null, |
| 217 |
"metadata": { |
| 218 |
"collapsed": true, |
| 219 |
"deletable": true, |
| 220 |
"editable": true |
| 221 |
}, |
| 222 |
"outputs": [], |
| 223 |
"source": [ |
| 224 |
"# Laying the histogram on its side\n", |
| 225 |
"hist.orientation = 'horizontal'\n", |
| 226 |
"fig.axes[0].orientation = 'vertical'\n", |
| 227 |
"fig.axes[1].orientation = 'horizontal'" |
| 228 |
] |
| 229 |
} |
| 230 |
], |
| 231 |
"metadata": { |
| 232 |
"kernelspec": { |
| 233 |
"display_name": "Python 3", |
| 234 |
"language": "python", |
| 235 |
"name": "python3" |
| 236 |
}, |
| 237 |
"language_info": { |
| 238 |
"codemirror_mode": { |
| 239 |
"name": "ipython", |
| 240 |
"version": 3 |
| 241 |
}, |
| 242 |
"file_extension": ".py", |
| 243 |
"mimetype": "text/x-python", |
| 244 |
"name": "python", |
| 245 |
"nbconvert_exporter": "python", |
| 246 |
"pygments_lexer": "ipython3", |
| 247 |
"version": "3.6.2" |
| 248 |
} |
| 249 |
}, |
| 250 |
"nbformat": 4, |
| 251 |
"nbformat_minor": 1 |
| 252 |
} |