| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"# Pyplot" |
| 8 |
] |
| 9 |
}, |
| 10 |
{ |
| 11 |
"cell_type": "markdown", |
| 12 |
"metadata": {}, |
| 13 |
"source": [ |
| 14 |
"`pyplot` is a context based functional API offering meaningful defaults. It's a concise API and very similar to matplotlib's pyplot. Users new to `bqplot` should use `pyplot` as a starting point. Users create figure and mark objects using `pyplot` functions.\n", |
| 15 |
"\n", |
| 16 |
"Steps for building plots in `pyplot`:\n", |
| 17 |
"1. Create a figure object using plt.figure()\n", |
| 18 |
"* (Optional steps)\n", |
| 19 |
" * Scales can be customized using `plt.scales` function\n", |
| 20 |
" * Axes options can customized by passing a dict to `axes_options` argument in the marks' functions\n", |
| 21 |
"* Create marks using `pyplot` functions like `plot`, `bar`, `scatter` etc. (All the marks created will be automatically added to the figure object created in step 1)\n", |
| 22 |
"* Render the figure object using the following approaches:\n", |
| 23 |
" * Using `plt.show` function which renders the figure in the current context along with toolbar for panzoom etc.\n", |
| 24 |
" * Using display on the figure object created in step 1 (toolbar doesn't show up in this case)\n", |
| 25 |
"\n", |
| 26 |
"`pyplot` also offers many helper functions. A few are listed here:\n", |
| 27 |
"* plt.xlim: sets the domain bounds of the current 'x' scale\n", |
| 28 |
"* plt.ylim: sets the domain bounds of the current 'y' scale\n", |
| 29 |
"* plt.grids: shows/hides the axis grid lines\n", |
| 30 |
"* plt.xlabel: sets the X-Axis label\n", |
| 31 |
"* plt.ylabel: sets the Y-Axis label\n", |
| 32 |
"* plt.hline: draws a horizontal line at a specified level\n", |
| 33 |
"* plt.vline: draws a vertical line at a specified level\n", |
| 34 |
"\n", |
| 35 |
"Let's look at the same examples which were created in the [Object Model Notebook](Object Model.ipynb)" |
| 36 |
] |
| 37 |
}, |
| 38 |
{ |
| 39 |
"cell_type": "code", |
| 40 |
"execution_count": null, |
| 41 |
"metadata": {}, |
| 42 |
"outputs": [], |
| 43 |
"source": [ |
| 44 |
"import bqplot.pyplot as plt" |
| 45 |
] |
| 46 |
}, |
| 47 |
{ |
| 48 |
"cell_type": "code", |
| 49 |
"execution_count": null, |
| 50 |
"metadata": {}, |
| 51 |
"outputs": [], |
| 52 |
"source": [ |
| 53 |
"# first, let's create two vectors x and y to plot using a Lines mark\n", |
| 54 |
"import numpy as np\n", |
| 55 |
"x = np.linspace(-10, 10, 100)\n", |
| 56 |
"y = np.sin(x)\n", |
| 57 |
"\n", |
| 58 |
"# 1. Create the figure object\n", |
| 59 |
"fig = plt.figure(title='Simple Line Chart')\n", |
| 60 |
"\n", |
| 61 |
"# 2. By default axes are created with basic defaults. If you want to customize the axes create \n", |
| 62 |
"# a dict and pass it to `axxes_options` argument in the marks\n", |
| 63 |
"axes_opts = {'x': {'label': 'X'}, \n", |
| 64 |
" 'y': {'label': 'Y'}}\n", |
| 65 |
"\n", |
| 66 |
"# 3. Create a Lines mark by calling plt.plot function\n", |
| 67 |
"line = plt.plot(x=x, y=y, axes_options=axes_opts) # note that custom axes options are passed here\n", |
| 68 |
"\n", |
| 69 |
"# 4. Render the figure using plt.show()\n", |
| 70 |
"plt.show()" |
| 71 |
] |
| 72 |
}, |
| 73 |
{ |
| 74 |
"cell_type": "markdown", |
| 75 |
"metadata": {}, |
| 76 |
"source": [ |
| 77 |
"For creating other marks (like scatter, pie, bars, etc.), only step 2 needs to be changed. Lets look a simple example to create a bar chart:" |
| 78 |
] |
| 79 |
}, |
| 80 |
{ |
| 81 |
"cell_type": "code", |
| 82 |
"execution_count": null, |
| 83 |
"metadata": {}, |
| 84 |
"outputs": [], |
| 85 |
"source": [ |
| 86 |
"# first, let's create two vectors x and y to plot a bar chart\n", |
| 87 |
"x = list('ABCDE')\n", |
| 88 |
"y = np.random.rand(5)\n", |
| 89 |
"\n", |
| 90 |
"# 1. Create the figure object\n", |
| 91 |
"fig = plt.figure(title='Simple Bar Chart')\n", |
| 92 |
"\n", |
| 93 |
"# 2. Customize the axes options\n", |
| 94 |
"axes_opts = {'x': {'label': 'X', 'grid_lines': 'none'},\n", |
| 95 |
" 'y': {'label': 'Y', 'tick_format': '.0%'}}\n", |
| 96 |
"\n", |
| 97 |
"# 3. Create a Bars mark by calling plt.bar function\n", |
| 98 |
"bar = plt.bar(x=x, y=y, padding=.2, axes_options=axes_opts)\n", |
| 99 |
"\n", |
| 100 |
"# 4. directly display the figure object created in step 1 (note that the toolbar no longer shows up)\n", |
| 101 |
"fig" |
| 102 |
] |
| 103 |
}, |
| 104 |
{ |
| 105 |
"cell_type": "markdown", |
| 106 |
"metadata": {}, |
| 107 |
"source": [ |
| 108 |
"Multiple marks can be rendered in a figure. It's as easy as creating marks one after another. They'll all be added to the same figure!" |
| 109 |
] |
| 110 |
}, |
| 111 |
{ |
| 112 |
"cell_type": "code", |
| 113 |
"execution_count": null, |
| 114 |
"metadata": {}, |
| 115 |
"outputs": [], |
| 116 |
"source": [ |
| 117 |
"# first, let's create two vectors x and y\n", |
| 118 |
"import numpy as np\n", |
| 119 |
"x = np.linspace(-10, 10, 25)\n", |
| 120 |
"y = 3 * x + 5\n", |
| 121 |
"y_noise = y + 10 * np.random.randn(25) # add some random noise to y\n", |
| 122 |
"\n", |
| 123 |
"# 1. Create the figure object\n", |
| 124 |
"fig = plt.figure(title='Scatter and Line')\n", |
| 125 |
"\n", |
| 126 |
"# 3. Create line and scatter marks\n", |
| 127 |
"# additional attributes (stroke_width, colors etc.) can be passed as attributes to the mark objects as needed\n", |
| 128 |
"line = plt.plot(x=x, y=y, colors=['green'], stroke_width=3)\n", |
| 129 |
"scatter = plt.scatter(x=x, y=y_noise, colors=['red'], stroke='black')\n", |
| 130 |
"\n", |
| 131 |
"# setting x and y axis labels using pyplot functions. Note that these functions \n", |
| 132 |
"# should be called only after creating the marks\n", |
| 133 |
"plt.xlabel('X')\n", |
| 134 |
"plt.ylabel('Y')\n", |
| 135 |
"\n", |
| 136 |
"# 4. render the figure\n", |
| 137 |
"fig" |
| 138 |
] |
| 139 |
}, |
| 140 |
{ |
| 141 |
"cell_type": "markdown", |
| 142 |
"metadata": {}, |
| 143 |
"source": [ |
| 144 |
"`pyplot` is a simpler and an intuitive API. It's available for all the marks except MarketMap. It should be used in almost all the cases by default since it offers a less verbose API compared to the Object Model. Please refer to the mark [examples](../Marks/Pyplot) using `pyplot` and also this [pyplot example](../Basic Plotting/Pyplot.ipynb) notebook" |
| 145 |
] |
| 146 |
} |
| 147 |
], |
| 148 |
"metadata": { |
| 149 |
"kernelspec": { |
| 150 |
"display_name": "Python 3", |
| 151 |
"language": "python", |
| 152 |
"name": "python3" |
| 153 |
}, |
| 154 |
"language_info": { |
| 155 |
"codemirror_mode": { |
| 156 |
"name": "ipython", |
| 157 |
"version": 3 |
| 158 |
}, |
| 159 |
"file_extension": ".py", |
| 160 |
"mimetype": "text/x-python", |
| 161 |
"name": "python", |
| 162 |
"nbconvert_exporter": "python", |
| 163 |
"pygments_lexer": "ipython3", |
| 164 |
"version": "3.7.6" |
| 165 |
} |
| 166 |
}, |
| 167 |
"nbformat": 4, |
| 168 |
"nbformat_minor": 1 |
| 169 |
} |