def __init__(self, cmap, vmin, vmax=None, label=True, label_position=None, label_rotation=None, clipmin=None, clipmax=None, orientation='horizontal', unit=None, contours=(), width=None, ticks=None, threshold=None, ticklocation='auto', background='white', tight=True, h=None, w=None, *args, **kwargs): # get Colormap if isinstance(cmap, np.ndarray): if threshold is not None: raise NotImplementedError("threshold parameter with cmap=array") if cmap.max() > 1: cmap = cmap / 255. cm = mpl.colors.ListedColormap(cmap, 'LUT') else: cm = mpl.cm.get_cmap(cmap) # prepare layout if orientation == 'horizontal': if h is None and w is None: h = 1 ax_aspect = 4 elif orientation == 'vertical': if h is None and w is None: h = 4 ax_aspect = 0.3 else: raise ValueError("orientation=%s" % repr(orientation)) layout = Layout(1, ax_aspect, 2, tight, False, h, w, *args, **kwargs) EelFigure.__init__(self, cm.name, layout) ax = self._axes[0] # translate between axes and data coordinates if isinstance(vmin, Normalize): norm = vmin else: vmin, vmax = fix_vlim_for_cmap(vmin, vmax, cm.name) norm = Normalize(vmin, vmax) # value ticks if ticks is False: ticks = () tick_labels = None elif isinstance(ticks, dict): tick_dict = ticks ticks = sorted(tick_dict) tick_labels = [tick_dict[t] for t in ticks] else: tick_labels = None if orientation == 'horizontal': axis = ax.xaxis contour_func = ax.axhline else: axis = ax.yaxis contour_func = ax.axvline if label is True: if unit: label = unit else: label = cm.name elif not label: label = '' # show only part of the colorbar if clipmin is not None or clipmax is not None: if isinstance(norm, SymmetricNormalize): raise NotImplementedError( "clipmin or clipmax with SymmetricNormalize") boundaries = norm.inverse(np.linspace(0, 1, cm.N + 1)) if clipmin is None: start = None else: start = np.digitize(clipmin, boundaries, True) if clipmax is None: stop = None else: stop = np.digitize(clipmax, boundaries) + 1 boundaries = boundaries[start:stop] else: boundaries = None colorbar = ColorbarBase(ax, cm, norm, boundaries=boundaries, orientation=orientation, ticklocation=ticklocation, ticks=ticks, label=label) # fix tick location if isinstance(norm, SymmetricNormalize) and ticks is not None: tick_norm = Normalize(norm.vmin, norm.vmax, norm.clip) axis.set_ticks(tick_norm(ticks)) # unit-based tick-labels if unit and tick_labels is None: formatter, label = find_axis_params_data(unit, label) tick_labels = tuple(map(formatter, colorbar.get_ticks())) if tick_labels is not None: if clipmin is not None: tick_labels = [l for l, t in zip(tick_labels, ticks) if t >= clipmin] axis.set_ticklabels(tick_labels) # label position/rotation if label_position is not None: axis.set_label_position(label_position) if label_rotation is not None: axis.label.set_rotation(label_rotation) if orientation == 'vertical': if (label_rotation + 10) % 360 < 20: axis.label.set_va('center') elif orientation == 'vertical' and len(label) <= 3: axis.label.set_rotation(0) axis.label.set_va('center') self._contours = [contour_func(c, c='k') for c in contours] self._draw_hooks.append(self.__fix_alpha) self._draw_hooks.append(self.__update_bar_tickness) self._background = background self._colorbar = colorbar self._orientation = orientation self._width = width self._show()
def __init__(self, cmap, vmin, vmax=None, label=True, label_position=None, label_rotation=None, clipmin=None, clipmax=None, orientation='horizontal', unit=None, contours=(), width=None, ticks=None, threshold=None, ticklocation='auto', background='white', tight=True, h=None, w=None, *args, **kwargs): # get Colormap if isinstance(cmap, np.ndarray): if threshold is not None: raise NotImplementedError("threshold parameter with cmap=array") if cmap.max() > 1: cmap = cmap / 255. cm = mpl.colors.ListedColormap(cmap, 'LUT') else: cm = mpl.cm.get_cmap(cmap) # prepare layout if orientation == 'horizontal': if h is None and w is None: h = 1 ax_aspect = 4 elif orientation == 'vertical': if h is None and w is None: h = 4 ax_aspect = 0.3 else: raise ValueError("orientation=%s" % repr(orientation)) layout = Layout(1, ax_aspect, 2, tight, False, h, w, *args, **kwargs) EelFigure.__init__(self, cm.name, layout) ax = self._axes[0] # translate between axes and data coordinates if isinstance(vmin, Normalize): norm = vmin else: vmin, vmax = fix_vlim_for_cmap(vmin, vmax, cm.name) norm = Normalize(vmin, vmax) # value ticks if ticks is False: ticks = () tick_labels = None elif isinstance(ticks, dict): tick_dict = ticks ticks = sorted(tick_dict) tick_labels = [tick_dict[t] for t in ticks] else: tick_labels = None if orientation == 'horizontal': axis = ax.xaxis contour_func = ax.axhline else: axis = ax.yaxis contour_func = ax.axvline if label is True: if unit: label = unit else: label = cm.name elif not label: label = '' # show only part of the colorbar if clipmin is not None or clipmax is not None: if isinstance(norm, SymmetricNormalize): raise NotImplementedError( "clipmin or clipmax with SymmetricNormalize") boundaries = norm.inverse(np.linspace(0, 1, cm.N + 1)) if clipmin is None: start = None else: start = np.digitize(clipmin, boundaries, True) if clipmax is None: stop = None else: stop = np.digitize(clipmax, boundaries) + 1 boundaries = boundaries[start:stop] else: boundaries = None colorbar = ColorbarBase(ax, cm, norm, boundaries=boundaries, orientation=orientation, ticklocation=ticklocation, ticks=ticks, label=label) # fix tick location if isinstance(norm, SymmetricNormalize) and ticks is not None: tick_norm = Normalize(norm.vmin, norm.vmax, norm.clip) axis.set_ticks(tick_norm(ticks)) # unit-based tick-labels if unit and tick_labels is None: formatter, label = find_axis_params_data(unit, label) tick_labels = tuple(map(formatter, colorbar.get_ticks())) if tick_labels is not None: if clipmin is not None: tick_labels = [l for l, t in zip(tick_labels, ticks) if t >= clipmin] axis.set_ticklabels(tick_labels) # label position/rotation if label_position is not None: axis.set_label_position(label_position) if label_rotation is not None: axis.label.set_rotation(label_rotation) if orientation == 'vertical': if (label_rotation + 10) % 360 < 20: axis.label.set_va('center') elif orientation == 'vertical' and len(label) <= 3: axis.label.set_rotation(0) axis.label.set_va('center') self._contours = [contour_func(c, c='k') for c in contours] self._draw_hooks.append(self.__fix_alpha) self._draw_hooks.append(self.__update_bar_tickness) self._background = background self._colorbar = colorbar self._orientation = orientation self._width = width self._show()
def __init__(self, cmap, vmin=None, vmax=None, label=True, label_position=None, label_rotation=None, clipmin=None, clipmax=None, orientation='horizontal', unit=None, contours=(), width=None, ticks=None, threshold=None, ticklocation='auto', background='white', tight=True, h=None, w=None, *args, **kwargs): # get Colormap if isinstance(cmap, np.ndarray): if threshold is not None: raise NotImplementedError("threshold parameter with cmap=<array>") if cmap.max() > 1: cmap = cmap / 255. cm = mpl.colors.ListedColormap(cmap, 'LUT') elif isinstance(cmap, Colormap): cm = cmap else: cm = mpl.cm.get_cmap(cmap) # prepare layout if orientation == 'horizontal': if h is None and w is None: h = 1 ax_aspect = 4 elif orientation == 'vertical': if h is None and w is None: h = 4 ax_aspect = 0.3 else: raise ValueError("orientation=%s" % repr(orientation)) layout = Layout(1, ax_aspect, 2, tight, False, h, w, *args, **kwargs) EelFigure.__init__(self, cm.name, layout) ax = self._axes[0] # translate between axes and data coordinates if isinstance(vmin, Normalize): norm = vmin else: vmin, vmax = fix_vlim_for_cmap(vmin, vmax, cm) norm = Normalize(vmin, vmax) if isinstance(unit, AxisScale): scale = unit else: scale = AxisScale(unit or 1, label) # value ticks if ticks is False: tick_locs = () formatter = scale.formatter elif isinstance(ticks, dict): tick_locs = sorted(ticks) formatter = FixedFormatter([ticks[t] for t in tick_locs]) else: if ticks is None: tick_locs = MaxNLocator(4) else: tick_locs = ticks formatter = scale.formatter if orientation == 'horizontal': axis = ax.xaxis contour_func = ax.axhline else: axis = ax.yaxis contour_func = ax.axvline if label is True: label = scale.label or cm.name if not label: label = '' # show only part of the colorbar if clipmin is not None or clipmax is not None: boundaries = norm.inverse(np.linspace(0, 1, cm.N + 1)) if clipmin is None: start = None else: start = np.digitize(clipmin, boundaries, True) # boundaries[start] = clipmin if clipmax is None: stop = None else: stop = np.digitize(clipmax, boundaries) + 1 # boundaries[stop-1] = clipmax boundaries = boundaries[start:stop] else: boundaries = None colorbar = ColorbarBase(ax, cm, norm, boundaries=boundaries, orientation=orientation, ticklocation=ticklocation, ticks=tick_locs, label=label, format=formatter) # label position/rotation if label_position is not None: axis.set_label_position(label_position) if label_rotation is not None: axis.label.set_rotation(label_rotation) if orientation == 'vertical': if (label_rotation + 10) % 360 < 20: axis.label.set_va('center') elif orientation == 'vertical' and len(label) <= 3: axis.label.set_rotation(0) axis.label.set_va('center') self._contours = [contour_func(c, c='k') for c in contours] self._draw_hooks.append(self.__fix_alpha) self._draw_hooks.append(self.__update_bar_tickness) self._background = background self._colorbar = colorbar self._orientation = orientation self._width = width self._show()