Skip to content

Commit e182d9e

Browse files
committed
Apply some trickery to get reasonable colorbars
1 parent 6b0ecb3 commit e182d9e

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

examples/soundfigures.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444

4545
# plot and save level of synthesized sound field
4646
plt.figure(figsize=(12.5, 12.5))
47-
im = sfs.plot.level(p, grid, xnorm=[0, -2.2, 0], colorbar=False,
47+
im = sfs.plot.level(p, grid, xnorm=[0, -2.2, 0], colorbar_label='dB',
4848
vmin=-50, vmax=0)
4949
plt.title('Level of Synthesized Sound Field')
50-
cbar = plt.colorbar(im, label='dB', shrink=0.8)
5150
plt.savefig('soundfigure_level.png')

sfs/plot.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Plot sound fields etc."""
2-
2+
from __future__ import division
33
import matplotlib.pyplot as plt
44
from matplotlib.patches import PathPatch
55
from matplotlib.path import Path
66
from matplotlib.collections import PatchCollection
7+
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
78
from mpl_toolkits.mplot3d import Axes3D
89
import numpy as np
910
from . import util
@@ -165,8 +166,9 @@ def loudspeaker_3d(x0, n0, a0=None, w=0.08, h=0.08):
165166
fig.show()
166167

167168

168-
def soundfield(p, grid, xnorm=None, colorbar=True, cmap='coolwarm_clip',
169-
ax=None, xlabel=None, ylabel=None, vmax=2.0, vmin=-2.0,
169+
def soundfield(p, grid, xnorm=None, cmap='coolwarm_clip', colorbar=True,
170+
colorbar_aspect=20, colorbar_pad=0.5, colorbar_label='',
171+
xlabel=None, ylabel=None, ax=None, vmin=-2.0, vmax=2.0,
170172
**kwargs):
171173
"""Two-dimensional plot of sound field.
172174
@@ -217,7 +219,17 @@ def soundfield(p, grid, xnorm=None, colorbar=True, cmap='coolwarm_clip',
217219
:func:`matplotlib.pyplot.ylabel`.
218220
colorbar : bool, optional
219221
If ``False``, no colorbar is created.
220-
ax : Axes
222+
colorbar_aspect : float, optional
223+
Aspect ratio of the colorbar, see
224+
:func:`matplotlib.pyplot.colorbar`.
225+
Strictly speaking, since the colorbar is vertical, it's actually
226+
the inverse of the aspect ratio.
227+
colorbar_pad : float, optional
228+
Space between image plot and colorbar, as a fraction of the
229+
width of the colorbar.
230+
colorbar_label : str, optional
231+
Label of the colorbar.
232+
ax : Axes, optional
221233
If given, the plot is created on `ax` instead of the current
222234
axis (see :func:`matplotlib.pyplot.gca`).
223235
cmap, vmin, vmax, **kwargs
@@ -282,7 +294,12 @@ def soundfield(p, grid, xnorm=None, colorbar=True, cmap='coolwarm_clip',
282294
ax.set_xlabel(xlabel)
283295
ax.set_ylabel(ylabel)
284296
if colorbar:
285-
ax.figure.colorbar(im, ax=ax)
297+
divider = make_axes_locatable(ax)
298+
width = axes_size.AxesY(ax, aspect=1/colorbar_aspect)
299+
pad = axes_size.Fraction(colorbar_pad, width)
300+
cax = divider.append_axes("right", size=width, pad=pad)
301+
ax.figure.colorbar(im, cax=cax, label=colorbar_label)
302+
plt.sca(ax)
286303
return im
287304

288305

0 commit comments

Comments
 (0)