def color_list(color, n): r""" Normalize a list of ``n`` colors. INPUT: - ``color`` -- anything specifying a :class:`Color`, a list of such specifications, or the string "rainbow"; - ``n`` - an integer. OUTPUT: - a list of ``n`` colors. If ``color`` specified a single color, it is repeated ``n`` times. If it was a list of ``n`` colors, it is returned without changes. If it was "rainbow", the rainbow of ``n`` colors is returned. EXAMPLES:: sage: from sage.geometry.toric_plotter import color_list sage: color_list("grey", 1) [RGB color (0.5019607843137255, 0.5019607843137255, 0.5019607843137255)] sage: len(color_list("grey", 3)) 3 sage: L = color_list("rainbow", 3) sage: L [RGB color (1.0, 0.0, 0.0), RGB color (0.0, 1.0, 0.0), RGB color (0.0, 0.0, 1.0)] sage: color_list(L, 3) [RGB color (1.0, 0.0, 0.0), RGB color (0.0, 1.0, 0.0), RGB color (0.0, 0.0, 1.0)] sage: color_list(L, 4) Traceback (most recent call last): ... ValueError: expected 4 colors, got 3! """ try: color = Color(color) return [color] * n except (ValueError, TypeError): if isinstance(color, (list, tuple)): if len(color) != n: raise ValueError("expected %d colors, got %d!" % (n, len(color))) return color if color == "rainbow": return [Color(c) for c in rainbow(n, "rgbtuple")] raise TypeError("cannot interpret %s as a color!" % color)
def plot(self, cell_colors=None, **kwds): """ Return a graphical representation for 2-dimensional Voronoi diagrams. INPUT: - ``cell_colors`` -- (default: ``None``) provide the colors for the cells, either as dictionary. Randomly colored cells are provided with ``None``. - ``**kwds`` -- optional keyword parameters, passed on as arguments for plot(). OUTPUT: A graphics object. EXAMPLES:: sage: P = [[0.671, 0.650], [0.258, 0.767], [0.562, 0.406], [0.254, 0.709], [0.493, 0.879]] sage: V = VoronoiDiagram(P); S=V.plot() sage: show(S, xmin=0, xmax=1, ymin=0, ymax=1, aspect_ratio=1, axes=false) sage: S=V.plot(cell_colors={0:'red', 1:'blue', 2:'green', 3:'white', 4:'yellow'}) sage: show(S, xmin=0, xmax=1, ymin=0, ymax=1, aspect_ratio=1, axes=false) sage: S=V.plot(cell_colors=['red','blue','red','white', 'white']) sage: show(S, xmin=0, xmax=1, ymin=0, ymax=1, aspect_ratio=1, axes=false) sage: S=V.plot(cell_colors='something else') Traceback (most recent call last): ... AssertionError: 'cell_colors' must be a list or a dictionary Trying to plot a Voronoi diagram of dimension other than 2 gives an error:: sage: VoronoiDiagram([[1, 2, 3], [6, 5, 4]]).plot() Traceback (most recent call last): ... NotImplementedError: Plotting of 3-dimensional Voronoi diagrams not implemented """ if self.ambient_dim() == 2: S = line([]) if cell_colors is None: from random import shuffle cell_colors = rainbow(self._n) shuffle(cell_colors) else: if not (isinstance(cell_colors, list) or (isinstance(cell_colors, dict))): raise AssertionError("'cell_colors' must be a list or a dictionary") for i, p in enumerate(self._P): col = cell_colors[i] S += (self.regions()[p]).render_solid(color=col, zorder=1) S += point(p, color=col, pointsize=10, zorder=3) S += point(p, color='black', pointsize=20, zorder=2) return plot(S, **kwds) raise NotImplementedError('Plotting of ' + str(self.ambient_dim()) + '-dimensional Voronoi diagrams not' + ' implemented')