From 503a2ec2f0afa5f7655cadd49e7f4e868fa9f2b2 Mon Sep 17 00:00:00 2001 From: Jason Gross Date: Wed, 14 Aug 2024 22:11:10 -0700 Subject: [PATCH 1/4] Add mark size for legends --- src/tikzplotlib/_legend.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/tikzplotlib/_legend.py b/src/tikzplotlib/_legend.py index 29d8e635..3f9e42c1 100644 --- a/src/tikzplotlib/_legend.py +++ b/src/tikzplotlib/_legend.py @@ -63,6 +63,23 @@ 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: + all_sizes = set(sz for handle in handles for sz in handle.get_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] From 2e8b86e5eb13adaa992e7f64a90d6e52124af8da Mon Sep 17 00:00:00 2001 From: Jason Gross Date: Thu, 15 Aug 2024 21:17:44 -0700 Subject: [PATCH 2/4] Use _sizes not get_sizes --- src/tikzplotlib/_legend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tikzplotlib/_legend.py b/src/tikzplotlib/_legend.py index 3f9e42c1..7977b139 100644 --- a/src/tikzplotlib/_legend.py +++ b/src/tikzplotlib/_legend.py @@ -67,7 +67,7 @@ def draw_legend(data, obj): mark_options = [] handles = obj.legendHandles if hasattr(obj, "legendHandles") else obj.legend_handles if hasattr(obj, "legend_handles") else None if handles: - all_sizes = set(sz for handle in handles for sz in handle.get_sizes()) + all_sizes = set(sz for handle in handles 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: From c833e3e82233e53eae45507e1c6db2546c62485a Mon Sep 17 00:00:00 2001 From: Jason Gross Date: Thu, 15 Aug 2024 21:37:30 -0700 Subject: [PATCH 3/4] Fix _sizes lookup --- src/tikzplotlib/_legend.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tikzplotlib/_legend.py b/src/tikzplotlib/_legend.py index 7977b139..cbb6134d 100644 --- a/src/tikzplotlib/_legend.py +++ b/src/tikzplotlib/_legend.py @@ -66,8 +66,8 @@ def draw_legend(data, obj): mark_options = [] handles = obj.legendHandles if hasattr(obj, "legendHandles") else obj.legend_handles if hasattr(obj, "legend_handles") else None - if handles: - all_sizes = set(sz for handle in handles for sz in handle._sizes) + if handles and any(hasattr(handle, "_sizes") for handle in handles): + all_sizes = set(sz for handle in handles for sz in handle._sizes if hasattr(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: From aa9adbb75a503c71d62f97ffadfddf4fbdb12af6 Mon Sep 17 00:00:00 2001 From: Jason Gross Date: Thu, 15 Aug 2024 21:59:53 -0700 Subject: [PATCH 4/4] Fix _sizes lookup --- src/tikzplotlib/_legend.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/tikzplotlib/_legend.py b/src/tikzplotlib/_legend.py index cbb6134d..f92be861 100644 --- a/src/tikzplotlib/_legend.py +++ b/src/tikzplotlib/_legend.py @@ -63,18 +63,24 @@ 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 + 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): - all_sizes = set(sz for handle in handles for sz in handle._sizes if hasattr(handle, "_sizes")) + 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.") + 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 + pgf_size = 0.5 * mark_size**0.5 mark_options.append(f"mark size={pgf_size:{ff}}") if mark_options: