-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
{Radio,Check}Buttons: Add 2D grid labels layout support #30803
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
base: main
Are you sure you want to change the base?
Conversation
b660bc5 to
d74610e
Compare
|
I don't know how to fix the other |
|
I have opened #30810 for the |
timhoffm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm slightly concerned with the 2D API:
- a horizontal layout must be created via
labels=[['button1', 'button2', 'button3']]which is not quite intuitive - OTOH 2D radio buttons are quite rare
So I'm wondering whether only supporting a 1D list and an orientation parameter would be better.
That's also how |
d74610e to
3175cc8
Compare
I agree. I added a commit that explains this explicitly. I was also wondering, would you be interested in a similar change to |
|
The difference with One option would be a
|
Yes, CheckButtons should be handled analogously, but first let’s clarify the API. |
You can achieve the same with I concede that more complex arrangements like are not possible. - We can later expand that API by either allowing None as an empty placeholder in the list or by then introducing 2D labels for explicit placement. But I would leave such extensions to when actual user requests arise. |
|
OK that's pretty convincing. Thinking about this again, at the moment I avoided taking care of inputs like: As for Now this feature is thoroughly tested, both the Python and Rst examples. |
50517f5 to
609da0c
Compare
The RadioButtons widget now supports arranging buttons in a 2D grid by passing a list of lists of strings as the labels parameter. Each inner list represents a row in the grid. Key features: - Active index and index_selected remain as single integers for the flattened array (reading left-to-right, top-to-bottom) - Column positions are automatically calculated based on the maximum text width in each column for optimal spacing - Text offset is now consistent between 1D and 2D layouts (0.10) - Includes new example: galleries/examples/widgets/radio_buttons_grid.py Closes matplotlib#13374 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
609da0c to
bbdf65c
Compare
|
@timhoffm since I used the same algorithm to distribute the buttons and texts for horizontal layouts too, there are tests failing now due to slight differences in the positions of the elements. Would you accept these differences?
|
|
Overall, I find this a bit too invasive to immediately roll out; in particular because (1) it temporarily adds artists and triggers a draw and (2) it changes the positions of the buttons. Let's do the following: factor the position calculation out into a separate function, and introduce if layout is None:
button_xs, button_ys, label_xs, label_ys = _legacy_radio_layout(labels, ...)
else:
button_xs, button_ys, label_xs, label_ys = _radio_layout(labels, ...)
[...]
self.labels = [
ax.text(x, y, label, transform=ax.transAxes,
horizontalalignment="left", verticalalignment="center",
**props)
for x, y, label, props in zip(label_xs, label_ys, labels, label_props)]
[...]
self._buttons = ax.scatter(button_xs, button_ys, **radio_props)By defining |
022a527 to
7ef400d
Compare
|
Indeed outsourcing this layout algorithm would be useful also for |
lib/matplotlib/widgets.py
Outdated
| activecolor : :mpltype:`color` | ||
| The color of the selected button. The default is ``'blue'`` if not | ||
| specified here or in *radio_props*. | ||
| layout : None, {"vertical", "horizontal"} or (int, int), default: None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| layout : None, {"vertical", "horizontal"} or (int, int), default: None | |
| layout : None or {"vertical", "horizontal"} or (int, int), default: None |
numpydoc convention is to separate all types by "or"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
numpydoc convention is to separate all types by "or"
I used an or between the literal strings too, to make it clear the set {"vertical", "horizontal"} is not allowed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okish, but in general https://numpydoc.readthedocs.io/en/latest/format.html#parameters
When a parameter can only assume one of a fixed set of values, those values can be listed in braces, with the default appearing first
There's no explicit rule how to combine this with or (int, int) though. But I'd slighly prefer None or {"vertical", "horizontal"} or (int, int)
|
Maybe before accepting this change, I should perform this change to |
|
As you wish. The inclusion of CheckButtons is small enough so that it can be done here, but it can also be done in a followup PR. |
|
Python 3.11 on ubuntu-22.04 (Minimum Versions) failures are unrelated... |





PR summary
The
RadioButtonswidget now supports arranging buttons in a 2D grid by passing a list of lists of strings as the labels parameter. Each inner list represents a row in the grid.Key features:
index_selectedremain as single integers for theflattened array (reading left-to-right, top-to-bottom)
text width in each column for optimal spacing
0.10)galleries/examples/widgets/radio_buttons_grid.py🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com
PR checklist