Beispiel #1
0
def draw_xlabels_xy(labels, axes):
    """Creates a label collection and adds it to the axis.

    Parameters
    ----------
    labels : list of dict
        List of dictionaries containing the label properties.
    axes : object
        Matplotlib axes.

    """
    for label in labels:
        x, y = label['pos']
        text = label['text']
        fontsize = label['fontsize']
        color = label.get('color') or '#ffffff'
        textcolor = label.get('textcolor') or '#000000'
        bbox = dict(color=color_to_rgb(color, normalize=True),
                    edgecolor=color_to_rgb(color, normalize=True),
                    alpha=1.0,
                    pad=0.0)
        t = axes.text(x,
                      y,
                      text,
                      fontsize=fontsize,
                      zorder=ZORDER_LABELS,
                      ha='center',
                      va='center',
                      color=color_to_rgb(textcolor, normalize=True))
        t.set_bbox(bbox)
Beispiel #2
0
def draw_xarrows_xy(lines, axes):
    """Creates an XY arrow collection and adds it to the axis.

    Parameters
    ----------
    lines : list of dict
        List of dictionaries containing the arrow line properties.
        The following properties of an arrow can be specified in the dict.

        * start (list): XY(Z) coordinates of the starting point.
        * end (list): XY(Z) coordinates of the end point.
        * text (str, optional): The text of the label. Default is ``None``.
        * textcolor (rgb tuple or hex string, optional): Color of the label text. Default is black.
        * fontsize (int, optional): The size of the font of the label text. Default is ```6``.
        * color (rgb tuple or hex string, optional): Color of the arrow. Default is black.
        * width (float): Width of the arrow. Default is ``1.0``.

    axes : object
        Matplotlib axes.

    """
    arrowprops = {
        'arrowstyle': '-|>,head_length=0.4,head_width=0.2',
        'connectionstyle': 'arc3,rad=0.0',
        'linewidth': 1.0,
        'color': '#000000',
        'shrinkB': 0.05,
    }
    for line in lines:
        sp = line['start']
        ep = line['end']
        text = line.get('text', None)
        textcolor = line.get('textcolor') or '#000000'
        fontsize = line.get('fontsize') or 6
        arrowprops['color'] = color_to_rgb(line.get('color', '#000000'),
                                           normalize=True)
        arrowprops['linewidth'] = line.get('width', 1.0)
        axes.annotate(
            '',
            xy=ep[0:2],
            xytext=sp[0:2],
            arrowprops=arrowprops,
            zorder=ZORDER_LINES,
        )
        if text:
            x, y, z = midpoint_line_xy((sp, ep))
            t = axes.text(x,
                          y,
                          text,
                          fontsize=fontsize,
                          zorder=ZORDER_LABELS,
                          ha='center',
                          va='center',
                          color=color_to_rgb(textcolor, normalize=True))
            t.set_bbox({
                'color': '#ffffff',
                'alpha': 1.0,
                'edgecolor': '#ffffff'
            })
Beispiel #3
0
def draw_xpolylines_xy(polylines, axes):
    patches = []
    paths = []

    widths = []
    colors = []

    for polyline in polylines:
        points = polyline['points']
        codes = [Path.MOVETO] + [Path.LINETO] * (len(points) - 1)

        width = polyline.get('width', 1.0)
        color = polyline.get('color', '#000000')
        text = polyline.get('text', None)
        textcolor = polyline.get('textcolor') or '#000000'
        fontsize = polyline.get('fontsize') or 6

        path = [point[0:2] for point in points]
        paths.append(path)

        widths.append(width)
        colors.append(color_to_rgb(color, normalize=True))

        if text:
            p = len(points)

            if p % 2 == 0:
                a = points[p // 2]
                b = points[p // 2 + 1]
                x, y, z = midpoint_line_xy((a, b))
            else:
                x, y = points[p // 2 + 1]

            t = axes.text(x,
                          y,
                          text,
                          fontsize=fontsize,
                          zorder=ZORDER_LABELS,
                          ha='center',
                          va='center',
                          color=color_to_rgb(textcolor, normalize=True))

            t.set_bbox({
                'color': '#ffffff',
                'alpha': 1.0,
                'edgecolor': '#ffffff'
            })

    coll = PolyCollection(paths,
                          closed=False,
                          linewidths=widths,
                          edgecolors=colors,
                          facecolors='none',
                          zorder=ZORDER_LINES)

    axes.add_collection(coll)

    return coll
Beispiel #4
0
def draw_xpolygons_xy(polygons, axes):
    """Creates a polygon collection and adds it to the axis.

    Parameters
    ----------
    polygons : list of dict
        List of dictionaries containing the polygon properties.
        The following properties can be specified in the dict.

        * points (list): XY(Z) coordinates of the polygon vertices.
        * text (str, optional): The text of the label. Default is ``None``.
        * textcolor (rgb tuple or hex string, optional): Color of the label text. Default is black.
        * fontsize (int, optional): The size of the font of the label text. Default is ```12``.
        * facecolor (rgb tuple or hex string, optional): Color of the polygon face. Default is white.
        * edgecolor (rgb tuple or hex string, optional): Color of the edge of the polygon. Default is black.
        * edgewidth (float): Width of the polygon edge. Default is ``1.0``.

    axes : object
        Matplotlib axes.

    Returns
    -------
    object
        The matplotlib polygon collection object.

    """
    facecolors = []
    edgecolors = []
    linewidths = []
    patches = []
    for attr in polygons:
        points = attr['points']
        text = attr.get('text')
        textcolor = color_to_rgb(attr.get('textcolor', '#000000'),
                                 normalize=True)
        facecolors.append(
            color_to_rgb(attr.get('facecolor', '#ffffff'), normalize=True))
        edgecolors.append(
            color_to_rgb(attr.get('edgecolor', '#000000'), normalize=True))
        linewidths.append(attr.get('edgewidth', 1.0))
        patches.append(Polygon([point[0:2] for point in points]))
        if text:
            c = centroid_points_xy(points)
            axes.text(c[0],
                      c[1],
                      text,
                      fontsize=attr.get('fontsize', 10.0),
                      zorder=ZORDER_LABELS,
                      ha='center',
                      va='center',
                      color=textcolor)
    coll = PatchCollection(patches,
                           facecolor=facecolors,
                           edgecolor=edgecolors,
                           lw=linewidths,
                           zorder=ZORDER_POLYGONS)
    axes.add_collection(coll)
    return coll
Beispiel #5
0
 def color(self, color):
     if color:
         color[:] = [(FromArgb(*color_to_rgb(bgc)),
                      FromArgb(*color_to_rgb(tc))) for bgc, tc in color]
         l = len(self.labels)  # noqa: E741
         c = len(color)
         if c < l:
             color += [(self._default_color, self._default_textcolor)
                       for i in range(l - c)]
         elif c > l:
             color[:] = color[:l]
         self._color = color
Beispiel #6
0
def draw_xlines_xy(lines, axes, alpha=1.0, linestyle='solid'):
    """Creates an XY line collection and adds it to the axis.

    Parameters:
        lines (list): List of dictionaries containing the line properties.
        axes (object): Matplotlib axes.
        alpha (float, list): 0.0 for transparent through 1.0  for opaque.
        linestyle (str, list): Matplotlib line style strings.


    Returns:
        object: The matplotlib line collection object.
    """
    fromto = []
    widths = []
    colors = []
    for line in lines:
        sp = line['start']
        ep = line['end']
        width = line.get('width', 1.0)
        color = line.get('color', '#000000')
        text = line.get('text', None)
        textcolor = line.get('textcolor') or '#000000'
        fontsize = line.get('fontsize') or 24
        fromto.append((sp, ep))
        widths.append(width)
        colors.append(color_to_rgb(color, normalize=True))
        if text:
            x, y, z = midpoint_line_xy((sp, ep))
            t = axes.text(x,
                          y,
                          text,
                          fontsize=fontsize,
                          zorder=ZORDER_LABELS,
                          ha='center',
                          va='center',
                          color=color_to_rgb(textcolor, normalize=True))
            t.set_bbox({
                'color': '#ffffff',
                'alpha': 1.0,
                'edgecolor': '#ffffff'
            })
    coll = LineCollection(fromto,
                          linewidths=widths,
                          colors=colors,
                          linestyle=linestyle,
                          alpha=alpha,
                          zorder=ZORDER_LINES)
    axes.add_collection(coll)
    return coll
Beispiel #7
0
def draw_xarrows_xy(lines, axes):
    """Creates an XY arrow collection and adds it to the axis.

    Parameters:
        lines (list): List of dictionaries containing the arrow line properties.
        axes (object): Matplotlib axes.

    """
    arrowprops = {
        'arrowstyle': '-|>,head_length=0.4,head_width=0.2',
        'connectionstyle': 'arc3,rad=0.0',
        'linewidth': 1.0,
        'color': '#000000',
        'shrinkB': 0.05,
    }
    for line in lines:
        sp = line['start']
        ep = line['end']
        text = line.get('text', None)
        textcolor = line.get('textcolor') or '#000000'
        fontsize = line.get('fontsize') or 6
        arrowprops['color'] = color_to_rgb(line.get('color', '#000000'),
                                           normalize=True)
        arrowprops['linewidth'] = line.get('width', 1.0)
        axes.annotate(
            '',
            xy=ep,
            xytext=sp,
            arrowprops=arrowprops,
            zorder=ZORDER_LINES,
        )
        if text:
            x, y, z = midpoint_line_xy((sp, ep))
            t = axes.text(x,
                          y,
                          text,
                          fontsize=fontsize,
                          zorder=ZORDER_LABELS,
                          ha='center',
                          va='center',
                          color=color_to_rgb(textcolor, normalize=True))
            t.set_bbox({
                'color': '#ffffff',
                'alpha': 1.0,
                'edgecolor': '#ffffff'
            })
Beispiel #8
0
 def color(self, color):
     color = color or self.default_color
     if not is_sequence_of_iterable(color):
         color = [color]
     color = [
         FromArgb(*color_to_rgb(c))
         for c in iterable_like(self.points, color, self.default_color)
     ]
     self._color = color
Beispiel #9
0
def draw_xpoints_xy(points, axes):
    """Creates an XY point collection and adds it to the axis.

    Parameters:
        points (list): List of dictionaries containing the point properties.
        axes (object): Matplotlib axes.

    Returns:
        object: The matplotlib point collection object.
    """
    circles = []
    facecolors = []
    edgecolors = []
    linewidths = []
    for point in points:
        pos = point['pos']
        radius = point['radius']
        text = point.get('text')
        fcolor = point.get('facecolor') or '#ffffff'
        ecolor = point.get('edgecolor') or '#000000'
        lwidth = point.get('edgewidth') or 1.0
        textcolor = point.get('textcolor') or '#000000'
        fontsize = point.get('fontsize') or 24
        circles.append(Circle(pos, radius=radius))
        facecolors.append(color_to_rgb(fcolor, normalize=True))
        edgecolors.append(color_to_rgb(ecolor, normalize=True))
        linewidths.append(lwidth)
        if text is not None:
            axes.text(pos[0] - 0.01,
                      pos[1] - 0.01,
                      text,
                      fontsize=fontsize,
                      zorder=ZORDER_LABELS,
                      ha='center',
                      va='center',
                      color=textcolor)
    coll = PatchCollection(circles,
                           linewidths=linewidths,
                           facecolors=facecolors,
                           edgecolors=edgecolors,
                           alpha=1.0,
                           zorder=ZORDER_POINTS)
    axes.add_collection(coll)
    return coll
Beispiel #10
0
 def color(self, color):
     if color:
         color[:] = [FromArgb(*color_to_rgb(c)) for c in color]
         e = self.mesh.number_of_edges()
         c = len(color)
         if c < e:
             color += [self._default_color for i in range(e - c)]
         elif c > e:
             color[:] = color[:e]
         self._color = color
Beispiel #11
0
def draw_xpolygons_xy(polygons, axes):
    """Creates a polygon collection and adds it to the axis.

    Parameters:
        polygons (list): List of dictionaries containing the polygon properties.
        axes (object): Matplotlib axes.

    Returns:
        object: The matplotlib polygon collection object.
    """
    facecolors = []
    edgecolors = []
    linewidths = []
    patches = []
    for attr in polygons:
        points = attr['points']
        text = attr.get('text')
        textcolor = color_to_rgb(attr.get('textcolor', '#000000'),
                                 normalize=True)
        facecolors.append(
            color_to_rgb(attr.get('facecolor', '#ffffff'), normalize=True))
        edgecolors.append(
            color_to_rgb(attr.get('edgecolor', '#000000'), normalize=True))
        linewidths.append(attr.get('edgewidth', 1.0))
        patches.append(Polygon(points))
        if text:
            c = centroid_points_xy(points)
            axes.text(c[0],
                      c[1],
                      text,
                      fontsize=attr.get('fontsize', 10.0),
                      zorder=ZORDER_LABELS,
                      ha='center',
                      va='center',
                      color=textcolor)
    coll = PatchCollection(patches,
                           facecolor=facecolors,
                           edgecolor=edgecolors,
                           lw=linewidths,
                           zorder=ZORDER_POLYGONS)
    axes.add_collection(coll)
    return coll
Beispiel #12
0
 def update_faces(self, facecolor=None):
     """Updates the plotter face collection based on the mesh."""
     facecolor = valuedict(self.mesh.faces(), facecolor, self.defaults['face.facecolor'])
     polygons = []
     facecolors = []
     for fkey in self.mesh.faces():
         points = self.mesh.face_coordinates(fkey, 'xy')
         polygons.append(Polygon(points))
         facecolors.append(color_to_rgb(facecolor[fkey], normalize=True))
     self.facecollection.set_paths(polygons)
     self.facecollection.set_facecolor(facecolors)
Beispiel #13
0
 def color(self, color):
     if color:
         p = len(self.points)
         if isinstance(color, (basestring, tuple)):
             color = [color for _ in range(p)]
         color = [FromArgb(*color_to_rgb(c)) for c in color]
         c = len(color)
         if c < p:
             color += [self._default_color for _ in range(p - c)]
         elif c > p:
             color[:] = color[:p]
         self._color = color
Beispiel #14
0
 def color(self, color):
     if color:
         l = len(self.lines)  # noqa: E741
         if isinstance(color, (basestring, tuple)):
             color = [color for _ in range(l)]
         color[:] = [FromArgb(*color_to_rgb(c)) for c in color]
         c = len(color)
         if c < l:
             color += [self._default_color for i in range(l - c)]
         elif c > l:
             color[:] = color[:l]
         self._color = color
Beispiel #15
0
 def display(self):
     points = []
     vcolor = self.network.attributes['color.vertex']
     vcolor = vcolor or self.default_vertexcolor
     vcolor = color_to_rgb(vcolor, True)
     for key, attr in self.network.vertices(True):
         points.append({
             'pos': (attr['x'], attr['y'], attr['z']),
             'size': 6.0,
             'color': vcolor,
         })
     lines = []
     ecolor = self.network.attributes['color.vertex']
     ecolor = ecolor or self.default_edgecolor
     ecolor = color_to_rgb(ecolor, True)
     for u, v in self.network.edges():
         lines.append({
             'start': self.network.vertex_coordinates(u),
             'end': self.network.vertex_coordinates(v),
             'color': ecolor,
             'width': 1.0
         })
     # loads = []
     # for key, attr in self.network.vertices(True):
     #     if attr['is_fixed']:
     #         continue
     #     if 'p' in attr:
     #         p = attr['p']
     #         l = sqrt(p[0] ** 2 + p[1] ** 2 + p[2] ** 2)
     #         if l:
     #             start = self.network.vertex_coordinates(key)
     #             end   = [start[i] + p[i] for i in range(3)]
     #             loads.append({
     #                 'start': start,
     #                 'end'  : end,
     #                 'color': (0, 1.0, 0),
     #                 'width': 3.0
     #             })
     xdraw_points(points)
     xdraw_lines(lines)
Beispiel #16
0
 def color(self, color):
     if not color:
         return
     f = len(self.faces)
     if isinstance(color, (basestring, tuple)):
         color = [color for _ in range(f)]
     color = [FromArgb(*color_to_rgb(c)) for c in color]
     c = len(color)
     if c < f:
         color += [self._default_color for _ in range(f - c)]
     elif c > f:
         color[:] = color[:f]
     self._color = color
Beispiel #17
0
 def display(self):
     points = []
     vcolor = self.network.attributes['color.vertex']
     vcolor = vcolor or self.default_vertexcolor
     vcolor = color_to_rgb(vcolor, True)
     for key, attr in self.network.vertices(True):
         points.append({
             'pos': (attr['x'], attr['y'], attr['z']),
             'size': 6.,
             'color': vcolor,
         })
     lines = []
     ecolor = self.network.attributes['color.vertex']
     ecolor = ecolor or self.default_edgecolor
     ecolor = color_to_rgb(ecolor, True)
     for u, v in self.network.edges():
         lines.append({
             'start': self.network.vertex_coordinates(u),
             'end': self.network.vertex_coordinates(v),
             'color': ecolor,
             'width': 1.
         })
     xdraw_points(points)
     xdraw_lines(lines)
Beispiel #18
0
 def color(self, color):
     if color:
         f = len(self.faces)
         if isinstance(color, (basestring, tuple)):
             # if a single color was provided
             color = [color for _ in range(f)]
         # convert to windows system colors
         color = [FromArgb(*color_to_rgb(c)) for c in color]
         # pad the color specification
         c = len(color)
         if c < f:
             color += [self._default_color for _ in range(f - c)]
         elif c > f:
             color[:] = color[:f]
         # assign to the protected value
         self._color = color
Beispiel #19
0
 def color(self, color):
     if color:
         l = len(self.lines)
         if isinstance(color, (basestring, tuple)):
             # if a single color was provided
             color = [color for _ in range(l)]
         # convert the specified colors to windows system colors
         color[:] = [FromArgb(* color_to_rgb(c)) for c in color]
         c = len(color)
         if c < l:
             # pad the list with default colors
             color += [self._default_color for i in range(l - c)]
         elif c > l:
             # resize to the number of lines
             color[:] = color[:l]
         # assign to the protected attribute
         self._color = color
Beispiel #20
0
 def __set__(self, instance, value):
     color_to_rgb(value, normalize=self.normalize)
Beispiel #21
0
def draw_xlines_xy(lines, axes, alpha=1.0, linestyle='solid'):
    """Creates an XY line collection and adds it to the axis.

    Parameters
    ----------
    lines : list
        List of dictionaries containing the line properties.
        The following properties of a line can be specified in the dict.

        * start (list): XY(Z) coordinates of the start point.
        * end (list): XY(Z) coordinatesof the end point.
        * width (float, optional): The width of the line. Default is ``1.0``.
        * color (rgb tuple or hex string, optional): The color of the line. Default is black.
        * text (str, optional): The text of the label. Default is ``None``.
        * textcolor (rgb tuple or hex string, optional): Color of the label text. Default is black.
        * fontsize (int, optional): The size of the font of the label text. Default is ```12``.

    axes : object
        Matplotlib axes.
    alpha : float, optional
        Opacity of the lines.
        Default is ``1.0``.
    linestyle : str, optional
        Matplotlib line style strings.
        Default is ``'solid'``.

    Returns
    -------
    object
        The matplotlib line collection object.

    """
    fromto = []
    widths = []
    colors = []
    for line in lines:
        sp = line['start']
        ep = line['end']
        width = line.get('width', 1.0)
        color = line.get('color', '#000000')
        text = line.get('text', None)
        textcolor = line.get('textcolor') or '#000000'
        fontsize = line.get('fontsize') or 6
        fromto.append((sp[0:2], ep[0:2]))
        widths.append(width)
        colors.append(color_to_rgb(color, normalize=True))
        if text:
            x, y, z = midpoint_line_xy((sp, ep))
            t = axes.text(x,
                          y,
                          text,
                          fontsize=fontsize,
                          zorder=ZORDER_LABELS,
                          ha='center',
                          va='center',
                          color=color_to_rgb(textcolor, normalize=True))
            t.set_bbox({'color': '#ffffff', 'alpha': 1.0})
    coll = LineCollection(fromto,
                          linewidths=widths,
                          colors=colors,
                          linestyle=linestyle,
                          alpha=alpha,
                          zorder=ZORDER_LINES)
    axes.add_collection(coll)
    return coll
Beispiel #22
0
def draw_xpoints_xy(points, axes):
    """Creates an XY point collection and adds it to the axis.

    Parameters
    ----------
    points : list of dict
        List of dictionaries containing the point properties.
        Each point is represented by a circle with a given radius.
        The following properties of the circle can be specified in the point dict.

        * pos (list): XY(Z) coordinates
        * radius (float, optional): the radius of the circle. Default is 0.1.
        * text (str, optional): the text of the label. Default is None.
        * facecolor (rgb or hex color, optional): The color of the face of the circle. Default is white.
        * edgecolor (rgb or hex color, optional): The color of the edge of the cicrle. Default is black.
        * edgewidth (float, optional): The width of the edge of the circle. Default is 1.0.
        * textcolor (rgb or hex color, optional): Color of the text label. Default is black.
        * fontsize (int, optional): Font size of the text label. Default is 12.

    axes : object
        Matplotlib axes.

    Returns
    -------
    object
        The matplotlib point collection object.

    """
    circles = []
    facecolors = []
    edgecolors = []
    linewidths = []
    for point in points:
        pos = point['pos']
        radius = point['radius']
        text = point.get('text')
        fcolor = point.get('facecolor') or '#ffffff'
        ecolor = point.get('edgecolor') or '#000000'
        lwidth = point.get('edgewidth') or 1.0
        textcolor = point.get('textcolor') or '#000000'
        fontsize = point.get('fontsize') or 12
        circles.append(Circle(pos[0:2], radius=radius))
        facecolors.append(color_to_rgb(fcolor, normalize=True))
        edgecolors.append(color_to_rgb(ecolor, normalize=True))
        linewidths.append(lwidth)
        if text is not None:
            axes.text(pos[0] - 0.01,
                      pos[1] - 0.01,
                      text,
                      fontsize=fontsize,
                      zorder=ZORDER_LABELS,
                      ha='center',
                      va='center',
                      color=textcolor)
    coll = PatchCollection(circles,
                           linewidths=linewidths,
                           facecolors=facecolors,
                           edgecolors=edgecolors,
                           alpha=1.0,
                           zorder=ZORDER_POINTS)
    axes.add_collection(coll)
    return coll
Beispiel #23
0
 def __set__(self, obj, value):
     self.value = color_to_rgb(value, normalize=self.normalize)