def setCbarLevels(cbar, cMin=None, cMax=None, nLevs=5): """Set colorbar levels given a number of levels and min/max values.""" if cMin is None: cMin = cbar.get_clim()[0] if cMax is None: cMax = cbar.get_clim()[1] if cMin == cMax: cMin *= 0.999 cMax *= 1.001 norm = None if hasattr(cbar, 'mappable'): norm = cbar.mappable.norm elif hasattr(cbar, 'norm'): norm = cbar.norm if isinstance(norm, mpl.colors.LogNorm): cbarLevels = np.logspace(np.log10(cMin), np.log10(cMax), nLevs) else: cbarLevels = np.linspace(cMin, cMax, nLevs) # FIXME: [10.1, 10.2, 10.3] mapped to [10 10 10] cbarLevelsString = [] if np.all(np.array(cbarLevels) < 1e-2): pg.debug("All values smaller than 1e-4, avoiding additional rounding.") roundValue = False else: roundValue = True for i in cbarLevels: cbarLevelsString.append(prettyFloat(i, roundValue)) # print(i, prettyFloat(i)) if hasattr(cbar, 'mappable'): cbar.mappable.set_clim(vmin=cMin, vmax=cMax) cbar.set_clim(cMin, cMax) cbar.set_ticks(cbarLevels) cbar.set_ticklabels(cbarLevelsString) cbar.draw_all() # necessary since mpl 3.0 cbar.ax.minorticks_off()
def setCbarLevels(cbar, cMin=None, cMax=None, nLevs=5): """TODO Documentme.""" if cMin is None: cMin = cbar.get_clim()[0] if cMax is None: cMax = cbar.get_clim()[1] if cMin == cMax: cMin *= 0.999 cMax *= 1.001 norm = None if hasattr(cbar, 'mappable'): norm = cbar.mappable.norm elif hasattr(cbar, 'norm'): norm = cbar.norm if isinstance(norm, mpl.colors.LogNorm): cbarLevels = np.logspace(np.log10(cMin), np.log10(cMax), nLevs) else: cbarLevels = np.linspace(cMin, cMax, nLevs) # print(cbarLevels) # FIXME: [10.1, 10.2, 10.3] mapped to [10 10 10] cbarLevelsString = [] for i in cbarLevels: cbarLevelsString.append(prettyFloat(i)) # print(i, prettyFloat(i)) if hasattr(cbar, 'mappable'): cbar.mappable.set_clim(vmin=cMin, vmax=cMax) cbar.set_clim(cMin, cMax) cbar.set_ticks(cbarLevels) cbar.set_ticklabels(cbarLevelsString) cbar.draw_all() # necessary since mpl 3.0 cbar.ax.minorticks_off()