diff --git a/src/tikzplotlib/_legend.py b/src/tikzplotlib/_legend.py index 29d8e635..f92be861 100644 --- a/src/tikzplotlib/_legend.py +++ b/src/tikzplotlib/_legend.py @@ -63,6 +63,29 @@ def draw_legend(data, obj): if fill_xcolor != "white": # white is default legend_style.append(f"fill={fill_xcolor}") + mark_options = [] + handles = ( + obj.legendHandles + if hasattr(obj, "legendHandles") + else obj.legend_handles if hasattr(obj, "legend_handles") else None + ) + if handles and any(hasattr(handle, "_sizes") for handle in handles): + handles_with_sizes = [handle for handle in handles if hasattr(handle, "_sizes")] + all_sizes = set(sz for handle in handles_with_sizes for sz in handle._sizes) + if len(all_sizes) > 1: + warnings.warn( + f"Varying marker sizes in the legend: {all_sizes}. Ignoring all of them." + ) + elif all_sizes: + mark_size = all_sizes.pop() + ff = data["float format"] + # setting half size because pgfplots counts the radius/half-width, and sqrt the area + pgf_size = 0.5 * mark_size**0.5 + mark_options.append(f"mark size={pgf_size:{ff}}") + + if mark_options: + legend_style.append(f"mark options={{{', '.join(mark_options)}}}") + # Get the horizontal alignment try: alignment = children_alignment[0]