|
4 | 4 | from matplotlib.patches import PathPatch |
5 | 5 | from matplotlib.path import Path |
6 | 6 | from matplotlib.collections import PatchCollection |
7 | | -from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size |
| 7 | +from mpl_toolkits import axes_grid1 |
8 | 8 | from mpl_toolkits.mplot3d import Axes3D |
9 | 9 | import numpy as np |
10 | 10 | from . import util |
@@ -166,10 +166,9 @@ def loudspeaker_3d(x0, n0, a0=None, w=0.08, h=0.08): |
166 | 166 | fig.show() |
167 | 167 |
|
168 | 168 |
|
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): |
173 | 172 | """Two-dimensional plot of sound field. |
174 | 173 |
|
175 | 174 | Parameters |
@@ -219,16 +218,8 @@ def soundfield(p, grid, xnorm=None, cmap='coolwarm_clip', colorbar=True, |
219 | 218 | :func:`matplotlib.pyplot.ylabel`. |
220 | 219 | colorbar : bool, optional |
221 | 220 | 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`. |
232 | 223 | ax : Axes, optional |
233 | 224 | If given, the plot is created on `ax` instead of the current |
234 | 225 | axis (see :func:`matplotlib.pyplot.gca`). |
@@ -294,12 +285,7 @@ def soundfield(p, grid, xnorm=None, cmap='coolwarm_clip', colorbar=True, |
294 | 285 | ax.set_xlabel(xlabel) |
295 | 286 | ax.set_ylabel(ylabel) |
296 | 287 | 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) |
303 | 289 | return im |
304 | 290 |
|
305 | 291 |
|
@@ -344,3 +330,43 @@ def particles(x, trim=None, ax=None, xlabel='x (m)', ylabel='y (m)', |
344 | 330 | ax.set_xlabel(xlabel) |
345 | 331 | if ylabel: |
346 | 332 | 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