|
1 | 1 | """Plot sound fields etc.""" |
2 | | - |
| 2 | +from __future__ import division |
3 | 3 | import matplotlib.pyplot as plt |
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 | 8 | from mpl_toolkits.mplot3d import Axes3D |
8 | 9 | import numpy as np |
9 | 10 | from . import util |
@@ -165,8 +166,9 @@ def loudspeaker_3d(x0, n0, a0=None, w=0.08, h=0.08): |
165 | 166 | fig.show() |
166 | 167 |
|
167 | 168 |
|
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, |
170 | 172 | **kwargs): |
171 | 173 | """Two-dimensional plot of sound field. |
172 | 174 |
|
@@ -217,7 +219,17 @@ def soundfield(p, grid, xnorm=None, colorbar=True, cmap='coolwarm_clip', |
217 | 219 | :func:`matplotlib.pyplot.ylabel`. |
218 | 220 | colorbar : bool, optional |
219 | 221 | 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 |
221 | 233 | If given, the plot is created on `ax` instead of the current |
222 | 234 | axis (see :func:`matplotlib.pyplot.gca`). |
223 | 235 | cmap, vmin, vmax, **kwargs |
@@ -282,7 +294,12 @@ def soundfield(p, grid, xnorm=None, colorbar=True, cmap='coolwarm_clip', |
282 | 294 | ax.set_xlabel(xlabel) |
283 | 295 | ax.set_ylabel(ylabel) |
284 | 296 | 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) |
286 | 303 | return im |
287 | 304 |
|
288 | 305 |
|
|
0 commit comments