Skip to content

Commit 1fca52d

Browse files
committed
Cherry-pick PR nschloe#623: Add mark size for legends
1 parent 4d43da4 commit 1fca52d

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

src/tikzplotlib/_legend.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ def draw_legend(data, obj):
6363
if fill_xcolor != "white": # white is default
6464
legend_style.append(f"fill={fill_xcolor}")
6565

66+
mark_options = []
67+
handles = (
68+
obj.legendHandles
69+
if hasattr(obj, "legendHandles")
70+
else obj.legend_handles if hasattr(obj, "legend_handles") else None
71+
)
72+
if handles and any(hasattr(handle, "_sizes") for handle in handles):
73+
handles_with_sizes = [handle for handle in handles if hasattr(handle, "_sizes")]
74+
all_sizes = set(sz for handle in handles_with_sizes for sz in handle._sizes)
75+
if len(all_sizes) > 1:
76+
warnings.warn(
77+
f"Varying marker sizes in the legend: {all_sizes}. Ignoring all of them."
78+
)
79+
elif all_sizes:
80+
mark_size = all_sizes.pop()
81+
ff = data["float format"]
82+
# setting half size because pgfplots counts the radius/half-width, and sqrt the area
83+
pgf_size = 0.5 * mark_size**0.5
84+
mark_options.append(f"mark size={pgf_size:{ff}}")
85+
86+
if mark_options:
87+
legend_style.append(f"mark options={{{', '.join(mark_options)}}}")
88+
6689
# Get the horizontal alignment
6790
try:
6891
alignment = children_alignment[0]

0 commit comments

Comments
 (0)