| 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 LinearScale, Axis, Figure, Scatter" |
| 12 |
] |
| 13 |
}, |
| 14 |
{ |
| 15 |
"cell_type": "code", |
| 16 |
"execution_count": null, |
| 17 |
"metadata": {}, |
| 18 |
"outputs": [], |
| 19 |
"source": [ |
| 20 |
"np.random.seed(0)\n", |
| 21 |
"size = 100\n", |
| 22 |
"x = range(size)\n", |
| 23 |
"y1 = np.cumsum(np.random.randn(size) * 100.0)\n", |
| 24 |
"y2 = np.cumsum(np.random.randn(size))" |
| 25 |
] |
| 26 |
}, |
| 27 |
{ |
| 28 |
"cell_type": "markdown", |
| 29 |
"metadata": {}, |
| 30 |
"source": [ |
| 31 |
"## Miscellaneous Properties" |
| 32 |
] |
| 33 |
}, |
| 34 |
{ |
| 35 |
"cell_type": "code", |
| 36 |
"execution_count": null, |
| 37 |
"metadata": {}, |
| 38 |
"outputs": [], |
| 39 |
"source": [ |
| 40 |
"fig = plt.figure()\n", |
| 41 |
"line = plt.plot(np.arange(10), np.arange(10), colors=['hotpink', 'orange'])\n", |
| 42 |
"fig" |
| 43 |
] |
| 44 |
}, |
| 45 |
{ |
| 46 |
"cell_type": "code", |
| 47 |
"execution_count": null, |
| 48 |
"metadata": {}, |
| 49 |
"outputs": [], |
| 50 |
"source": [ |
| 51 |
"# set the grid lines of axes to dashed\n", |
| 52 |
"xax, yax = plt.axes()['x'], plt.axes()['y']\n", |
| 53 |
"\n", |
| 54 |
"xax.grid_lines = 'dashed'\n", |
| 55 |
"yax.grid_lines = 'dashed'" |
| 56 |
] |
| 57 |
}, |
| 58 |
{ |
| 59 |
"cell_type": "code", |
| 60 |
"execution_count": null, |
| 61 |
"metadata": {}, |
| 62 |
"outputs": [], |
| 63 |
"source": [ |
| 64 |
"# Change the side of the axis\n", |
| 65 |
"yax.side = 'right'\n", |
| 66 |
"xax.side = 'top'" |
| 67 |
] |
| 68 |
}, |
| 69 |
{ |
| 70 |
"cell_type": "code", |
| 71 |
"execution_count": null, |
| 72 |
"metadata": {}, |
| 73 |
"outputs": [], |
| 74 |
"source": [ |
| 75 |
"# change the orientation of axes\n", |
| 76 |
"yax.orientation = 'horizontal'\n", |
| 77 |
"xax.orientation = 'vertical'" |
| 78 |
] |
| 79 |
}, |
| 80 |
{ |
| 81 |
"cell_type": "code", |
| 82 |
"execution_count": null, |
| 83 |
"metadata": {}, |
| 84 |
"outputs": [], |
| 85 |
"source": [ |
| 86 |
"# Change the format of the ticks on the axis\n", |
| 87 |
"yax.tick_format = '0.2f'\n", |
| 88 |
"xax.tick_format = '0.0f'" |
| 89 |
] |
| 90 |
}, |
| 91 |
{ |
| 92 |
"cell_type": "code", |
| 93 |
"execution_count": null, |
| 94 |
"metadata": {}, |
| 95 |
"outputs": [], |
| 96 |
"source": [ |
| 97 |
"# Change the color of the axis\n", |
| 98 |
"with xax.hold_sync():\n", |
| 99 |
" xax.color = 'orangered'\n", |
| 100 |
" xax.label_color = 'orangered'" |
| 101 |
] |
| 102 |
}, |
| 103 |
{ |
| 104 |
"cell_type": "code", |
| 105 |
"execution_count": null, |
| 106 |
"metadata": {}, |
| 107 |
"outputs": [], |
| 108 |
"source": [ |
| 109 |
"# change the grid colors\n", |
| 110 |
"xax.grid_color = 'red'" |
| 111 |
] |
| 112 |
}, |
| 113 |
{ |
| 114 |
"cell_type": "markdown", |
| 115 |
"metadata": {}, |
| 116 |
"source": [ |
| 117 |
"## Label Placement" |
| 118 |
] |
| 119 |
}, |
| 120 |
{ |
| 121 |
"cell_type": "code", |
| 122 |
"execution_count": null, |
| 123 |
"metadata": {}, |
| 124 |
"outputs": [], |
| 125 |
"source": [ |
| 126 |
"fig = plt.figure()\n", |
| 127 |
"axes_options = {'x': {'label': 'X'}, 'y': {'label': 'Y'}}\n", |
| 128 |
"line = plt.plot(x, y1, colors=['orangered'], axes_options=axes_options)\n", |
| 129 |
"fig" |
| 130 |
] |
| 131 |
}, |
| 132 |
{ |
| 133 |
"cell_type": "code", |
| 134 |
"execution_count": null, |
| 135 |
"metadata": {}, |
| 136 |
"outputs": [], |
| 137 |
"source": [ |
| 138 |
"xax, yax = plt.axes()['x'], plt.axes()['y']\n", |
| 139 |
"\n", |
| 140 |
"# moving the label along the axis\n", |
| 141 |
"xax.label_location = 'end'\n", |
| 142 |
"yax.label_location = 'start'" |
| 143 |
] |
| 144 |
}, |
| 145 |
{ |
| 146 |
"cell_type": "code", |
| 147 |
"execution_count": null, |
| 148 |
"metadata": {}, |
| 149 |
"outputs": [], |
| 150 |
"source": [ |
| 151 |
"# moving the label perpendicular to the axis\n", |
| 152 |
"yax.label_offset = '6ex'\n", |
| 153 |
"xax.label_offset = '-1em'" |
| 154 |
] |
| 155 |
}, |
| 156 |
{ |
| 157 |
"cell_type": "markdown", |
| 158 |
"metadata": {}, |
| 159 |
"source": [ |
| 160 |
"## Ticks properties" |
| 161 |
] |
| 162 |
}, |
| 163 |
{ |
| 164 |
"cell_type": "code", |
| 165 |
"execution_count": null, |
| 166 |
"metadata": { |
| 167 |
"scrolled": false |
| 168 |
}, |
| 169 |
"outputs": [], |
| 170 |
"source": [ |
| 171 |
"fig = plt.figure()\n", |
| 172 |
"line = plt.plot(x[:20], y1[:20], 'm')\n", |
| 173 |
"fig" |
| 174 |
] |
| 175 |
}, |
| 176 |
{ |
| 177 |
"cell_type": "code", |
| 178 |
"execution_count": null, |
| 179 |
"metadata": {}, |
| 180 |
"outputs": [], |
| 181 |
"source": [ |
| 182 |
"xax, yax = plt.axes()['x'], plt.axes()['y']" |
| 183 |
] |
| 184 |
}, |
| 185 |
{ |
| 186 |
"cell_type": "code", |
| 187 |
"execution_count": null, |
| 188 |
"metadata": {}, |
| 189 |
"outputs": [], |
| 190 |
"source": [ |
| 191 |
"# Setting the number of ticks\n", |
| 192 |
"xax.num_ticks = 8" |
| 193 |
] |
| 194 |
}, |
| 195 |
{ |
| 196 |
"cell_type": "code", |
| 197 |
"execution_count": null, |
| 198 |
"metadata": {}, |
| 199 |
"outputs": [], |
| 200 |
"source": [ |
| 201 |
"# Setting the tick values\n", |
| 202 |
"xax.tick_values = np.arange(1, 21)" |
| 203 |
] |
| 204 |
}, |
| 205 |
{ |
| 206 |
"cell_type": "code", |
| 207 |
"execution_count": null, |
| 208 |
"metadata": {}, |
| 209 |
"outputs": [], |
| 210 |
"source": [ |
| 211 |
"# Setting the style for the text of the ticks\n", |
| 212 |
"xax.tick_style = {'stroke': 'orangered', 'font-size': 14}" |
| 213 |
] |
| 214 |
}, |
| 215 |
{ |
| 216 |
"cell_type": "markdown", |
| 217 |
"metadata": {}, |
| 218 |
"source": [ |
| 219 |
"## Tick labels\n", |
| 220 |
"The tick label dictionary can override a label " |
| 221 |
] |
| 222 |
}, |
| 223 |
{ |
| 224 |
"cell_type": "code", |
| 225 |
"execution_count": null, |
| 226 |
"metadata": {}, |
| 227 |
"outputs": [], |
| 228 |
"source": [ |
| 229 |
"# Roman labeling\n", |
| 230 |
"xax.tick_labels = {\n", |
| 231 |
" 1: 'I',\n", |
| 232 |
" 3: 'III',\n", |
| 233 |
" 5: 'V',\n", |
| 234 |
" 7: 'VII',\n", |
| 235 |
" 9: 'IX',\n", |
| 236 |
" 11: 'XI',\n", |
| 237 |
" 13: 'XIII',\n", |
| 238 |
" 15: 'XV',\n", |
| 239 |
" 17: 'XVII',\n", |
| 240 |
" 19: 'XIX'\n", |
| 241 |
"}" |
| 242 |
] |
| 243 |
}, |
| 244 |
{ |
| 245 |
"cell_type": "markdown", |
| 246 |
"metadata": {}, |
| 247 |
"source": [ |
| 248 |
"# Axis Placement" |
| 249 |
] |
| 250 |
}, |
| 251 |
{ |
| 252 |
"cell_type": "code", |
| 253 |
"execution_count": null, |
| 254 |
"metadata": {}, |
| 255 |
"outputs": [], |
| 256 |
"source": [ |
| 257 |
"# using object model for this example\n", |
| 258 |
"sc_x = LinearScale()\n", |
| 259 |
"sc_y = LinearScale(reverse=True)\n", |
| 260 |
"sc_y2 = LinearScale()\n", |
| 261 |
"\n", |
| 262 |
"sc1 = Scatter(x=x[:10], y=y1[:10], \n", |
| 263 |
" scales={'x': sc_x, 'y': sc_y},\n", |
| 264 |
" default_size=100)\n", |
| 265 |
"\n", |
| 266 |
"sc2 = Scatter(x=x[:10], y=y2[:10],\n", |
| 267 |
" colors=['dodgerblue'],\n", |
| 268 |
" marker='cross', scales={'x': sc_x, 'y': sc_y2})" |
| 269 |
] |
| 270 |
}, |
| 271 |
{ |
| 272 |
"cell_type": "markdown", |
| 273 |
"metadata": {}, |
| 274 |
"source": [ |
| 275 |
"## Placement of axis in terms of figure scales" |
| 276 |
] |
| 277 |
}, |
| 278 |
{ |
| 279 |
"cell_type": "code", |
| 280 |
"execution_count": null, |
| 281 |
"metadata": {}, |
| 282 |
"outputs": [], |
| 283 |
"source": [ |
| 284 |
"## Setting offset for axis in terms of figure scales\n", |
| 285 |
"xax = Axis(label='Test X', scale=sc_x, \n", |
| 286 |
" grid_lines='solid', offset=dict(value=0.2), label_location=\"start\")\n", |
| 287 |
"\n", |
| 288 |
"yax = Axis(label='Test Y', scale=sc_y, \n", |
| 289 |
" orientation='vertical', tick_format='0.2f', \n", |
| 290 |
" grid_lines='solid', label_location=\"end\")\n", |
| 291 |
"\n", |
| 292 |
"yax2 = Axis(label='Test Y2', scale=sc_y2, orientation='vertical', side='right', \n", |
| 293 |
" tick_format='0.2f')\n", |
| 294 |
"Figure(axes=[xax, yax, yax2], marks=[sc1, sc2], legend_location='top-right',\n", |
| 295 |
" padding_x=0.025)" |
| 296 |
] |
| 297 |
}, |
| 298 |
{ |
| 299 |
"cell_type": "code", |
| 300 |
"execution_count": null, |
| 301 |
"metadata": {}, |
| 302 |
"outputs": [], |
| 303 |
"source": [ |
| 304 |
"# restoring to default position\n", |
| 305 |
"xax.offset = {}" |
| 306 |
] |
| 307 |
}, |
| 308 |
{ |
| 309 |
"cell_type": "markdown", |
| 310 |
"metadata": {}, |
| 311 |
"source": [ |
| 312 |
"## Placement of axis in terms of actual scales and data" |
| 313 |
] |
| 314 |
}, |
| 315 |
{ |
| 316 |
"cell_type": "code", |
| 317 |
"execution_count": null, |
| 318 |
"metadata": {}, |
| 319 |
"outputs": [], |
| 320 |
"source": [ |
| 321 |
"yax.offset = dict(value=3.0, scale=sc_x)" |
| 322 |
] |
| 323 |
}, |
| 324 |
{ |
| 325 |
"cell_type": "markdown", |
| 326 |
"metadata": {}, |
| 327 |
"source": [ |
| 328 |
"# Color Axis" |
| 329 |
] |
| 330 |
}, |
| 331 |
{ |
| 332 |
"cell_type": "code", |
| 333 |
"execution_count": null, |
| 334 |
"metadata": {}, |
| 335 |
"outputs": [], |
| 336 |
"source": [ |
| 337 |
"# add a fig margin to allow place for color bar\n", |
| 338 |
"fig = plt.figure(fig_margin=dict(top=80, left=80, right=80, bottom=80))\n", |
| 339 |
"scatter = plt.scatter(x, y1, color=y2, stroke='black')\n", |
| 340 |
"fig" |
| 341 |
] |
| 342 |
}, |
| 343 |
{ |
| 344 |
"cell_type": "markdown", |
| 345 |
"metadata": {}, |
| 346 |
"source": [ |
| 347 |
"## Changing side and orientation of color axis" |
| 348 |
] |
| 349 |
}, |
| 350 |
{ |
| 351 |
"cell_type": "code", |
| 352 |
"execution_count": null, |
| 353 |
"metadata": {}, |
| 354 |
"outputs": [], |
| 355 |
"source": [ |
| 356 |
"xax, yax, cax = plt.axes()['x'], plt.axes()['y'], plt.axes()['color']\n", |
| 357 |
"cax.side = 'top'" |
| 358 |
] |
| 359 |
}, |
| 360 |
{ |
| 361 |
"cell_type": "code", |
| 362 |
"execution_count": null, |
| 363 |
"metadata": {}, |
| 364 |
"outputs": [], |
| 365 |
"source": [ |
| 366 |
"with cax.hold_sync():\n", |
| 367 |
" cax.orientation = 'vertical'\n", |
| 368 |
" cax.side = 'right'" |
| 369 |
] |
| 370 |
}, |
| 371 |
{ |
| 372 |
"cell_type": "code", |
| 373 |
"execution_count": null, |
| 374 |
"metadata": {}, |
| 375 |
"outputs": [], |
| 376 |
"source": [ |
| 377 |
"yax.side = 'right'\n", |
| 378 |
"cax.side = 'left'" |
| 379 |
] |
| 380 |
} |
| 381 |
], |
| 382 |
"metadata": { |
| 383 |
"anaconda-cloud": {}, |
| 384 |
"kernelspec": { |
| 385 |
"display_name": "Python 3", |
| 386 |
"language": "python", |
| 387 |
"name": "python3" |
| 388 |
}, |
| 389 |
"language_info": { |
| 390 |
"codemirror_mode": { |
| 391 |
"name": "ipython", |
| 392 |
"version": 3 |
| 393 |
}, |
| 394 |
"file_extension": ".py", |
| 395 |
"mimetype": "text/x-python", |
| 396 |
"name": "python", |
| 397 |
"nbconvert_exporter": "python", |
| 398 |
"pygments_lexer": "ipython3", |
| 399 |
"version": "3.7.6" |
| 400 |
} |
| 401 |
}, |
| 402 |
"nbformat": 4, |
| 403 |
"nbformat_minor": 1 |
| 404 |
} |