Skip to content

Commit 9240b7f

Browse files
committed
Create separate function add_colorbar()
1 parent 91f4387 commit 9240b7f

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

examples/soundfigures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +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_label='dB',
48-
vmin=-50, vmax=0)
47+
im = sfs.plot.level(p, grid, xnorm=[0, -2.2, 0], vmin=-50, vmax=0,
48+
colorbar_kwargs=dict(label='dB'))
4949
plt.title('Level of Synthesized Sound Field')
5050
plt.savefig('soundfigure_level.png')

sfs/plot.py

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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
7+
from mpl_toolkits import axes_grid1
88
from mpl_toolkits.mplot3d import Axes3D
99
import numpy as np
1010
from . import util
@@ -166,10 +166,9 @@ def loudspeaker_3d(x0, n0, a0=None, w=0.08, h=0.08):
166166
fig.show()
167167

168168

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,
172-
**kwargs):
169+
def soundfield(p, grid, xnorm=None, cmap='coolwarm_clip', vmin=-2.0, vmax=2.0,
170+
xlabel=None, ylabel=None, colorbar=True, colorbar_kwargs={},
171+
ax=None, **kwargs):
173172
"""Two-dimensional plot of sound field.
174173
175174
Parameters
@@ -219,16 +218,8 @@ def soundfield(p, grid, xnorm=None, cmap='coolwarm_clip', colorbar=True,
219218
:func:`matplotlib.pyplot.ylabel`.
220219
colorbar : bool, optional
221220
If ``False``, no colorbar is created.
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.
221+
colorbar_kwargs : dict, optional
222+
Further colorbar arguments, see :func:`add_colorbar`.
232223
ax : Axes, optional
233224
If given, the plot is created on `ax` instead of the current
234225
axis (see :func:`matplotlib.pyplot.gca`).
@@ -294,12 +285,7 @@ def soundfield(p, grid, xnorm=None, cmap='coolwarm_clip', colorbar=True,
294285
ax.set_xlabel(xlabel)
295286
ax.set_ylabel(ylabel)
296287
if colorbar:
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)
288+
add_colorbar(im, **colorbar_kwargs)
303289
return im
304290

305291

@@ -344,3 +330,43 @@ def particles(x, trim=None, ax=None, xlabel='x (m)', ylabel='y (m)',
344330
ax.set_xlabel(xlabel)
345331
if ylabel:
346332
ax.set_ylabel(ylabel)
333+
334+
335+
def add_colorbar(im, aspect=20, pad=0.5, **kwargs):
336+
"""Add a vertical color bar to a plot.
337+
338+
Parameters
339+
----------
340+
im : ScalarMappable
341+
The output of :func:`sfs.plot.soundfield`,
342+
:func:`sfs.plot.level` or any other
343+
:class:`matplotlib.cm.ScalarMappable`.
344+
aspect : float, optional
345+
Aspect ratio of the colorbar. Strictly speaking, since the
346+
colorbar is vertical, it's actually the inverse of the aspect
347+
ratio.
348+
pad : float, optional
349+
Space between image plot and colorbar, as a fraction of the
350+
width of the colorbar.
351+
352+
.. note:: The `pad` argument of
353+
:meth:`matplotlib.figure.Figure.colorbar` has a
354+
slightly different meaning ("fraction of original
355+
axes")!
356+
\**kwargs
357+
All further arguments are forwarded to
358+
:meth:`matplotlib.figure.Figure.colorbar`.
359+
360+
See Also
361+
--------
362+
matplotlib.pyplot.colorbar
363+
364+
"""
365+
ax = im.axes
366+
divider = axes_grid1.make_axes_locatable(ax)
367+
width = axes_grid1.axes_size.AxesY(ax, aspect=1/aspect)
368+
pad = axes_grid1.axes_size.Fraction(pad, width)
369+
current_ax = plt.gca()
370+
cax = divider.append_axes("right", size=width, pad=pad)
371+
plt.sca(current_ax)
372+
return ax.figure.colorbar(im, cax=cax, orientation='vertical', **kwargs)

0 commit comments

Comments
 (0)