Skip to content

Commit a0aa9a8

Browse files
committed
AxesGrid: add modified version of colorbar
svn path=/trunk/matplotlib/; revision=7753
1 parent 4e2e1f9 commit a0aa9a8

File tree

15 files changed

+1192
-26
lines changed

15 files changed

+1192
-26
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2009-09-13 AxesGrid : add modified version of colorbar. Add colorbar
2+
location howto. - JJL
3+
14
2009-09-07 AxesGrid : implemented axisline style.
25
Added a demo examples/axes_grid/demo_axisline_style.py- JJL
36

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import matplotlib.pyplot as plt
2+
3+
from mpl_toolkits.axes_grid.inset_locator import inset_axes, zoomed_inset_axes
4+
from mpl_toolkits.axes_grid.colorbar import colorbar
5+
6+
def get_demo_image():
7+
from matplotlib.cbook import get_sample_data
8+
import numpy as np
9+
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
10+
z = np.load(f)
11+
# z is a numpy array of 15x15
12+
return z, (-3,4,-4,3)
13+
14+
15+
fig = plt.figure(1, [5,4])
16+
ax = fig.add_subplot(111)
17+
18+
Z, extent = get_demo_image()
19+
20+
ax.set(aspect=1,
21+
xlim=(-15, 15),
22+
ylim=(-20, 5))
23+
24+
25+
axins = zoomed_inset_axes(ax, 2, loc=2) # zoom = 6
26+
im = axins.imshow(Z, extent=extent, interpolation="nearest",
27+
origin="lower")
28+
29+
plt.xticks(visible=False)
30+
plt.yticks(visible=False)
31+
32+
33+
# colorbar
34+
cax = inset_axes(axins,
35+
width="5%", # width = 10% of parent_bbox width
36+
height="100%", # height : 50%
37+
loc=3,
38+
bbox_to_anchor=(1.05, 0., 1, 1),
39+
bbox_transform=axins.transAxes,
40+
borderpad=0,
41+
)
42+
43+
44+
colorbar(im, cax=cax) #, ticks=[1,2,3])
45+
46+
47+
plt.draw()
48+
plt.show()
49+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import matplotlib.pyplot as plt
2+
from mpl_toolkits.axes_grid.axes_divider import make_axes_locatable
3+
4+
from mpl_toolkits.axes_grid.colorbar import colorbar
5+
# from matplotlib.pyplot import colorbar
6+
7+
fig = plt.figure(1, figsize=(6, 3))
8+
fig.subplots_adjust(wspace=0.5)
9+
10+
ax1 = fig.add_subplot(121)
11+
im1 = ax1.imshow([[1,2],[3,4]])
12+
13+
ax1_divider = make_axes_locatable(ax1)
14+
cax1 = ax1_divider.append_axes("right", size="7%", pad="2%")
15+
cb1 = colorbar(im1, cax=cax1)
16+
17+
ax2 = fig.add_subplot(122)
18+
im2 = ax2.imshow([[1,2],[3,4]])
19+
20+
ax2_divider = make_axes_locatable(ax2)
21+
cax2 = ax2_divider.append_axes("top", size="7%", pad="2%")
22+
cb2 = colorbar(im2, cax=cax2, orientation="horizontal")
23+
cax2.xaxis.set_ticks_position("top")
24+
plt.show()
25+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import matplotlib.pyplot as plt
2+
3+
from mpl_toolkits.axes_grid.inset_locator import inset_axes
4+
from mpl_toolkits.axes_grid.colorbar import colorbar
5+
6+
fig = plt.figure(1, [6, 3])
7+
8+
# first subplot
9+
ax1 = fig.add_subplot(121)
10+
11+
axins1 = inset_axes(ax1,
12+
width="50%", # width = 10% of parent_bbox width
13+
height="5%", # height : 50%
14+
loc=1)
15+
16+
im1=ax1.imshow([[1,2],[2, 3]])
17+
colorbar(im1, cax=axins1, orientation="horizontal", ticks=[1,2,3])
18+
axins1.xaxis.set_ticks_position("bottom")
19+
20+
# first subplot
21+
ax = fig.add_subplot(122)
22+
23+
axins = inset_axes(ax,
24+
width="5%", # width = 10% of parent_bbox width
25+
height="50%", # height : 50%
26+
loc=3,
27+
bbox_to_anchor=(1.05, 0., 1, 1),
28+
bbox_transform=ax.transAxes,
29+
borderpad=0,
30+
)
31+
32+
# Controlling the placement of the inset axes is basically same as that
33+
# of the legend. you may want to play with the borderpad value and
34+
# the bbox_to_anchor coordinate.
35+
36+
im=ax.imshow([[1,2],[2, 3]])
37+
colorbar(im, cax=axins, ticks=[1,2,3])
38+
39+
plt.draw()
40+
plt.show()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import matplotlib.pyplot as plt
2+
3+
plt.rcParams["text.usetex"]=False
4+
5+
fig = plt.figure(1, figsize=(6, 3))
6+
7+
ax1 = fig.add_subplot(121)
8+
im1 = ax1.imshow([[1,2],[3,4]])
9+
cb1 = plt.colorbar(im1)
10+
cb1.ax.set_yticks([1, 3])
11+
ax1.set_title("Original MPL's colorbar w/\nset_yticks([1,3])", size=10)
12+
13+
from mpl_toolkits.axes_grid.colorbar import colorbar
14+
ax2 = fig.add_subplot(122)
15+
im2 = ax2.imshow([[1,2],[3,4]])
16+
cb2 = colorbar(im2)
17+
cb2.ax.set_yticks([1, 3])
18+
ax2.set_title("AxesGrid's colorbar w/\nset_yticks([1,3])", size=10)
19+
20+
plt.show()
21+

doc/mpl_toolkits/axes_grid/index.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ The matplotlib AxesGrid toolkit is a collection of helper classes to
88
ease displaying multiple images in matplotlib. While the aspect
99
parameter in matplotlib adjust the position of the single axes,
1010
AxesGrid toolkit provides a framework to adjust the position of
11-
multiple axes according to their aspects.
11+
multiple axes according to their aspects.
1212

1313

14+
.. image:: ../../_static/demo_axes_grid.png
15+
1416

1517
Documentation
1618
=============
@@ -19,4 +21,5 @@ Documentation
1921
:maxdepth: 2
2022

2123
users/index.rst
24+
howtos/index.rst
2225
api/index.rst

doc/mpl_toolkits/axes_grid/users/axes_divider.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ may consider it as [0:2, 1].
8181

8282
See the example,
8383

84-
.. plot:: mpl_toolkits/axes_grid/figures/simple_axes_divider2.py
84+
.. plot:: mpl_toolkits/axes_grid/examples/simple_axes_divider2.py
8585
:include-source:
8686

8787
You can adjust the size of the each axes accroding to their x or y
8888
data limits (AxesX and AxesY), similar to the axes aspect parameter.
8989

90-
.. plot:: mpl_toolkits/axes_grid/figures/simple_axes_divider3.py
90+
.. plot:: mpl_toolkits/axes_grid/examples/simple_axes_divider3.py
9191
:include-source:
9292

doc/mpl_toolkits/axes_grid/users/axislines.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ In summary, all these changes was to support
2929
* a curvelinear grid.
3030
* a floating axis
3131

32-
.. plot:: mpl_toolkits/axes_grid/figures/demo_floating_axis.py
32+
.. plot:: mpl_toolkits/axes_grid/examples/demo_floating_axis.py
3333

3434

3535
*axes_grid.axislines.Axes* defines a *axis* attribute, which is a
@@ -211,7 +211,7 @@ coordinates, or you may use Parasite Axes for convenience.::
211211
ax1.parasites.append(ax2)
212212

213213

214-
.. plot:: mpl_toolkits/axes_grid/figures/demo_curvelinear_grid.py
214+
.. plot:: mpl_toolkits/axes_grid/examples/demo_curvelinear_grid.py
215215

216216

217217

doc/mpl_toolkits/axes_grid/users/overview.rst

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ ratio. For example, displaying images of a same size with some fixed
3535
padding between them cannot be easily done in matplotlib. AxesGrid is
3636
used in such case.
3737

38-
.. plot:: mpl_toolkits/axes_grid/figures/simple_axesgrid.py
38+
.. plot:: mpl_examples/axes_grid/simple_axesgrid.py
3939
:include-source:
4040

4141
* The postion of each axes is determined at the drawing time (see
@@ -49,7 +49,7 @@ used in such case.
4949
height. The widths (height) of the axes in the same row (column) are
5050
scaled according to their view limits (xlim or ylim).
5151

52-
.. plot:: mpl_toolkits/axes_grid/figures/simple_axesgrid2.py
52+
.. plot:: mpl_toolkits/axes_grid/examples/simple_axesgrid2.py
5353
:include-source:
5454

5555
* xaxis are shared among axes in a same column. Similarly, yaxis are
@@ -137,7 +137,7 @@ attribute.
137137

138138
The examples below show what you can do with AxesGrid.
139139

140-
.. plot:: mpl_toolkits/axes_grid/figures/demo_axes_grid.py
140+
.. plot:: mpl_toolkits/axes_grid/examples/demo_axes_grid.py
141141

142142

143143
RGB Axes
@@ -158,7 +158,7 @@ yaxis of each axes are shared. ::
158158
origin="lower", interpolation="nearest")
159159

160160

161-
.. plot:: mpl_toolkits/axes_grid/figures/simple_rgb.py
161+
.. plot:: mpl_toolkits/axes_grid/examples/simple_rgb.py
162162

163163

164164

@@ -219,7 +219,7 @@ The "scatter_hist.py" example in mpl can be rewritten using
219219
See the full source code below.
220220

221221

222-
.. plot:: mpl_toolkits/axes_grid/figures/scatter_hist.py
222+
.. plot:: mpl_toolkits/axes_grid/examples/scatter_hist.py
223223

224224

225225
The scatter_hist using the AxesDivider has some advantage over the
@@ -246,7 +246,7 @@ axes.
246246
Example 1. twinx
247247
----------------
248248

249-
.. plot:: mpl_toolkits/axes_grid/figures/parasite_simple.py
249+
.. plot:: mpl_toolkits/axes_grid/examples/parasite_simple.py
250250
:include-source:
251251

252252
Example 2. twin
@@ -257,7 +257,7 @@ x-limit in the host axes, the x-limit of the parasite axes will change
257257
accordingly.
258258

259259

260-
.. plot:: mpl_toolkits/axes_grid/figures/parasite_simple2.py
260+
.. plot:: mpl_toolkits/axes_grid/examples/parasite_simple2.py
261261

262262

263263

@@ -290,13 +290,13 @@ For example, you can hide right, and top axis by ::
290290
ax.axis["top"].set_visible(False)
291291

292292

293-
.. plot:: mpl_toolkits/axes_grid/figures/simple_axisline3.py
293+
.. plot:: mpl_toolkits/axes_grid/examples/simple_axisline3.py
294294

295295

296296
SubplotZero gives you two more additional (floating?) axis of x=0 and
297297
y=0 (in data coordinate)
298298

299-
.. plot:: mpl_toolkits/axes_grid/figures/simple_axisline2.py
299+
.. plot:: mpl_toolkits/axes_grid/examples/simple_axisline2.py
300300
:include-source:
301301

302302

@@ -315,7 +315,7 @@ axis. ::
315315
r"$\pi$", r"$\frac{3}{2}\pi$", r"$2\pi$"])
316316

317317

318-
.. plot:: mpl_toolkits/axes_grid/figures/simple_axisline4.py
318+
.. plot:: mpl_toolkits/axes_grid/examples/simple_axisline4.py
319319

320320

321321
AxisLine Axes lets you create a custom axis, ::
@@ -330,7 +330,7 @@ AxisLine Axes lets you create a custom axis, ::
330330
And, you can use it with parasiteAxes.
331331

332332

333-
.. plot:: mpl_toolkits/axes_grid/figures/demo_parasite_axes2.py
333+
.. plot:: mpl_toolkits/axes_grid/examples/demo_parasite_axes2.py
334334

335335

336336
AnchoredArtists
@@ -343,7 +343,7 @@ limited support for an arbitrary transform. For example, the ellipse
343343
in the example below will have width and height in the data
344344
coordinate.
345345

346-
.. plot:: mpl_toolkits/axes_grid/figures/simple_anchored_artists.py
346+
.. plot:: mpl_toolkits/axes_grid/examples/simple_anchored_artists.py
347347
:include-source:
348348

349349

@@ -377,15 +377,15 @@ some factor. For example, ::
377377
creates an inset axes whose data scale is half of the parent axes.
378378
Here is complete examples.
379379

380-
.. plot:: mpl_toolkits/axes_grid/figures/inset_locator_demo.py
380+
.. plot:: mpl_toolkits/axes_grid/examples/inset_locator_demo.py
381381

382382
For example, :func:`zoomed_inset_axes` can be used when you want the
383383
inset represents the zoom-up of the small portion in the parent axes.
384384
And :mod:`~mpl_toolkits/axes_grid/inset_locator` provides a helper
385385
function :func:`mark_inset` to mark the location of the area
386386
represented by the inset axes.
387387

388-
.. plot:: mpl_toolkits/axes_grid/figures/inset_locator_demo2.py
388+
.. plot:: mpl_toolkits/axes_grid/examples/inset_locator_demo2.py
389389
:include-source:
390390

391391

@@ -395,6 +395,6 @@ Curvelinear Grid
395395
You can draw a cuvelinear grid and ticks. Also a floating axis can be
396396
created. See :ref:`axislines-manual` for more details.
397397

398-
.. plot:: mpl_toolkits/axes_grid/figures/demo_floating_axis.py
398+
.. plot:: mpl_toolkits/axes_grid/examples/demo_floating_axis.py
399399

400400

examples/axes_grid/demo_axes_grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def demo_grid_with_single_cbar(fig):
4444
Z, extent = get_demo_image()
4545
for i in range(4):
4646
im = grid[i].imshow(Z, extent=extent, interpolation="nearest")
47-
plt.colorbar(im, cax = grid.cbar_axes[0])
47+
#plt.colorbar(im, cax = grid.cbar_axes[0])
4848
grid.cbar_axes[0].colorbar(im)
4949

5050
# This affects all axes as share_all = True.

0 commit comments

Comments
 (0)