Debian Codegraph @

bqplot/examples/Advanced Plotting/Advanced Plotting.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {
7 "input_collapsed": false
8 },
9 "outputs": [],
10 "source": [
11 "import numpy as np\n",
12 "import bqplot.pyplot as plt\n",
13 "from bqplot import LogScale, LinearScale, OrdinalScale, ColorScale"
14 ]
15 },
16 {
17 "cell_type": "markdown",
18 "metadata": {},
19 "source": [
20 "## Multiple marks in a single figure"
21 ]
22 },
23 {
24 "cell_type": "code",
25 "execution_count": null,
26 "metadata": {},
27 "outputs": [],
28 "source": [
29 "fig = plt.figure(title='Multiple Marks')\n",
30 "\n",
31 "# create multiple marks. they'll be added to the same figure\n",
32 "bar_chart = plt.bar([1, 2, 3, 4, 5], [2, 9, 10, 40, 40])\n",
33 "line_chart = plt.plot([1, 2, 3, 4, 5], [10, 5, 30, 60, 20], 'rs-')\n",
34 "\n",
35 "fig"
36 ]
37 },
38 {
39 "cell_type": "markdown",
40 "metadata": {},
41 "source": [
42 "## Sample Histogram with mid-points set"
43 ]
44 },
45 {
46 "cell_type": "code",
47 "execution_count": null,
48 "metadata": {},
49 "outputs": [],
50 "source": [
51 "fig = plt.figure(title='Histogram')\n",
52 "hist = plt.hist(sample=np.random.randn(100), bins=10, colors=['orange'])\n",
53 "fig"
54 ]
55 },
56 {
57 "cell_type": "code",
58 "execution_count": null,
59 "metadata": {},
60 "outputs": [],
61 "source": [
62 "x_axis = plt.axes()['sample']\n",
63 "\n",
64 "# Set the tick values to be the mid points of the bins\n",
65 "x_axis.tick_values = hist.midpoints"
66 ]
67 },
68 {
69 "cell_type": "markdown",
70 "metadata": {},
71 "source": [
72 "## Line Chart Log Scale"
73 ]
74 },
75 {
76 "cell_type": "code",
77 "execution_count": null,
78 "metadata": {},
79 "outputs": [],
80 "source": [
81 "dates = np.arange('2005-02', '2005-03', dtype='datetime64[D]')\n",
82 "size = len(dates)\n",
83 "prices = 100 + 5 * np.cumsum(np.random.randn(size))"
84 ]
85 },
86 {
87 "cell_type": "code",
88 "execution_count": null,
89 "metadata": {},
90 "outputs": [],
91 "source": [
92 "fig = plt.figure()\n",
93 "plt.scales(scales={'y': LogScale()})\n",
94 "axes_options = {'x': dict(label='Date', grid_lines='dashed', num_ticks=4),\n",
95 " 'y': dict(label='Log Price', tick_format='0.1f')}\n",
96 "logline = plt.plot(dates, prices, 'm', axes_options=axes_options)\n",
97 "fig"
98 ]
99 },
100 {
101 "cell_type": "markdown",
102 "metadata": {},
103 "source": [
104 "## Setting min and max along an axis for plots"
105 ]
106 },
107 {
108 "cell_type": "code",
109 "execution_count": null,
110 "metadata": {},
111 "outputs": [],
112 "source": [
113 "fig = plt.figure()\n",
114 "line = plt.plot(np.arange(100), np.random.randn(100).cumsum(), 'r')\n",
115 "fig"
116 ]
117 },
118 {
119 "cell_type": "code",
120 "execution_count": null,
121 "metadata": {},
122 "outputs": [],
123 "source": [
124 "## changing the min/max od x scale\n",
125 "line.scales['x'].min = -10\n",
126 "line.scales['x'].max = 110"
127 ]
128 },
129 {
130 "cell_type": "markdown",
131 "metadata": {},
132 "source": [
133 "## Marks which do not affect the domain along an axis"
134 ]
135 },
136 {
137 "cell_type": "code",
138 "execution_count": null,
139 "metadata": {},
140 "outputs": [],
141 "source": [
142 "fig = plt.figure()\n",
143 "x = np.arange(50)\n",
144 "y = np.cumsum(np.random.randn(50) * 100.0)\n",
145 "line1 = plt.plot(x, y, 'b')\n",
146 "line2 = plt.plot(x, y * 2, 'r', preserve_domain={'y': True})\n",
147 "fig"
148 ]
149 },
150 {
151 "cell_type": "code",
152 "execution_count": null,
153 "metadata": {},
154 "outputs": [],
155 "source": [
156 "line2.preserve_domain={}"
157 ]
158 },
159 {
160 "cell_type": "markdown",
161 "metadata": {},
162 "source": [
163 "## Preserve domain for color scale"
164 ]
165 },
166 {
167 "cell_type": "code",
168 "execution_count": null,
169 "metadata": {},
170 "outputs": [],
171 "source": [
172 "x = np.arange(50)\n",
173 "y = np.cumsum(np.random.randn(50) * 100.0)\n",
174 "\n",
175 "fig = plt.figure()\n",
176 "plt.scales(scales={'color': ColorScale(colors=['red', 'white', 'green'], mid=0.0)})\n",
177 "scat1 = plt.scatter(x, y, color=y)\n",
178 "scat2 = plt.scatter(x, y * 2, color=y * 2, preserve_domain={'color': True}, marker='cross')\n",
179 "fig"
180 ]
181 },
182 {
183 "cell_type": "markdown",
184 "metadata": {},
185 "source": [
186 "## Reversing a scale"
187 ]
188 },
189 {
190 "cell_type": "code",
191 "execution_count": null,
192 "metadata": {},
193 "outputs": [],
194 "source": [
195 "x = np.arange(50)\n",
196 "y = np.cumsum(np.random.randn(50) * 100.0)\n",
197 "\n",
198 "fig = plt.figure()\n",
199 "plt.scales(scales={'x': LinearScale(reverse=True)})\n",
200 "line = plt.plot(x, y, 'b')\n",
201 "fig"
202 ]
203 },
204 {
205 "cell_type": "markdown",
206 "metadata": {},
207 "source": [
208 "## Fixing the domain of an ordinal scale"
209 ]
210 },
211 {
212 "cell_type": "code",
213 "execution_count": null,
214 "metadata": {},
215 "outputs": [],
216 "source": [
217 "x = np.arange(10)\n",
218 "y = np.cumsum(np.random.randn(3, 10), axis=1)\n",
219 "\n",
220 "fig = plt.figure()\n",
221 "axes_options = {'x': dict(set_ticks=True, grid_lines='none'),\n",
222 " 'y': dict(grid_lines='none')}\n",
223 "plt.scales(scales={'x': OrdinalScale(domain=list(range(20)))})\n",
224 "bar_chart = plt.bar(x, y=y, \n",
225 " colors=['hotpink', 'orange', 'green'],\n",
226 " labels=['One', 'Two', 'Three'],\n",
227 " axes_options=axes_options,\n",
228 " display_legend=True)\n",
229 "fig"
230 ]
231 },
232 {
233 "cell_type": "markdown",
234 "metadata": {},
235 "source": [
236 "## Applying clip to marks"
237 ]
238 },
239 {
240 "cell_type": "code",
241 "execution_count": null,
242 "metadata": {},
243 "outputs": [],
244 "source": [
245 "x = np.arange(100)\n",
246 "y1, y2 = np.random.randn(2, 100).cumsum(axis=1)\n",
247 "\n",
248 "fig = plt.figure()\n",
249 "plt.scales(scales={'x': LinearScale(min=10, max=90)})\n",
250 "line1 = plt.plot(x, y1, 'r', labels=['Clipped Line'], display_legend=True)\n",
251 "line2 = plt.plot(x, y2, 'g', apply_clip=False, labels=['Non clipped line'], display_legend=True)\n",
252 "fig"
253 ]
254 }
255 ],
256 "metadata": {
257 "anaconda-cloud": {},
258 "kernelspec": {
259 "display_name": "Python 3",
260 "language": "python",
261 "name": "python3"
262 },
263 "language_info": {
264 "codemirror_mode": {
265 "name": "ipython",
266 "version": 3
267 },
268 "file_extension": ".py",
269 "mimetype": "text/x-python",
270 "name": "python",
271 "nbconvert_exporter": "python",
272 "pygments_lexer": "ipython3",
273 "version": "3.7.6"
274 }
275 },
276 "nbformat": 4,
277 "nbformat_minor": 4
278 }