Exemplo n.º 1
0
    def draw(self, ax: plt.axes, add_patch: bool = True):
        '''
        * Represent a particle for animation.
        '''
        rectangle = Rectangle((self._x[-1], self._y[-1]),
                              self.length,
                              self.width,
                              edgecolor='r',
                              fill=False)

        if add_patch:
            ax.add_patch(rectangle)

        return rectangle
def fill_color_scale(scale: plt.axes) -> None:
    """Generate and populate color scale for quality on right of figure."""
    # Scale range for normalizing data.
    scale_range = scale.get_ylim()[1] - scale.get_ylim()[0]
    # 255 steps because I like 8 bit RBG values.
    for i in range(255):
        # get the RBG vaules from blue to yellow
        R, G, B = 255 - i, 255 - i, 0 + i
        # Plot the patch in the scale
        scale.add_patch(
            mplpatches.Rectangle((0, (7 + ((i / 255) * scale_range))),
                                 1,
                                 scale_range / 255,
                                 facecolor=(R / 255, G / 255, B / 255)))
    return
def plot_polygon(axes: plt.axes, polygon: MultiPolygon) -> None:
    """Add a polygon to an axes

    Args:
        axes: The axes to add the polygon to
        polygon: The polygon to add

    Returns:
        None
    """
    patch = PolygonPatch(polygon,
                         facecolor=[0, 0, 0.5],
                         edgecolor=[0, 0, 0],
                         alpha=0.5)
    axes.add_patch(patch)
def plot_polygon(axes: plt.axes, polygon: MultiPolygon, color, _label="") \
        -> None:
    """Plot a polygon (or multipolygon) with matplotlib

    Args:
        axes: The matplotlib axes to plot on
        polygon: The polygon to plot
        color: Color to use for shading the polygon
        _label: Label for the polygon that will be displayed in the legend

    Returns:
        None
    """
    patch = PolygonPatch(polygon,
                         facecolor=color,
                         edgecolor=[0, 0, 0],
                         alpha=0.3,
                         label=_label)
    axes.add_patch(patch)
Exemplo n.º 5
0
    def _plot_analytical(self,
                         ax: plt.axes,
                         sym_func,
                         title: str = "",
                         maxmin_hline: bool = True,
                         xunits: str = "",
                         yunits: str = "",
                         xlabel: str = "",
                         ylabel: str = "",
                         color=None,
                         inverted=False):
        """
        Auxiliary function for plotting a sympy.Piecewise analytical function.

        :param ax: a matplotlib.Axes object where the data is to be plotted.
        :param x_vec: array-like, support where the provided symbolic function will be plotted
        :param sym_func: symbolic function using the variable x
        :param title: title to show above the plot, optional
        :param maxmin_hline: when set to False, the extreme values of the function are not displayed
        :param xunits: str, physical unit to be used for the x-axis. Example: "m"
        :param yunits: str, physical unit to be used for the y-axis. Example: "kN"
        :param xlabel: str, physical variable displayed on the x-axis. Example: "Length"
        :param ylabel: str, physical variable displayed on the y-axis. Example: "Shear force"
        :param color: color to be used for the shaded area of the plot. No shading if not provided
        :return: a matplotlib.Axes object representing the plotted data.

        """
        x_vec = np.linspace(self._x0, self._x1,
                            min(int((self.length) * 1000 + 1), 1e4))
        y_lam = lambdify(x, sym_func, "numpy")
        y_vec = np.array([y_lam(t) for t in x_vec])

        if inverted:
            y_vec *= -1

        if color:
            a, b = x_vec[0], x_vec[-1]
            verts = [(a, 0)] + list(zip(x_vec, y_vec)) + [(b, 0)]
            poly = Polygon(verts, facecolor=color, edgecolor='0.5', alpha=0.4)
            ax.add_patch(poly)

        if maxmin_hline:
            tol = 1e-3

            if abs(max(y_vec)) > tol:
                ax.axhline(y=max(y_vec), linestyle='--', color="g", alpha=0.5)
                max_idx = y_vec.argmax()
                plt.annotate('${:0.1f}'.format(
                    y_vec[max_idx] *
                    (1 - 2 * inverted)).rstrip('0').rstrip('.') +
                             " $ {}".format(yunits),
                             xy=(x_vec[max_idx], y_vec[max_idx]),
                             xytext=(8, 0),
                             xycoords=('data', 'data'),
                             textcoords='offset points',
                             size=12)

            if abs(min(y_vec)) > tol:
                ax.axhline(y=min(y_vec), linestyle='--', color="g", alpha=0.5)
                min_idx = y_vec.argmin()
                plt.annotate('${:0.1f}'.format(
                    y_vec[min_idx] *
                    (1 - 2 * inverted)).rstrip('0').rstrip('.') +
                             " $ {}".format(yunits),
                             xy=(x_vec[min_idx], y_vec[min_idx]),
                             xytext=(8, 0),
                             xycoords=('data', 'data'),
                             textcoords='offset points',
                             size=12)

        xspan = x_vec.max() - x_vec.min()
        ax.set_xlim([x_vec.min() - 0.01 * xspan, x_vec.max() + 0.01 * xspan])
        ax.spines['right'].set_visible(False)
        ax.spines['top'].set_visible(False)

        if title:
            ax.set_title(title)

        if xlabel or xunits:
            ax.set_xlabel('{} [{}]'.format(xlabel, xunits))

        if ylabel or yunits:
            ax.set_ylabel("{} [{}]".format(ylabel, yunits))

        return ax