Skip to content

Commit 2cc411a

Browse files
committed
Updated axis_grid1 examples
1 parent d633b76 commit 2cc411a

File tree

1 file changed

+92
-111
lines changed

1 file changed

+92
-111
lines changed

AnatomyOfMatplotlib-Part6-mpl_toolkits.ipynb

Lines changed: 92 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -109,64 +109,57 @@
109109
},
110110
"outputs": [],
111111
"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",
123123
"from mpl_toolkits.axes_grid1 import host_subplot\n",
124124
"import mpl_toolkits.axisartist as AA\n",
125125
"import matplotlib.pyplot as plt\n",
126126
"\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",
134129
"\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",
142132
"\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",
143138
"\n",
139+
"par2.axis[\"right\"].toggle(all=True)\n",
144140
"\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",
147143
"\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",
152148
"\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",
156152
"\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",
159155
"\n",
160-
" host.legend()\n",
156+
"host.legend()\n",
161157
"\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",
165161
"\n",
166-
" plt.draw()\n",
167-
" plt.show()\n",
168-
"\n",
169-
" #plt.savefig(\"Test\")\n"
162+
"plt.show()\n"
170163
]
171164
},
172165
{
@@ -184,34 +177,37 @@
184177
},
185178
"outputs": [],
186179
"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",
198193
"from matplotlib.transforms import Affine2D\n",
199-
"\n",
200194
"import mpl_toolkits.axisartist.floating_axes as floating_axes\n",
201-
"\n",
202195
"import numpy as np\n",
203196
"import mpl_toolkits.axisartist.angle_helper as angle_helper\n",
204197
"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",
207202
"\n",
208203
"def setup_axes1(fig, rect):\n",
209204
" \"\"\"\n",
210205
" A simple one.\n",
211206
" \"\"\"\n",
212207
" tr = Affine2D().scale(2, 1).rotate_deg(30)\n",
213208
"\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",
215211
"\n",
216212
" ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)\n",
217213
" fig.add_subplot(ax1)\n",
@@ -229,9 +225,6 @@
229225
" With custom locator and formatter.\n",
230226
" Note that the extreme values are swapped.\n",
231227
" \"\"\"\n",
232-
"\n",
233-
" #tr_scale = Affine2D().scale(np.pi/180., 1.)\n",
234-
"\n",
235228
" tr = PolarAxes.PolarTransform()\n",
236229
"\n",
237230
" pi = np.pi\n",
@@ -243,25 +236,24 @@
243236
"\n",
244237
" grid_locator2 = MaxNLocator(2)\n",
245238
"\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",
253245
"\n",
254246
" ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)\n",
255247
" fig.add_subplot(ax1)\n",
256248
"\n",
257249
" # create a parasite axes whose transData in RA, cz\n",
258250
" aux_ax = ax1.get_aux_axes(tr)\n",
259251
"\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",
265257
"\n",
266258
" return ax1, aux_ax\n",
267259
"\n",
@@ -286,13 +278,12 @@
286278
"\n",
287279
" ra0, ra1 = 8.*15, 14.*15\n",
288280
" 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",
296287
"\n",
297288
" ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)\n",
298289
" fig.add_subplot(ax1)\n",
@@ -310,47 +301,37 @@
310301
" ax1.axis[\"left\"].label.set_text(r\"cz [km$^{-1}$]\")\n",
311302
" ax1.axis[\"top\"].label.set_text(r\"$\\alpha_{1950}$\")\n",
312303
"\n",
313-
"\n",
314304
" # create a parasite axes whose transData in RA, cz\n",
315305
" aux_ax = ax1.get_aux_axes(tr)\n",
316306
"\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",
322312
"\n",
323313
" return ax1, aux_ax\n",
324314
"\n",
325315
"\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",
326319
"\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",
345322
"\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",
346327
"\n",
347-
" ax3, aux_ax3 = setup_axes3(fig, 133)\n",
328+
"ax3, aux_ax3 = setup_axes3(fig, 133)\n",
348329
"\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",
352333
"\n",
353-
" plt.show()"
334+
"plt.show()\n"
354335
]
355336
},
356337
{

0 commit comments

Comments
 (0)