|
109 | 109 | },
|
110 | 110 | "outputs": [],
|
111 | 111 | "source": [
|
112 |
| - "%load http://matplotlib.org/mpl_examples/axes_grid/demo_parasite_axes2.py" |
113 |
| - ] |
114 |
| - }, |
115 |
| - { |
116 |
| - "cell_type": "code", |
117 |
| - "execution_count": null, |
118 |
| - "metadata": { |
119 |
| - "collapsed": false |
120 |
| - }, |
121 |
| - "outputs": [], |
122 |
| - "source": [ |
| 112 | + "# %load http://matplotlib.org/mpl_examples/axes_grid/demo_parasite_axes2.py\n", |
| 113 | + "\"\"\"\n", |
| 114 | + "Parasite axis demo\n", |
| 115 | + "\n", |
| 116 | + "The following code is an example of a parasite axis. It aims to show a user how\n", |
| 117 | + "to plot multiple different values onto one single plot. Notice how in this\n", |
| 118 | + "example, par1 and par2 are both calling twinx meaning both are tied directly to\n", |
| 119 | + "the x-axis. From there, each of those two axis can behave separately from the\n", |
| 120 | + "each other, meaning they can take on separate values from themselves as well as\n", |
| 121 | + "the x-axis.\n", |
| 122 | + "\"\"\"\n", |
123 | 123 | "from mpl_toolkits.axes_grid1 import host_subplot\n",
|
124 | 124 | "import mpl_toolkits.axisartist as AA\n",
|
125 | 125 | "import matplotlib.pyplot as plt\n",
|
126 | 126 | "\n",
|
127 |
| - "if 1:\n", |
128 |
| - "\n", |
129 |
| - " host = host_subplot(111, axes_class=AA.Axes)\n", |
130 |
| - " plt.subplots_adjust(right=0.75)\n", |
131 |
| - "\n", |
132 |
| - " par1 = host.twinx()\n", |
133 |
| - " par2 = host.twinx()\n", |
| 127 | + "host = host_subplot(111, axes_class=AA.Axes)\n", |
| 128 | + "plt.subplots_adjust(right=0.75)\n", |
134 | 129 | "\n",
|
135 |
| - " offset = 60\n", |
136 |
| - " new_fixed_axis = par2.get_grid_helper().new_fixed_axis\n", |
137 |
| - " par2.axis[\"right\"] = new_fixed_axis(loc=\"right\",\n", |
138 |
| - " axes=par2,\n", |
139 |
| - " offset=(offset, 0))\n", |
140 |
| - " \n", |
141 |
| - " par2.axis[\"right\"].toggle(all=True)\n", |
| 130 | + "par1 = host.twinx()\n", |
| 131 | + "par2 = host.twinx()\n", |
142 | 132 | "\n",
|
| 133 | + "offset = 60\n", |
| 134 | + "new_fixed_axis = par2.get_grid_helper().new_fixed_axis\n", |
| 135 | + "par2.axis[\"right\"] = new_fixed_axis(loc=\"right\",\n", |
| 136 | + " axes=par2,\n", |
| 137 | + " offset=(offset, 0))\n", |
143 | 138 | "\n",
|
| 139 | + "par2.axis[\"right\"].toggle(all=True)\n", |
144 | 140 | "\n",
|
145 |
| - " host.set_xlim(0, 2)\n", |
146 |
| - " host.set_ylim(0, 2)\n", |
| 141 | + "host.set_xlim(0, 2)\n", |
| 142 | + "host.set_ylim(0, 2)\n", |
147 | 143 | "\n",
|
148 |
| - " host.set_xlabel(\"Distance\")\n", |
149 |
| - " host.set_ylabel(\"Density\")\n", |
150 |
| - " par1.set_ylabel(\"Temperature\")\n", |
151 |
| - " par2.set_ylabel(\"Velocity\")\n", |
| 144 | + "host.set_xlabel(\"Distance\")\n", |
| 145 | + "host.set_ylabel(\"Density\")\n", |
| 146 | + "par1.set_ylabel(\"Temperature\")\n", |
| 147 | + "par2.set_ylabel(\"Velocity\")\n", |
152 | 148 | "\n",
|
153 |
| - " p1, = host.plot([0, 1, 2], [0, 1, 2], label=\"Density\")\n", |
154 |
| - " p2, = par1.plot([0, 1, 2], [0, 3, 2], label=\"Temperature\")\n", |
155 |
| - " p3, = par2.plot([0, 1, 2], [50, 30, 15], label=\"Velocity\")\n", |
| 149 | + "p1, = host.plot([0, 1, 2], [0, 1, 2], label=\"Density\")\n", |
| 150 | + "p2, = par1.plot([0, 1, 2], [0, 3, 2], label=\"Temperature\")\n", |
| 151 | + "p3, = par2.plot([0, 1, 2], [50, 30, 15], label=\"Velocity\")\n", |
156 | 152 | "\n",
|
157 |
| - " par1.set_ylim(0, 4)\n", |
158 |
| - " par2.set_ylim(1, 65)\n", |
| 153 | + "par1.set_ylim(0, 4)\n", |
| 154 | + "par2.set_ylim(1, 65)\n", |
159 | 155 | "\n",
|
160 |
| - " host.legend()\n", |
| 156 | + "host.legend()\n", |
161 | 157 | "\n",
|
162 |
| - " host.axis[\"left\"].label.set_color(p1.get_color())\n", |
163 |
| - " par1.axis[\"right\"].label.set_color(p2.get_color())\n", |
164 |
| - " par2.axis[\"right\"].label.set_color(p3.get_color())\n", |
| 158 | + "host.axis[\"left\"].label.set_color(p1.get_color())\n", |
| 159 | + "par1.axis[\"right\"].label.set_color(p2.get_color())\n", |
| 160 | + "par2.axis[\"right\"].label.set_color(p3.get_color())\n", |
165 | 161 | "\n",
|
166 |
| - " plt.draw()\n", |
167 |
| - " plt.show()\n", |
168 |
| - "\n", |
169 |
| - " #plt.savefig(\"Test\")\n" |
| 162 | + "plt.show()\n" |
170 | 163 | ]
|
171 | 164 | },
|
172 | 165 | {
|
|
184 | 177 | },
|
185 | 178 | "outputs": [],
|
186 | 179 | "source": [
|
187 |
| - "%load http://matplotlib.org/mpl_toolkits/axes_grid/examples/demo_floating_axes.py" |
188 |
| - ] |
189 |
| - }, |
190 |
| - { |
191 |
| - "cell_type": "code", |
192 |
| - "execution_count": null, |
193 |
| - "metadata": { |
194 |
| - "collapsed": false |
195 |
| - }, |
196 |
| - "outputs": [], |
197 |
| - "source": [ |
| 180 | + "# %load http://matplotlib.org/mpl_toolkits/axes_grid/examples/demo_floating_axes.py\n", |
| 181 | + "\"\"\"\n", |
| 182 | + "Demo of the floating axes.\n", |
| 183 | + "\n", |
| 184 | + "This demo shows features of functions in floating_axes:\n", |
| 185 | + " * Using scatter function and bar function with changing the\n", |
| 186 | + " shape of the plot.\n", |
| 187 | + " * Using GridHelperCurveLinear to rotate the plot and set the\n", |
| 188 | + " boundary of the plot.\n", |
| 189 | + " * Using FloatingSubplot to create a subplot using the return\n", |
| 190 | + " value from GridHelperCurveLinear.\n", |
| 191 | + " * Making sector plot by adding more features to GridHelperCurveLinear.\n", |
| 192 | + "\"\"\"\n", |
198 | 193 | "from matplotlib.transforms import Affine2D\n",
|
199 |
| - "\n", |
200 | 194 | "import mpl_toolkits.axisartist.floating_axes as floating_axes\n",
|
201 |
| - "\n", |
202 | 195 | "import numpy as np\n",
|
203 | 196 | "import mpl_toolkits.axisartist.angle_helper as angle_helper\n",
|
204 | 197 | "from matplotlib.projections import PolarAxes\n",
|
205 |
| - "from mpl_toolkits.axisartist.grid_finder import FixedLocator, MaxNLocator, \\\n", |
206 |
| - " DictFormatter\n", |
| 198 | + "from mpl_toolkits.axisartist.grid_finder import (FixedLocator, MaxNLocator,\n", |
| 199 | + " DictFormatter)\n", |
| 200 | + "import matplotlib.pyplot as plt\n", |
| 201 | + "\n", |
207 | 202 | "\n",
|
208 | 203 | "def setup_axes1(fig, rect):\n",
|
209 | 204 | " \"\"\"\n",
|
210 | 205 | " A simple one.\n",
|
211 | 206 | " \"\"\"\n",
|
212 | 207 | " tr = Affine2D().scale(2, 1).rotate_deg(30)\n",
|
213 | 208 | "\n",
|
214 |
| - " grid_helper = floating_axes.GridHelperCurveLinear(tr, extremes=(0, 4, 0, 4))\n", |
| 209 | + " grid_helper = floating_axes.GridHelperCurveLinear(\n", |
| 210 | + " tr, extremes=(-0.5, 3.5, 0, 4))\n", |
215 | 211 | "\n",
|
216 | 212 | " ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)\n",
|
217 | 213 | " fig.add_subplot(ax1)\n",
|
|
229 | 225 | " With custom locator and formatter.\n",
|
230 | 226 | " Note that the extreme values are swapped.\n",
|
231 | 227 | " \"\"\"\n",
|
232 |
| - "\n", |
233 |
| - " #tr_scale = Affine2D().scale(np.pi/180., 1.)\n", |
234 |
| - "\n", |
235 | 228 | " tr = PolarAxes.PolarTransform()\n",
|
236 | 229 | "\n",
|
237 | 230 | " pi = np.pi\n",
|
|
243 | 236 | "\n",
|
244 | 237 | " grid_locator2 = MaxNLocator(2)\n",
|
245 | 238 | "\n",
|
246 |
| - " grid_helper = floating_axes.GridHelperCurveLinear(tr,\n", |
247 |
| - " extremes=(.5*pi, 0, 2, 1),\n", |
248 |
| - " grid_locator1=grid_locator1,\n", |
249 |
| - " grid_locator2=grid_locator2,\n", |
250 |
| - " tick_formatter1=tick_formatter1,\n", |
251 |
| - " tick_formatter2=None,\n", |
252 |
| - " )\n", |
| 239 | + " grid_helper = floating_axes.GridHelperCurveLinear(\n", |
| 240 | + " tr, extremes=(.5*pi, 0, 2, 1),\n", |
| 241 | + " grid_locator1=grid_locator1,\n", |
| 242 | + " grid_locator2=grid_locator2,\n", |
| 243 | + " tick_formatter1=tick_formatter1,\n", |
| 244 | + " tick_formatter2=None)\n", |
253 | 245 | "\n",
|
254 | 246 | " ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)\n",
|
255 | 247 | " fig.add_subplot(ax1)\n",
|
256 | 248 | "\n",
|
257 | 249 | " # create a parasite axes whose transData in RA, cz\n",
|
258 | 250 | " aux_ax = ax1.get_aux_axes(tr)\n",
|
259 | 251 | "\n",
|
260 |
| - " aux_ax.patch = ax1.patch # for aux_ax to have a clip path as in ax\n", |
261 |
| - " ax1.patch.zorder=0.9 # but this has a side effect that the patch is\n", |
262 |
| - " # drawn twice, and possibly over some other\n", |
263 |
| - " # artists. So, we decrease the zorder a bit to\n", |
264 |
| - " # prevent this.\n", |
| 252 | + " aux_ax.patch = ax1.patch # for aux_ax to have a clip path as in ax\n", |
| 253 | + " ax1.patch.zorder = 0.9 # but this has a side effect that the patch is\n", |
| 254 | + " # drawn twice, and possibly over some other\n", |
| 255 | + " # artists. So, we decrease the zorder a bit to\n", |
| 256 | + " # prevent this.\n", |
265 | 257 | "\n",
|
266 | 258 | " return ax1, aux_ax\n",
|
267 | 259 | "\n",
|
|
286 | 278 | "\n",
|
287 | 279 | " ra0, ra1 = 8.*15, 14.*15\n",
|
288 | 280 | " cz0, cz1 = 0, 14000\n",
|
289 |
| - " grid_helper = floating_axes.GridHelperCurveLinear(tr,\n", |
290 |
| - " extremes=(ra0, ra1, cz0, cz1),\n", |
291 |
| - " grid_locator1=grid_locator1,\n", |
292 |
| - " grid_locator2=grid_locator2,\n", |
293 |
| - " tick_formatter1=tick_formatter1,\n", |
294 |
| - " tick_formatter2=None,\n", |
295 |
| - " )\n", |
| 281 | + " grid_helper = floating_axes.GridHelperCurveLinear(\n", |
| 282 | + " tr, extremes=(ra0, ra1, cz0, cz1),\n", |
| 283 | + " grid_locator1=grid_locator1,\n", |
| 284 | + " grid_locator2=grid_locator2,\n", |
| 285 | + " tick_formatter1=tick_formatter1,\n", |
| 286 | + " tick_formatter2=None)\n", |
296 | 287 | "\n",
|
297 | 288 | " ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)\n",
|
298 | 289 | " fig.add_subplot(ax1)\n",
|
|
310 | 301 | " ax1.axis[\"left\"].label.set_text(r\"cz [km$^{-1}$]\")\n",
|
311 | 302 | " ax1.axis[\"top\"].label.set_text(r\"$\\alpha_{1950}$\")\n",
|
312 | 303 | "\n",
|
313 |
| - "\n", |
314 | 304 | " # create a parasite axes whose transData in RA, cz\n",
|
315 | 305 | " aux_ax = ax1.get_aux_axes(tr)\n",
|
316 | 306 | "\n",
|
317 |
| - " aux_ax.patch = ax1.patch # for aux_ax to have a clip path as in ax\n", |
318 |
| - " ax1.patch.zorder=0.9 # but this has a side effect that the patch is\n", |
319 |
| - " # drawn twice, and possibly over some other\n", |
320 |
| - " # artists. So, we decrease the zorder a bit to\n", |
321 |
| - " # prevent this.\n", |
| 307 | + " aux_ax.patch = ax1.patch # for aux_ax to have a clip path as in ax\n", |
| 308 | + " ax1.patch.zorder = 0.9 # but this has a side effect that the patch is\n", |
| 309 | + " # drawn twice, and possibly over some other\n", |
| 310 | + " # artists. So, we decrease the zorder a bit to\n", |
| 311 | + " # prevent this.\n", |
322 | 312 | "\n",
|
323 | 313 | " return ax1, aux_ax\n",
|
324 | 314 | "\n",
|
325 | 315 | "\n",
|
| 316 | + "##########################################################\n", |
| 317 | + "fig = plt.figure(figsize=(8, 4))\n", |
| 318 | + "fig.subplots_adjust(wspace=0.3, left=0.05, right=0.95)\n", |
326 | 319 | "\n",
|
327 |
| - "if 1:\n", |
328 |
| - " import matplotlib.pyplot as plt\n", |
329 |
| - " fig = plt.figure(1, figsize=(8, 4))\n", |
330 |
| - " fig.subplots_adjust(wspace=0.3, left=0.05, right=0.95)\n", |
331 |
| - "\n", |
332 |
| - " ax1, aux_ax2 = setup_axes1(fig, 131)\n", |
333 |
| - " aux_ax2.bar([0, 1, 2, 3], [3, 2, 1, 3])\n", |
334 |
| - " \n", |
335 |
| - " #theta = np.random.rand(10) #*.5*np.pi\n", |
336 |
| - " #radius = np.random.rand(10) #+1.\n", |
337 |
| - " #aux_ax1.scatter(theta, radius)\n", |
338 |
| - "\n", |
339 |
| - "\n", |
340 |
| - " ax2, aux_ax2 = setup_axes2(fig, 132)\n", |
341 |
| - "\n", |
342 |
| - " theta = np.random.rand(10)*.5*np.pi\n", |
343 |
| - " radius = np.random.rand(10)+1.\n", |
344 |
| - " aux_ax2.scatter(theta, radius)\n", |
| 320 | + "ax1, aux_ax1 = setup_axes1(fig, 131)\n", |
| 321 | + "aux_ax1.bar([0, 1, 2, 3], [3, 2, 1, 3])\n", |
345 | 322 | "\n",
|
| 323 | + "ax2, aux_ax2 = setup_axes2(fig, 132)\n", |
| 324 | + "theta = np.random.rand(10)*.5*np.pi\n", |
| 325 | + "radius = np.random.rand(10) + 1.\n", |
| 326 | + "aux_ax2.scatter(theta, radius)\n", |
346 | 327 | "\n",
|
347 |
| - " ax3, aux_ax3 = setup_axes3(fig, 133)\n", |
| 328 | + "ax3, aux_ax3 = setup_axes3(fig, 133)\n", |
348 | 329 | "\n",
|
349 |
| - " theta = (8 + np.random.rand(10)*(14-8))*15. # in degrees\n", |
350 |
| - " radius = np.random.rand(10)*14000.\n", |
351 |
| - " aux_ax3.scatter(theta, radius)\n", |
| 330 | + "theta = (8 + np.random.rand(10)*(14 - 8))*15. # in degrees\n", |
| 331 | + "radius = np.random.rand(10)*14000.\n", |
| 332 | + "aux_ax3.scatter(theta, radius)\n", |
352 | 333 | "\n",
|
353 |
| - " plt.show()" |
| 334 | + "plt.show()\n" |
354 | 335 | ]
|
355 | 336 | },
|
356 | 337 | {
|
|
0 commit comments