Fix centre and square state and add rotation for rectangle selector by ericpre · Pull Request #20839 · matplotlib/matplotlib · GitHub
Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/deprecations/20839-EP.rst
5 changes: 5 additions & 0 deletions doc/users/next_whats_new/rectangle_patch_rotation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Rectangle patch rotation point
------------------------------

The rotation point of the `~matplotlib.patches.Rectangle` can now be set to 'xy',
'center' or a 2-tuple of numbers.
40 changes: 40 additions & 0 deletions doc/users/next_whats_new/selector_improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Selectors improvement: rotation, aspect ratio correction and add/remove state
-----------------------------------------------------------------------------

The `~matplotlib.widgets.RectangleSelector` and
`~matplotlib.widgets.EllipseSelector` can now be rotated interactively between
-45° and 45°. The range limits are currently dictated by the implementation.
The rotation is enabled or disabled by striking the *r* key
('r' is the default key mapped to 'rotate' in *state_modifier_keys*) or by calling
``selector.add_state('rotate')``.

The aspect ratio of the axes can now be taken into account when using the
"square" state. This is enabled by specifying ``use_data_coordinates='True'`` when
the selector is initialized.

In addition to changing selector state interactively using the modifier keys
defined in *state_modifier_keys*, the selector state can now be changed
programmatically using the *add_state* and *remove_state* methods.


.. code-block:: python

import matplotlib.pyplot as plt
from matplotlib.widgets import RectangleSelector
import numpy as np

values = np.arange(0, 100)

fig = plt.figure()
ax = fig.add_subplot()
ax.plot(values, values)

selector = RectangleSelector(ax, print, interactive=True,
drag_from_anywhere=True,
use_data_coordinates=True)
selector.add_state('rotate') # alternatively press 'r' key
# rotate the selector interactively

selector.remove_state('rotate') # alternatively press 'r' key

selector.add_state('square')
Comment thread
ericpre marked this conversation as resolved.
10 changes: 10 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8094,3 +8094,13 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
tricontourf = mtri.tricontourf
tripcolor = mtri.tripcolor
triplot = mtri.triplot

def _get_aspect_ratio(self):
"""
Convenience method to calculate the aspect ratio of the axes in
the display coordinate system.
"""
figure_size = self.get_figure().get_size_inches()
ll, ur = self.get_position() * figure_size
width, height = ur - ll
return height / (width * self.get_data_ratio())
59 changes: 53 additions & 6 deletions lib/matplotlib/patches.py
Loading