def test_rescale(): # [0, 10] -> [0, 1] # Results are invariant to uniformly translated # or expanded inputs a = np.arange(0, 11) npt.assert_allclose(rescale(a), a * .1) npt.assert_allclose(rescale(a), rescale(a - 42)) npt.assert_allclose(rescale(a), rescale(a + 42)) npt.assert_allclose(rescale(a), rescale(a / np.pi)) npt.assert_allclose(rescale(a), rescale(a * np.pi)) # Some more n = 6 a = np.arange(0, n) npt.assert_allclose(rescale(a), a / (n - 1)) npt.assert_allclose(rescale(a, _from=(0, 10)), a * .1) npt.assert_allclose(rescale(a, to=(0, n * (n - 1))), a * n)
def draw(self): """ Draw guide Returns ------- out : matplotlib.offsetbox.Offsetbox A drawing of this legend """ obverse = slice(0, None) reverse = slice(None, None, -1) width = self.barwidth height = self.barheight nbars = len(self.bar) length = height direction = self.direction colors = self.bar['color'].tolist() labels = self.key['label'].tolist() themeable = self.theme.figure._themeable # When there is more than one guide, we keep # record of all of them using lists if 'legend_title' not in themeable: themeable['legend_title'] = [] if 'legend_text_colorbar' not in themeable: themeable['legend_text_colorbar'] = [] # .5 puts the ticks in the middle of the bars when # raster=False. So when raster=True the ticks are # in between interpolation points and the matching is # close though not exactly right. _from = self.bar['value'].min(), self.bar['value'].max() tick_locations = rescale(self.key['value'], (.5, nbars-.5), _from) * length/nbars if direction == 'horizontal': width, height = height, width length = width if self.reverse: colors = colors[::-1] labels = labels[::-1] tick_locations = length - tick_locations[::-1] # title # title_box = TextArea(self.title, textprops=dict(color='black')) themeable['legend_title'].append(title_box) # colorbar and ticks # da = ColoredDrawingArea(width, height, 0, 0) if self.raster: add_interpolated_colorbar(da, colors, direction) else: add_segmented_colorbar(da, colors, direction) if self.ticks: _locations = tick_locations if not self.draw_ulim: _locations = _locations[:-1] if not self.draw_llim: _locations = _locations[1:] add_ticks(da, _locations, direction) # labels # if self.label: labels_da, legend_text = create_labels(da, labels, tick_locations, direction) themeable['legend_text_colorbar'].extend(legend_text) else: labels_da = ColoredDrawingArea(0, 0) # colorbar + labels # if direction == 'vertical': packer, align = HPacker, 'bottom' align = 'center' else: packer, align = VPacker, 'right' align = 'center' slc = obverse if self.label_position == 'right' else reverse if self.label_position in ('right', 'bottom'): slc = obverse else: slc = reverse main_box = packer(children=[da, labels_da][slc], sep=self._label_margin, align=align, pad=0) # title + colorbar(with labels) # lookup = { 'right': (HPacker, reverse), 'left': (HPacker, obverse), 'bottom': (VPacker, reverse), 'top': (VPacker, obverse)} packer, slc = lookup[self.title_position] children = [title_box, main_box][slc] box = packer(children=children, sep=self._title_margin, align=self._title_align, pad=0) return box
def test_rescale(): # [0, 10] -> [0, 1] # Results are invariant to uniformly translated # or expanded inputs a = np.arange(0, 11) npt.assert_allclose(rescale(a), a*.1) npt.assert_allclose(rescale(a), rescale(a-42)) npt.assert_allclose(rescale(a), rescale(a+42)) npt.assert_allclose(rescale(a), rescale(a/np.pi)) npt.assert_allclose(rescale(a), rescale(a*np.pi)) # Some more n = 6 a = np.arange(0, n) npt.assert_allclose(rescale(a), a/(n-1)) npt.assert_allclose(rescale(a, _from=(0, 10)), a*.1) npt.assert_allclose(rescale(a, to=(0, n*(n-1))), a*n)