예제 #1
0
def create_pipe_collection(net, pipes=None, pipe_geodata=None, junction_geodata=None,
                           use_junction_geodata=False, infofunc=None, cmap=None, norm=None,
                           picker=False, z=None, cbar_title="Pipe Loading [%]", clim=None,
                           **kwargs):
    """
    Creates a matplotlib pipe collection of pandapipes pipes.

    :param net: The pandapipes network
    :type net: pandapipesNet
    :param pipes: The pipes for which the collections are created. If None, all pipes
            in the network are considered.
    :type pipes: list, default None
    :param pipe_geodata: Coordinates to use for plotting. If None, net["pipe_geodata"] is used.
    :type pipe_geodata: pandas.DataFrame, default None
    :param junction_geodata: Coordinates to use for plotting in case of use_junction_geodata=True.\
        If None, net["junction_geodata"] is used.
    :type junction_geodata: pandas.DataFrame, default None
    :param use_junction_geodata: Defines whether junction or pipe geodata are used.
    :type use_junction_geodata: bool, default False
    :param infofunc: infofunction for the line element
    :type infofunc: function, default None
    :param cmap: colormap for the line colors
    :type cmap: matplotlib norm object, default None
    :param norm: matplotlib norm object
    :type norm: matplotlib norm object, default None
    :param picker: Picker argument passed to the patch collection
    :type picker: bool, default False
    :param z: Array of pipe loading magnitudes for colormap. Used in case of given cmap. If None,\
        net.res_pipe.loading_percent is used.
    :type z: array, default None
    :param cbar_title: colormap bar title in case of given cmap
    :type cbar_title: str, default "Pipe Loading [%]"
    :param clim: Setting the norm limits for image scaling
    :type clim: tuple of floats, default None
    :param kwargs: Keyword arguments are passed to the patch function and the patch maker
    :return: lc (matplotlib line collection) - line collection for pipes
    """
    if use_junction_geodata is False and net.pipe_geodata.empty:
        # if bus geodata is available, but no line geodata
        logger.warning("use_junction_geodata is automatically set to True, since net.pipe_geodata "
                       "is empty.")
        use_junction_geodata = True

    pipes = get_index_array(pipes, net.pipe.index)
    if len(pipes) == 0:
        return None

    if use_junction_geodata:
        coords, pipes_with_geo = coords_from_node_geodata(
            pipes, net.pipe.from_junction.loc[pipes].values, net.pipe.to_junction.loc[pipes].values,
            junction_geodata if junction_geodata is not None else net["junction_geodata"], "pipe",
            "Junction")
    else:
        if pipe_geodata is None:
            pipe_geodata = net.pipe_geodata
        pipes_with_geo = pipes[np.isin(pipes, pipe_geodata.index.values)]
        coords = list(pipe_geodata.loc[pipes_with_geo, "coords"])
        pipes_without_geo = set(pipes) - set(pipes_with_geo)
        if pipes_without_geo:
            logger.warning("Could not plot pipes %s. Pipe geodata is missing for those pipes!"
                           % pipes_without_geo)

    if len(pipes_with_geo) == 0:
        return None

    infos = [infofunc(pipe) for pipe in pipes_with_geo] if infofunc else []

    lc = _create_line2d_collection(coords, pipes_with_geo, infos=infos, picker=picker, **kwargs)

    if cmap is not None:
        if z is None:
            z = net.res_pipe.v_mean_m_per_s.loc[pipes_with_geo]
        elif isinstance(z, pd.Series):
            z = z.loc[pipes_with_geo]
        add_cmap_to_collection(lc, cmap, norm, z, cbar_title, clim=clim)

    return lc
예제 #2
0
def create_junction_collection(net, junctions=None, size=5, patch_type="circle", color=None,
                               z=None, cmap=None, norm=None, infofunc=None, picker=False,
                               junction_geodata=None, cbar_title="Junction Pressure [bar]",
                               **kwargs):
    """
    Creates a matplotlib patch collection of pandapipes junctions.

    :param net: The pandapipes network
    :type net: pandapipesNet
    :param junctions: The junctions for which the collections are created.
                      If None, all junctions in the network are considered.
    :type junctions: list, default None
    :param size: Patch size
    :type size: int, default 5
    :param patch_type: Patch type, can be \n
        - "circle" or "ellipse" for an ellipse (cirlces are just ellipses with the same width +\
            height)
        - "rect" or "rectangle" for a rectangle
        - "poly<n>" for a polygon with n edges
    :type patch_type: str, default "circle"
    :param color: Color or list of colors for every element
    :type color: iterable, float, default None
    :param z: Array of magnitudes for colormap. Used in case of given cmap. If None,\
        net.res_junction.p_bar is used.
    :type z: array, default None
    :param cmap: colormap for the patch colors
    :type cmap: matplotlib colormap object, default None
    :param norm:  matplotlib norm object
    :type norm: matplotlib norm object, default None
    :param infofunc: infofunction for the patch element
    :type infofunc: function, default None
    :param picker: Picker argument passed to the patch collection
    :type picker: bool, default False
    :param junction_geodata: Coordinates to use for plotting. If None, net["junction_geodata"] is\
        used
    :type junction_geodata: pandas.DataFrame, default None
    :param cbar_title: colormap bar title in case of given cmap
    :type cbar_title: str, default "Junction Pressure [bar]"
    :param kwargs: Keyword arguments are passed to the patch function and the patch maker
    :return: pc (matplotlib collection object) - patch collection
    """
    junctions = get_index_array(junctions, net.junction.index)
    if len(junctions) == 0:
        return None
    if junction_geodata is None:
        junction_geodata = net["junction_geodata"]

    junctions_with_geo = junctions[np.isin(junctions, junction_geodata.index.values)]
    if len(junctions_with_geo) < len(junctions):
        logger.warning("The following junctions cannot be displayed, as there is no geodata "
                       "available: %s" % (set(junctions) - set(junctions_with_geo)))

    coords = list(zip(junction_geodata.loc[junctions_with_geo, "x"].values,
                      junction_geodata.loc[junctions_with_geo, "y"].values))

    infos = [infofunc(junc) for junc in junctions_with_geo] if infofunc is not None else []

    pc = _create_node_collection(junctions_with_geo, coords, size, patch_type, color, picker, infos,
                                 **kwargs)

    if cmap is not None:
        if z is None:
            z = net.res_junction.p_bar.loc[junctions_with_geo]
        add_cmap_to_collection(pc, cmap, norm, z, cbar_title)

    return pc