示例#1
0
 def draw_line(self, ax: plt.Axes, start: Vector, end: Vector,
               properties: Properties, z: float):
     ax.add_line(
         Line2D(
             (start.x, end.x),
             (start.y, end.y),
             linewidth=self.lineweight(properties),
             linestyle=self.pattern(properties),
             color=properties.color,
             zorder=z,
         ))
示例#2
0
def drawLine(ax: plt.Axes,
             x0: float,
             y0: float,
             x1: float,
             y1: float,
             color: float = None,
             linestyle: str = 'solid',
             alpha: float = None,
             linewidth: float = None,
             label: str = None,
             profile=None) -> None:
    """
    Draw a line from ``(x0, y0)`` to ``(x1, y1)``

    Args:
        ax: a plt.Axes to draw on
        x0: x coord of the start point
        y0: y coord of the start point
        x1: x coord of the end point
        y1: y coord of the end point
        color: the color of the line as a value 0-1 within the colormap space
        linestyle: 'solid', 'dashed'
        alpha: a float 0-1
        label: if given, a label is plotted next to the line
        profile: the profile (created via makeProfile) to use. Leave None to
            use the default profile

    Examples
    --------

    >>> import matplotlib.pyplot as plt
    >>> from emlib import matplotting
    >>> fig, ax = plt.subplots()
    >>> matplotting.drawLine(ax, 0, 0, 1, 1)
    >>> plt.show()
    """
    linewidth = _fallback(linewidth, profile, 'line_width')
    alpha = _fallback(alpha, profile, 'line_alpha')
    color = _fallback(color, profile, 'edgecolor')
    X, Y = np.array([[x0, x1], [y0, y1]])
    assert linestyle in ('solid', 'dashed')
    line = mlines.Line2D(X,
                         Y,
                         lw=linewidth,
                         alpha=alpha,
                         color=_colormap(color),
                         linestyle=linestyle)
    ax.add_line(line)
    if label is not None:
        drawLabel(ax, x=(x0 + x1) * 0.5, y=y0, text=label, profile=profile)
    if _get(profile, 'autoscale'):
        autoscaleAxis(ax)
示例#3
0
文件: boid.py 项目: TheDecks/Studies
    def draw(self, ax: plt.Axes, show_range: bool = False):
        """Draw boid on provided matplotlib axes object.

        :param ax: axes on which the boid should be drawn.
        :param show_range: whether to also show "seeing" range of the boid.
        """
        if show_range:
            arc = ptch.Arc(
                (self.x, self.y),
                width=2 * self.lu_distance,
                height=2 * self.lu_distance,
                angle=math.degrees(self.direction),
                theta1=math.degrees(2 * math.pi - self.lu_angle / 2),
                theta2=math.degrees(self.lu_angle / 2),
                color='black',
                linewidth=1,
                alpha=0.3,
                zorder=5)
            line_1 = plt.Line2D([
                self.x + self.lu_distance *
                math.cos(self.lu_angle / 2 + self.direction), self.x
            ], [
                self.y + self.lu_distance *
                math.sin(self.lu_angle / 2 + self.direction), self.y
            ],
                                color='black',
                                linewidth=1,
                                alpha=0.3,
                                zorder=5)
            line_2 = plt.Line2D([
                self.x + self.lu_distance *
                math.cos(2 * math.pi - self.lu_angle / 2 + self.direction),
                self.x
            ], [
                self.y + self.lu_distance *
                math.sin(2 * math.pi - self.lu_angle / 2 + self.direction),
                self.y
            ],
                                color='black',
                                linewidth=1,
                                alpha=0.3,
                                zorder=5)
            ax.add_line(line_1)
            ax.add_line(line_2)
            ax.add_patch(arc)
        ax.add_patch(self._arrow_representation())
示例#4
0
def drawConnectedLines(ax: plt.Axes,
                       pairs: List[Tuple[float, float]],
                       connectEdges=False,
                       color: Union[float, tuple] = None,
                       alpha: float = None,
                       linewidth: float = None,
                       label: str = None,
                       linestyle: str = None,
                       profile: dict = None) -> None:
    """
    Draw an open / closed poligon

    Args:
        ax: the plot axes
        pairs: a list of (x, y) pairs
        connectEdges: close the form, connecting start end end points
        color: the color to use. A float selects a color from the current color map
        alpha: alpha value of the lines
        linewidth: the line width
        label: an optional label to attach to the start of the lines
        linestyle: the line style, one of "solid", "dashed"
        profile: the profile used, or None for default
    """
    linewidth = _fallback(linewidth, profile, 'line_width')
    alpha = _fallback(alpha, profile, 'line_alpha')
    color = _fallback(color, profile, 'edgecolor')
    linestyle = _fallback(linestyle, profile, 'line_style')
    if connectEdges:
        pairs = pairs + (pairs[0])
    color = _getcolor(color)
    xs, ys = _unzip(pairs)
    line = mlines.Line2D(xs,
                         ys,
                         lw=linewidth,
                         alpha=alpha,
                         color=_getcolor(color),
                         linestyle=linestyle)
    ax.add_line(line)
    if label is not None:
        avgx = sum(xs) / len(xs)
        avgy = sum(ys) / len(ys)
        drawLabel(ax, x=avgx, y=avgy, text=label, profile=profile)
    if _get(profile, 'autoscale'):
        autoscaleAxis(ax)
示例#5
0
 def draw_line(self, ax: plt.Axes, start: Vector, end: Vector,
               properties: Properties, z: int):
     pattern = self.pattern(properties)
     lineweight = self.lineweight(properties)
     color = properties.color
     if len(pattern) < 2:
         ax.add_line(
             Line2D(
                 (start.x, end.x),
                 (start.y, end.y),
                 linewidth=lineweight,
                 color=color,
                 zorder=z,
             ))
     else:
         renderer = EzdxfLineTypeRenderer(pattern)
         lines = LineCollection(
             [((s.x, s.y), (e.x, e.y))
              for s, e in renderer.line_segment(start, end)],
             linewidths=lineweight,
             color=color,
             zorder=z)
         lines.set_capstyle('butt')
         ax.add_collection(lines)
示例#6
0
    def __plot_query_state(self,
                           fig: plt.Figure,
                           ax: plt.Axes,
                           u: FiniteMetricVertex,
                           v: FiniteMetricVertex,
                           w: FiniteMetricVertex,
                           i: int,
                           final: bool = False) -> None:
        """
        Plot a single frame/plot that depicts the current state of the ADO
        query. Clears and overwrites any previous contents of the plot.

        :param fig: A Matplotlib figure object representing the figure being
        modified/displayed.
        :param ax: A Matplotlib Axes object representing the subplot being
        modified/displayed.
        :param u: The current value of u in the ADO query algorithm.
        :param v: The current value of v in the ADO query algorithm.
        :param w: The current value of w in the ADO query algorithm.
        :param i: The iteration of the ADO query algorithm.
        :param final: Whether or not this is the final query state/final
        iteration of the algorithm.
        """
        ax.cla()

        # Plot all the points in the graph
        ax.scatter([v.i for v in self.vertices], [v.j for v in self.vertices],
                   s=4,
                   color="black",
                   marker=".",
                   label="Points")

        # Plot u, v, w with special symbols/colors
        ax.scatter([u.i], [u.j], s=12, color="red", marker="*", label="u")
        ax.annotate("u", (u.i, u.j), color="red")
        ax.scatter([v.i], [v.j], s=12, color="green", marker="*", label="v")
        ax.annotate("v", (v.i, v.j), color="green")
        ax.scatter([w.i], [w.j], s=5, color="orange", marker="p", label="w")
        ax.annotate("w", (w.i, w.j),
                    color="orange",
                    xytext=(-15, -10),
                    textcoords="offset pixels")

        # For the current u, mark and label its p_i(u)s
        p_i_u = [self.p[u][i] for i in range(self.k)]
        ax.scatter([v[0].i for v in p_i_u], [v[0].j for v in p_i_u],
                   s=4,
                   color="violet",
                   marker="o",
                   label="p_i(u)")
        for j in range(1, self.k):
            ax.annotate("p_{}(u)".format(j), (p_i_u[j][0].i, p_i_u[j][0].j),
                        xytext=(5, 5),
                        textcoords="offset pixels",
                        color="violet")

        # For the current v, highlight its batch B(v) in a different color
        B_v = [w for w in self.B[v]]
        ax.scatter([w.i for w in B_v], [w.j for w in B_v],
                   s=4,
                   color="lime",
                   marker="*",
                   label="B(v)")

        # Draw line from u to current w
        ax.add_line(Line2D([u.i, w.i], [u.j, w.j], color="pink"))

        # For the final plot, draw a line from w to current v as well
        if final:
            ax.add_line(Line2D([w.i, v.i], [w.j, v.j], color="palegreen"))

        title = "Iteration {} (final)".format(i) \
            if final else "Iteration {}".format(i)

        ax.set_title(title)
        ax.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left')
        plt.tight_layout()
        fig.show()