Skip to content

Commit

Permalink
we got the class for the legend
Browse files Browse the repository at this point in the history
  • Loading branch information
kauevestena committed Sep 2, 2024
1 parent bb445bf commit 83e96cc
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ osm2geojson
requests
shapely
scikit-learn
pyarrow
pyarrow
svg.py
matplotlib
94 changes: 94 additions & 0 deletions webmap/standalone_legend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.lines import Line2D
from matplotlib.patches import Patch

# thx ChatGPT, employed to transform a scratch code into the class
class StandaloneLegend:
def __init__(self):
# Initialize the figure for the legend
self.legend_elements = []
self.legend_labels = []
self.legendFig = plt.figure("Legend plot")

def add_line(self, label='Line', **kwargs):
"""
Add a line to the legend with customizable parameters.
Parameters:
label (str): The label for the line element.
**kwargs: Additional keyword arguments for Line2D.
"""
line = Line2D([0], [0], label=label, **kwargs)
self.legend_elements.append(line)
self.legend_labels.append(label)

def add_marker(self, marker='o', label='Marker', **kwargs):
"""
Add a marker to the legend with customizable parameters.
Parameters:
marker (str): The marker style.
label (str): The label for the marker element.
**kwargs: Additional keyword arguments for Line2D.
"""
# Add default transparent color to kwargs if not provided
kwargs.setdefault('color', (0.0, 0.0, 0.0, 0.0))
marker = Line2D([0], [0], marker=marker, label=label, **kwargs)
self.legend_elements.append(marker)
self.legend_labels.append(label)

def add_patch(self, facecolor='orange', edgecolor='w', label='Patch', **kwargs):
"""
Add a patch to the legend with customizable parameters.
Parameters:
facecolor (str): The face color of the patch.
edgecolor (str): The edge color of the patch.
label (str): The label for the patch element.
**kwargs: Additional keyword arguments for Patch.
"""
patch = Patch(facecolor=facecolor, edgecolor=edgecolor, label=label, **kwargs)
self.legend_elements.append(patch)
self.legend_labels.append(label)

def add_element(self, element_type, label, **kwargs):
"""
Add a custom element to the legend based on the element type.
Parameters:
element_type (str): The type of element ('line', 'marker', 'patch').
label (str): The label for the element.
**kwargs: Additional keyword arguments for the element.
"""
if element_type == 'line':
self.add_line(label=label, **kwargs)
elif element_type == 'marker':
self.add_marker(label=label, **kwargs)
elif element_type == 'patch':
self.add_patch(label=label, **kwargs)
else:
raise ValueError(f"Unknown element type: {element_type}. Supported types are 'line', 'marker', and 'patch'.")

def export(self, filename='legend.png'):
# Export the legend to an image file
self.legendFig.legend(handles=self.legend_elements, labels=self.legend_labels, loc='center')
self.legendFig.savefig(filename, bbox_inches='tight', transparent=True)
plt.close(self.legendFig) # Close the figure to free memory

# # Example usage:
# legend = StandaloneLegend()
# legend.add_line(color='b', linewidth=4, label='Line')
# legend.add_marker(marker='o', markerfacecolor='g', markersize=15, label='Marker')
# legend.add_patch(facecolor='orange', edgecolor='r', label='Patch')
# # Using add_element iteratively
# elements = [
# ('line', {'color': 'red', 'linewidth': 2, 'label': 'Red Line'}),
# ('marker', {'marker': 's', 'markerfacecolor': 'blue', 'markersize': 10, 'label': 'Square Marker'}),
# ('patch', {'facecolor': 'yellow', 'edgecolor': 'black', 'label': 'Yellow Patch'})
# ]

# for elem_type, kwargs in elements:
# legend.add_element(elem_type, **kwargs)

# legend.export('tests/legend.png')

0 comments on commit 83e96cc

Please sign in to comment.