def test_sort_xy(): x = np.linspace(10, 0, 10) y = np.linspace(1, 10, 10) xx, yy = util.sort_xy(x, y) assert xx == tuple(reversed(x)) assert yy == tuple(reversed(y))
def barplot_magnitudes(magnitudes, orientation='h', sort=False, buffer=1, zorder=3, offset=0, width=0.8, fig=None, ax=None): """Create a barplot of magnitudes of coefficient pairs and their names. e.g., astigmatism will get one bar. Parameters ---------- magnitudes : dict keys of names, values of magnitudes. E.g., {'Primary Coma': 1234567} orientation : str, {'h', 'v', 'horizontal', 'vertical'} orientation of the plot sort : bool, optional whether to sort the zernikes in descending order buffer : float, optional buffer to use around the left and right (or top and bottom) bars zorder : int, optional zorder of the bars. Use zorder > 3 to put bars in front of gridlines offset : float, optional offset to apply to bars, useful for before/after Zernike breakdowns width : float, optional width of bars, useful for before/after Zernike breakdowns fig : matplotlib.figure.Figure Figure containing the plot ax : matplotlib.axes.Axis Axis containing the plot Returns ------- fig : matplotlib.figure.Figure Figure containing the plot ax : matplotlib.axes.Axis Axis containing the plot """ from matplotlib import pyplot as plt mags = magnitudes.values() names = magnitudes.keys() idxs = np.arange(len(names)) # idxs = np.asarray(list(range(len(names)))) if sort: mags, names = sort_xy(mags, names) mags = list(reversed(mags)) names = list(reversed(names)) lims = (idxs[0] - buffer, idxs[-1] + buffer) fig, ax = share_fig_ax(fig, ax) if orientation.lower() in ('h', 'horizontal'): ax.bar(idxs + offset, mags, zorder=zorder, width=width) plt.xticks(idxs, names, rotation=90) ax.set(xlim=lims) else: ax.barh(idxs + offset, mags, zorder=zorder, height=width) plt.yticks(idxs, names) ax.set(ylim=lims) return fig, ax