def test_extended_breaks(): x = np.arange(100) limits = min(x), max(x) for n in (5, 7, 10, 13, 31): breaks = extended_breaks(n=n) assert len(breaks(limits)) <= n + 1 # Reverse limits breaks = extended_breaks(n=7) npt.assert_array_equal(breaks((0, 6)), breaks((6, 0))) # Infinite limits limits = float('-inf'), float('inf') breaks = extended_breaks(n=5) assert len(breaks(limits)) == 0 # Zero range discrete limits = [1, 1] assert len(breaks(limits)) == 1 assert breaks(limits)[0] == limits[1] # Zero range continuous limits = [np.pi, np.pi] assert len(breaks(limits)) == 1 assert breaks(limits)[0] == limits[1]
def test_extended_breaks(): x = np.arange(100) limits = min(x), max(x) for n in (5, 7, 10, 13, 31): breaks = extended_breaks(n=n) assert len(breaks(limits)) <= n+1 # Reverse limits breaks = extended_breaks(n=7) npt.assert_array_equal(breaks((0, 6)), breaks((6, 0))) # Infinite limits limits = float('-inf'), float('inf') breaks = extended_breaks(n=5) assert len(breaks(limits)) == 0 # Zero range discrete limits = [1, 1] assert len(breaks(limits)) == 1 assert breaks(limits)[0] == limits[1] # Zero range continuous limits = [np.pi, np.pi] assert len(breaks(limits)) == 1 assert breaks(limits)[0] == limits[1]
def contour_lines(X, Y, Z, levels): # Preparation of values and the creating of contours is # adapted from MPL with some adjustments. X = np.asarray(X, dtype=np.float64) Y = np.asarray(Y, dtype=np.float64) Z = np.asarray(Z, dtype=np.float64) zmin, zmax = Z.min(), Z.max() mask = None corner_mask = False nchunk = 0 contour_generator = _contour.QuadContourGenerator(X, Y, Z, mask, corner_mask, nchunk) if isinstance(levels, int): levels = extended_breaks(n=levels)((zmin, zmax)) # The counter_generator gives us a list of vertices that # represent all the contour lines at that level. There # may be 0, 1 or more vertices at a level. Each one of # these we call a piece, and it represented as an nx2 array. # # We want x-y values that describe *all* the contour lines # in tidy format. Therefore each x-y vertex has a # corresponding level and piece id. segments = [] piece_ids = [] level_values = [] start_pid = 1 for level in levels: vertices = contour_generator.create_contour(level) for pid, piece in enumerate(vertices, start=start_pid): n = len(piece) segments.append(piece) piece_ids.append(np.repeat(pid, n)) level_values.append(np.repeat(level, n)) start_pid = pid + 1 # Collapse the info and make it fit for dataframe columns if segments: x, y = np.vstack(segments).T piece = np.hstack(piece_ids) level = np.hstack(level_values) else: x, y = [], [] piece = [] level = [] data = pd.DataFrame({ 'x': x, 'y': y, 'level': level, 'piece': piece, }) return data
def contour_lines(X, Y, Z, levels): # Preparation of values and the creating of contours is # adapted from MPL with some adjustments. X = np.asarray(X, dtype=np.float64) Y = np.asarray(Y, dtype=np.float64) Z = np.asarray(Z, dtype=np.float64) zmin, zmax = Z.min(), Z.max() mask = None corner_mask = False nchunk = 0 contour_generator = _contour.QuadContourGenerator( X, Y, Z, mask, corner_mask, nchunk) if isinstance(levels, int): levels = extended_breaks(n=levels)((zmin, zmax)) # The counter_generator gives us a list of vertices that # represent all the contour lines at that level. There # may be 0, 1 or more vertices at a level. Each one of # these we call a piece, and it represented as an nx2 array. # # We want x-y values that describe *all* the contour lines # in tidy format. Therefore each x-y vertex has a # corresponding level and piece id. segments = [] piece_ids = [] level_values = [] start_pid = 1 for level in levels: vertices = contour_generator.create_contour(level) for pid, piece in enumerate(vertices, start=start_pid): n = len(piece) segments.append(piece) piece_ids.append(np.repeat(pid, n)) level_values.append(np.repeat(level, n)) start_pid = pid + 1 # Collapse the info and make it fit for dataframe columns x, y = np.vstack(segments).T piece = np.hstack(piece_ids) level = np.hstack(level_values) data = pd.DataFrame({ 'x': x, 'y': y, 'level': level, 'piece': piece, }) return data
def train(self, scale): # Do nothing if scales are inappropriate if set(scale.aesthetics) & {'color', 'colour', 'fill'} == 0: warn("colorbar guide needs color or fill scales.") return None if not issubclass(scale.__class__, scale_continuous): warn("colorbar guide needs continuous scales") return None # value = breaks (numeric) is used for determining the # position of ticks limits = scale.limits breaks = scale.get_breaks(strict=True) breaks = np.asarray(breaks) breaks = breaks[~np.isnan(breaks)] if not len(breaks): return None self.key = pd.DataFrame({ scale.aesthetics[0]: scale.map(breaks), 'label': scale.get_labels(breaks), 'value': breaks }) bar = extended_breaks(n=self.nbin, Q=[1, 2, 5, 10])(limits) # discard locations in bar not in scale.limits bar = bar.compress((limits[0] <= bar) & (bar <= limits[1])) self.bar = pd.DataFrame({'color': scale.map(bar), 'value': bar}) labels = ' '.join(six.text_type(x) for x in self.key['label']) info = '\n'.join([ self.title, labels, ' '.join(self.bar['color'].tolist()), self.__class__.__name__ ]) self.hash = hashlib.md5(info.encode('utf-8')).hexdigest() return self