Control handles for canvas tools. Parameters ---------- ax : `~matplotlib.axes.Axes` Matplotlib Axes where tool handles are displayed. x, y : 1D arrays Coordinates of control handles. marker : str, default: 'o' Shape of marker used to display handle.
| 3214 | |
| 3215 | |
| 3216 | class ToolHandles: |
| 3217 | """ |
| 3218 | Control handles for canvas tools. |
| 3219 | |
| 3220 | Parameters |
| 3221 | ---------- |
| 3222 | ax : `~matplotlib.axes.Axes` |
| 3223 | Matplotlib Axes where tool handles are displayed. |
| 3224 | x, y : 1D arrays |
| 3225 | Coordinates of control handles. |
| 3226 | marker : str, default: 'o' |
| 3227 | Shape of marker used to display handle. See `~.pyplot.plot`. |
| 3228 | marker_props : dict, optional |
| 3229 | Additional marker properties. See `.Line2D`. |
| 3230 | useblit : bool, default: True |
| 3231 | Whether to use blitting for faster drawing (if supported by the |
| 3232 | backend). See the tutorial :ref:`blitting` |
| 3233 | for details. |
| 3234 | """ |
| 3235 | |
| 3236 | def __init__(self, ax, x, y, *, marker='o', marker_props=None, useblit=True): |
| 3237 | self.ax = ax |
| 3238 | props = {'marker': marker, 'markersize': 7, 'markerfacecolor': 'w', |
| 3239 | 'linestyle': 'none', 'alpha': 0.5, 'visible': False, |
| 3240 | 'label': '_nolegend_', |
| 3241 | **cbook.normalize_kwargs(marker_props, Line2D)} |
| 3242 | self._markers = Line2D(x, y, animated=useblit, **props) |
| 3243 | self.ax.add_line(self._markers) |
| 3244 | |
| 3245 | @property |
| 3246 | def x(self): |
| 3247 | return self._markers.get_xdata() |
| 3248 | |
| 3249 | @property |
| 3250 | def y(self): |
| 3251 | return self._markers.get_ydata() |
| 3252 | |
| 3253 | @property |
| 3254 | def artists(self): |
| 3255 | return (self._markers, ) |
| 3256 | |
| 3257 | def set_data(self, pts, y=None): |
| 3258 | """Set x and y positions of handles.""" |
| 3259 | if y is not None: |
| 3260 | x = pts |
| 3261 | pts = np.array([x, y]) |
| 3262 | self._markers.set_data(pts) |
| 3263 | |
| 3264 | def set_visible(self, val): |
| 3265 | self._markers.set_visible(val) |
| 3266 | |
| 3267 | def set_animated(self, val): |
| 3268 | self._markers.set_animated(val) |
| 3269 | |
| 3270 | def closest(self, x, y): |
| 3271 | """Return index and pixel distance to closest index.""" |
| 3272 | pts = np.column_stack([self.x, self.y]) |
| 3273 | # Transform data coordinates to pixel coordinates. |