e2nIEE / pandapower

Convenient Power System Modelling and Analysis based on PYPOWER and pandas
https://www.pandapower.org
Other
811 stars 472 forks source link

[bug] List of colors does not apply to Polygon Patches #2199

Open DEUCE1957 opened 6 months ago

DEUCE1957 commented 6 months ago

Bug report checklis

Reproducible Example

import pandapower
from pandapower.plotting import create_bus_collection, draw_collections
from pandapower.networks import create_cigre_network_lv
import matplotlib.pyplot as plt
grid = create_cigre_network_lv()
fig, ax = plt.subplots()
bus_collection = create_bus_collection(grid, patch_type="poly6",
                                       color="black" # Works
                                       )
draw_collections([bus_collection],
                 ax=ax, plot_colorbars=True, set_aspect=True, axes_visible=(True, True))
plt.show()
fig, ax = plt.subplots()
# THROWS ERROR
bus_collection = create_bus_collection(grid, patch_type="poly6",
                                       color=["black"]*15 # Causes Error
                                       )
draw_collections([bus_collection],
                 ax=ax, plot_colorbars=True, set_aspect=True, axes_visible=(True, True))
plt.show()

Issue Description and Traceback

Above example throws the Error message: "ValueError: RGBA sequence should have length 3 or 4"

This is due to a typo / shadowing issue in the function 'polygon_patches'. Which can be found in 'pandapower.plotting.patch_makers'

def polygon_patches(node_coords, radius, num_edges, color=None, **kwargs):
    """
    ... OMITTED ...
    """
    if not MATPLOTLIB_INSTALLED:
        soft_dependency_error(str(sys._getframe().f_code.co_name)+"()", "matplotlib")
    patches = list()
    if color is not None:
        colors = get_color_list(color, len(node_coords))
        for (x, y), col in zip(node_coords, colors):
           # TYPO IS HERE should be 'col' not 'color', causes list to be used instead of the elements!
            patches.append(RegularPolygon([x, y], numVertices=num_edges, radius=radius, color=color,
                                          **kwargs))
    else:
        for x, y in node_coords:
            patches.append(RegularPolygon([x, y], numVertices=num_edges, radius=radius, **kwargs))
    return patches

Fix is simply to write "color=col" in the patches.append line.

Expected Behavior

Expect to be able to individually control the color of each patch by passing a list of colors. This works for the node_patches, for example, but not for polygon_patches.

Installed Versions

Python: 3.12.1 Pandapower: 2.13.1

Label

vogt31337 commented 1 month ago

maybe could you provide a pull request?