You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: improve plot1d legend handling with automatic titles and opt-out capability (#622)
This PR enhances the `plot1d` method to provide automatic legend
handling for stacked plots, making it more user-friendly while
maintaining full backwards compatibility.
## Changes Made
### Enhanced Legend Functionality
- **Added `legend` parameter** (default `True`) to control whether
legends are shown for stacked categorical plots
- **Automatic legend titles** set from the axis label when available,
falling back to axis name
- **Robust axis detection** when `ax` is not provided - obtains axis
from the returned artists object using `artists[0].stairs.axes`
(consistent with existing codebase patterns)
- **Error propagation** - removed exception handling around axis
detection so structural issues are caught by tests
- **Opt-out capability** - users can disable legends entirely with
`legend=False`
### Improved Documentation
- **Complete parameter documentation** covering `ax`, `overlay`,
`legend`, and `**kwargs`
- **Clear return type specification** and parameter descriptions
- **Usage guidance** for the new legend functionality
### Example Usage
```python
import hist
import numpy as np
# Create histogram with categorical data
h = hist.Hist(
hist.axis.Regular(50, 0, 10, name="energy", label="Energy [GeV]"),
hist.axis.StrCategory([], name="type", label="Event Type", growth=True)
)
h.fill(energy=np.random.normal(5, 1, 1000), type="Signal")
h.fill(energy=np.random.normal(3, 2, 1000), type="Background")
# Default behavior - legend with axis label as title
h.plot1d() # Shows legend titled "Event Type"
# Opt out of legend
h.plot1d(legend=False) # No legend shown
```
## Technical Details
The implementation leverages the existing `artists[0].stairs.axes`
pattern used elsewhere in the codebase for reliable axis detection. When
no explicit axis label is provided, the legend title defaults to the
axis name, ensuring legends are always meaningful.
For 1D histograms, the legend parameter is available but not applicable
since there are no categories to distinguish.
## Testing
Added comprehensive tests covering:
- Default legend behavior with axis labels as titles
- Legend opt-out functionality
- Histograms without explicit axis labels
- 1D histogram compatibility
- Edge cases and error conditions
The changes are fully backwards compatible and maintain the existing
return value structure.
<!-- START COPILOT CODING AGENT TIPS -->
---
💡 You can make Copilot smarter by setting up custom instructions,
customizing its development environment and configuring Model Context
Protocol (MCP) servers. Learn more [Copilot coding agent
tips](https://gh.io/copilot-coding-agent-tips) in the docs.
---------
Signed-off-by: Henry Schreiner <[email protected]>
Co-authored-by: Henry Schreiner <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: henryiii <[email protected]>
Co-authored-by: Henry Schreiner <[email protected]>
0 commit comments