def create_bus_collection(net, buses=None, size=5, marker="o", patch_type="circle", colors=None, z=None, cmap=None, norm=None, infofunc=None, picker=False, bus_geodata=None, cbar_title="Bus Voltage [pu]", **kwargs): """ Creates a matplotlib patch collection of pandapower buses. Input: **net** (pandapowerNet) - The pandapower network OPTIONAL: **buses** (list, None) - The buses for which the collections are created. If None, all buses in the network are considered. **size** (int, 5) - patch size **marker** (str, "o") - patch marker **patch_type** (str, "circle") - patch type, can be - "circle" for a circle - "rect" for a rectangle - "poly<n>" for a polygon with n edges **infofunc** (function, None) - infofunction for the patch element **colors** (list, None) - list of colors for every element **z** (array, None) - array of bus voltage magnitudes for colormap. Used in case of given cmap. If None net.res_bus.vm_pu is used. **cmap** (ListedColormap, None) - colormap for the patch colors **norm** (matplotlib norm object, None) - matplotlib norm object **picker** (bool, False) - picker argument passed to the patch collection **bus_geodata** (DataFrame, None) - coordinates to use for plotting If None, net["bus_geodata"] is used **cbar_title** (str, "Bus Voltage [pu]") - colormap bar title in case of given cmap **kwargs - key word arguments are passed to the patch function OUTPUT: **pc** - patch collection """ buses = net.bus.index.tolist() if buses is None else list(buses) if len(buses) == 0: return None if bus_geodata is None: bus_geodata = net["bus_geodata"] coords = zip(bus_geodata.loc[buses, "x"].values, bus_geodata.loc[buses, "y"].values) infos = [] # RegularPolygon has no param width/height, everything else might use defaults if not patch_type.startswith("poly"): if 'height' not in kwargs and 'width' not in kwargs: kwargs['height'] = kwargs['width'] = 2 * size if patch_type == "rect": kwargs['height'] *= 2 kwargs['width'] *= 2 def figmaker(x, y, i): if colors is not None: kwargs["color"] = colors[i] if patch_type == 'ellipse' or patch_type == 'circle': # circles are just ellipses angle = kwargs['angle'] if 'angle' in kwargs else 0 fig = Ellipse((x, y), angle=angle, **kwargs) elif patch_type == "rect": fig = Rectangle([x - kwargs['width'] / 2, y - kwargs['height'] / 2], **kwargs) elif patch_type.startswith("poly"): edges = int(patch_type[4:]) fig = RegularPolygon([x, y], numVertices=edges, radius=size, **kwargs) else: logger.error("Wrong patchtype. Please choose a correct patch type.") if infofunc: infos.append(infofunc(buses[i])) return fig patches = [figmaker(x, y, i) for i, (x, y) in enumerate(coords) if x != np.nan] pc = PatchCollection(patches, match_original=True, picker=picker) pc.bus_indices = np.array(buses) if cmap is not None: pc.set_cmap(cmap) pc.set_norm(norm) if z is None and net is not None: z = net.res_bus.vm_pu.loc[buses] else: logger.warning("z is None and no net is provided") pc.set_array(np.array(z)) pc.has_colormap = True pc.cbar_title = cbar_title pc.patch_type = patch_type pc.size = size if 'orientation' in kwargs: pc.orientation = kwargs['orientation'] if "zorder" in kwargs: pc.set_zorder(kwargs["zorder"]) pc.info = infos return pc
def create_bus_symbol_collection(coords, buses=None, size=5, marker="o", patch_type="circle", colors=None, z=None, cmap=None, norm=None, infofunc=None, picker=False, net=None, **kwargs): infos = [] if 'height' in kwargs and 'width' in kwargs: height, width = kwargs['height'], kwargs['width'] else: height, width = size, size def figmaker(x, y, i): if patch_type == "circle": if colors: fig = Circle((x, y), size, color=colors[i], **kwargs) else: fig = Circle((x, y), size, **kwargs) elif patch_type == 'ellipse': angle = kwargs['angle'] if 'angle' in kwargs else 0 if colors: fig = Ellipse((x, y), width=width, height=height, color=colors[i], **kwargs) else: fig = Ellipse((x, y), width=width, height=height, angle=angle, **kwargs) elif patch_type == "rect": if colors: fig = Rectangle([x - width, y - height], 2 * width, 2 * height, color=colors[i], **kwargs) else: fig = Rectangle([x - width, y - height], 2 * width, 2 * height, **kwargs) elif patch_type.startswith("poly"): edges = int(patch_type[4:]) if colors: fig = RegularPolygon([x, y], numVertices=edges, radius=size, color=colors[i], **kwargs) else: fig = RegularPolygon([x, y], numVertices=edges, radius=size, **kwargs) else: logger.error( "Wrong patchtype. Please choose a correct patch type.") if infofunc: infos.append(infofunc(buses[i])) return fig patches = [ figmaker(x, y, i) for i, (x, y) in enumerate(coords) if x != np.nan ] pc = PatchCollection(patches, match_original=True, picker=picker) pc.bus_indices = np.array(buses) if cmap: pc.set_cmap(cmap) pc.set_norm(norm) if z is None and net: z = net.res_bus.vm_pu.loc[buses] else: logger.warning("z is None and no net is provided") pc.set_array(np.array(z)) pc.has_colormap = True pc.cbar_title = "Bus Voltage [pu]" pc.patch_type = patch_type pc.size = size if 'orientation' in kwargs: pc.orientation = kwargs['orientation'] if "zorder" in kwargs: pc.set_zorder(kwargs["zorder"]) pc.info = infos return pc
def create_bus_collection(net, buses=None, size=5, marker="o", patch_type="circle", colors=None, cmap=None, norm=None, infofunc=None, picker=False, **kwargs): """ Creates a matplotlib patch collection of pandapower buses. Input: **net** (pandapowerNet) - The pandapower network OPTIONAL: **buses** (list, None) - The buses for which the collections are created. If None, all buses in the network are considered. **size** (int, 5) - patch size **marker** (str, "o") - patch marker **patch_type** (str, "circle") - patch type, can be - "circle" for a circle - "rect" for a rectangle - "poly<n>" for a polygon with n edges **infofunc** (function, None) - infofunction for the patch element **colors** (list, None) - list of colors for every element **cmap** - colormap for the patch colors **picker** - picker argument passed to the patch collection **kwargs - key word arguments are passed to the patch function """ buses = net.bus.index.tolist() if buses is None else list(buses) if len(buses) == 0: return None patches = [] infos = [] def figmaker(x, y, i): if patch_type == "circle": if colors: fig = Circle((x, y), size, color=colors[i], **kwargs) else: fig = Circle((x, y), size, **kwargs) elif patch_type == "rect": if colors: fig = Rectangle([x - size, y - size], 2 * size, 2 * size, color=colors[i], **kwargs) else: fig = Rectangle([x - size, y - size], 2 * size, 2 * size, **kwargs) elif patch_type.startswith("poly"): edges = int(patch_type[4:]) if colors: fig = RegularPolygon([x, y], numVertices=edges, radius=size, color=colors[i], **kwargs) else: fig = RegularPolygon([x, y], numVertices=edges, radius=size, **kwargs) if infofunc: infos.append(infofunc(buses[i])) return fig patches = [ figmaker(x, y, i) for i, (x, y) in enumerate( zip(net.bus_geodata.loc[buses].x.values, net.bus_geodata.loc[buses].y.values)) if x != -1 and x != np.nan ] pc = PatchCollection(patches, match_original=True, picker=picker) pc.bus_indices = np.array(buses) if cmap: pc.set_cmap(cmap) pc.set_norm(norm) pc.set_array(net.res_bus.vm_pu.loc[buses]) pc.has_colormap = True pc.cbar_title = "Bus Voltage [pu]" pc.patch_type = patch_type pc.size = size if "zorder" in kwargs: pc.set_zorder(kwargs["zorder"]) pc.info = infos return pc