Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mark size for legends #623

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/tikzplotlib/_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down