Skip to content

Conversation

@cvanelteren
Copy link
Collaborator

@cvanelteren cvanelteren commented Dec 6, 2025

Closes #417

This approach takes a similar approach to 946c33a to add the option to add legends to a subset that is not on the outside.

Here is a minimal example

import ultraplot as uplt
import numpy as np

# Create a 2x3 subplot grid
fig, axs = uplt.subplots(nrows=2, ncols=3)

# Add a legend below the first row, spanning the first two columns
fig, ax = uplt.subplots(ncols=2, nrows=2)
ax.plot([1, 2, 3], [4, 5, 6], label="Line 1")
ax.plot([1, 2, 3], [6, 5, 4], label="Line 2")
fig.legend(ax=ax[0, :], loc="bottom", cols=(1, 2))
fig.save("test.png")
test

@cvanelteren cvanelteren marked this pull request as draft December 6, 2025 11:09
@codecov
Copy link

codecov bot commented Dec 6, 2025

Codecov Report

❌ Patch coverage is 96.90722% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
ultraplot/figure.py 72.72% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@cvanelteren cvanelteren requested a review from Copilot December 6, 2025 11:52
@cvanelteren cvanelteren marked this pull request as ready for review December 6, 2025 11:52
@cvanelteren cvanelteren requested a review from beckermr December 6, 2025 11:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds the ability to place legends on specific axes within a figure with precise span control over which subplot columns or rows the legend covers. This mirrors the existing functionality for colorbars (from commit 946c33a) and provides consistent API for controlling legend placement.

Key changes:

  • Added span, row, col, rows, and cols parameters to legend methods for controlling legend placement across subplot grids
  • Implemented validation logic that enforces correct parameter usage (e.g., vertical legends must use rows parameter, horizontal legends must use cols)
  • Added comprehensive test coverage including validation tests, array handling, and multi-side legend placement

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
ultraplot/tests/test_legend.py Adds 15 new test cases covering span functionality, parameter validation, and edge cases for legend placement across subplot grids
ultraplot/figure.py Updates legend() method to accept and process span parameters, extracting single axes from arrays when span is specified, mirroring colorbar implementation
ultraplot/axes/base.py Adds span parameters to legend() and _add_legend() method signatures and passes them to _add_guide_panel() for panel creation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

cvanelteren and others added 4 commits December 6, 2025 21:56
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Collaborator

@beckermr beckermr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc strings need updating I think.

@cvanelteren
Copy link
Collaborator Author

Copy link
Collaborator

@beckermr beckermr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you are correct. Sorry and LGTM!

@beckermr beckermr merged commit 2405218 into Ultraplot:main Dec 7, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request] legend spanning multiple subplots

2 participants