Skip to content

Improvement for visualize_cut_plane function #1072

@achenry

Description

@achenry

See changes below to plot a clean rectangular plot (example attached), rather than contour being rotated within figure.

Image
def visualize_cut_plane(
    cut_plane,
    ax=None,
    vel_component='u',
    min_speed=None,
    max_speed=None,
    cmap="coolwarm",
    levels=None,
    clevels=None,
    color_bar=False,
    label_contours=False,
    title="",
    **kwargs
):
    """
    Generate pseudocolor mesh plot of the cut_plane.

    Args:
        cut_plane (:py:class:`~.tools.cut_plane.CutPlane`): 2D
            plane through wind plant.
        ax (:py:class:`matplotlib.pyplot.axes`, optional): Figure axes. Defaults
            to None.
        vel_component (str, optional): The velocity component that the cut plane is
            perpendicular to.
        min_speed (float, optional): Minimum value of wind speed for
            contours. Defaults to None.
        max_speed (float, optional): Maximum value of wind speed for
            contours. Defaults to None.
        cmap (str, optional): Colormap specifier. Defaults to
            'coolwarm'.
        levels (np.array, optional): Contour levels for line contour plot.
            Defaults to None.
        clevels (np.array, optional): Contour levels for tricontourf plot.
            Defaults to None.
        color_bar (Boolean, optional): Flag to include a color bar on the plot.
            Defaults to False.
        label_contours (Boolean, optional): Flag to include a numerical contour labels
            on the plot. Defaults to False.
        title (str, optional): User-supplied title for the plot. Defaults to "".
        **kwargs: Additional parameters to pass to line contour plot.

    Returns:
        ax (:py:class:`matplotlib.pyplot.axes`): Figure axes.
    """

    if not ax:
        fig, ax = plt.subplots()

    if vel_component=='u':
        # vel_mesh = cut_plane.df.u.values.reshape(cut_plane.resolution[1], cut_plane.resolution[0])
        if min_speed is None:
            min_speed = cut_plane.df.u.min()
        if max_speed is None:
            max_speed = cut_plane.df.u.max()
    elif vel_component=='v':
        # vel_mesh = cut_plane.df.v.values.reshape(cut_plane.resolution[1], cut_plane.resolution[0])
        if min_speed is None:
            min_speed = cut_plane.df.v.min()
        if max_speed is None:
            max_speed = cut_plane.df.v.max()
    elif vel_component=='w':
        # vel_mesh = cut_plane.df.w.values.reshape(cut_plane.resolution[1], cut_plane.resolution[0])
        if min_speed is None:
            min_speed = cut_plane.df.w.min()
        if max_speed is None:
            max_speed = cut_plane.df.w.max()

    # Allow separate number of levels for tricontourf and for line_contour
    if clevels is None:
        clevels = levels
    
    ## CHANGES START
    corner_values_df = pd.DataFrame(
        data={"x1": [cut_plane.df.x1.min(), cut_plane.df.x1.min(), cut_plane.df.x1.max(), cut_plane.df.x1.max()], 
              "x2": [cut_plane.df.x2.min(), cut_plane.df.x2.max(), cut_plane.df.x2.min(), cut_plane.df.x2.max()], 
              "u": [cut_plane.df.u.max()] * 4})
    cut_plane.df = pd.concat([cut_plane.df, corner_values_df], ignore_index=True)
    ## CHANGES END
    
    # Plot the cut-through
    im = ax.tricontourf(
        cut_plane.df.x1,
        cut_plane.df.x2,
        cut_plane.df.u,
        vmin=min_speed,
        vmax=max_speed,
        levels=clevels,
        cmap=cmap,
        extend="both",
    )

    # Add line contour
    line_contour_cut_plane(
        cut_plane,
        ax=ax,
        levels=levels,
        colors="b",
        label_contours=label_contours,
        linewidths=0.8,
        alpha=0.3,
        **kwargs
    )

    if cut_plane.normal_vector == "x":
        ax.invert_xaxis()

    if color_bar:
        cbar = plt.colorbar(im, ax=ax)
        cbar.set_label('m/s')

    # Set the title
    ax.set_title(title)

    # Make equal axis
    ax.set_aspect("equal")

    return ax

Metadata

Metadata

Assignees

No one assigned

    Labels

    backlogTasks that need to be done but aren't scheduled. Higher priority than ideas-list.new-featureA new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions