Skip to content

Commit f36fb46

Browse files
committed
add feature to control the color intensity
1 parent 5f091ec commit f36fb46

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

hammock_plot/hammock_plot.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ def plot(self,
146146
):
147147

148148
var_lst = var
149-
color_lst = color
149+
# parse the color input to enable intensity feature
150+
color_lst = color_lst = self._parse_colors(color)
150151
self.data_df = self.data_df_origin.copy()
151152
for col in self.data_df:
152153
if self.data_df[col].dtype.name == "category":
@@ -623,4 +624,27 @@ def _list_labels(self, ax, figsize_y, figsize_x, label):
623624
coordinates_dict[val] = (x, y)
624625
return ax, coordinates_dict
625626

627+
def _parse_colors(self, color_list):
628+
"""Parses a list of colors, converting 'color=alpha' format to hex with transparency."""
629+
parsed_colors = []
630+
631+
if color_list is None:
632+
return None
633+
634+
for color in color_list:
635+
if "=" in color: # Handle colors with alpha values
636+
try:
637+
color_name, alpha_str = color.split("=") # Split color and alpha
638+
alpha = float(alpha_str) # Convert alpha to float
639+
rgba = mcolors.to_rgba(color_name, alpha=alpha) # Convert to RGBA
640+
hex_color = mcolors.to_hex(rgba, keep_alpha=True) # Convert to Hex
641+
parsed_colors.append(hex_color)
642+
except Exception as e:
643+
print(f"Warning: Invalid color format '{color}', using default color.")
644+
parsed_colors.append(color_name) # Fallback to default color
645+
else:
646+
parsed_colors.append(color) # Add standard color names directly
647+
648+
return parsed_colors
649+
626650

0 commit comments

Comments
 (0)