Skip to content

Commit 76d0cbd

Browse files
committed
Add mark size for legends
1 parent 0ed7c3d commit 76d0cbd

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/tikzplotlib/_legend.py

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

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

0 commit comments

Comments
 (0)