| 52 | |
| 53 | |
| 54 | class DraggableLegend(DraggableOffsetBox): |
| 55 | def __init__(self, legend, use_blit=False, update="loc"): |
| 56 | """ |
| 57 | Wrapper around a `.Legend` to support mouse dragging. |
| 58 | |
| 59 | Parameters |
| 60 | ---------- |
| 61 | legend : `.Legend` |
| 62 | The `.Legend` instance to wrap. |
| 63 | use_blit : bool, optional |
| 64 | Use blitting for faster image composition. For details see |
| 65 | :ref:`func-animation`. |
| 66 | update : {'loc', 'bbox'}, optional |
| 67 | If "loc", update the *loc* parameter of the legend upon finalizing. |
| 68 | If "bbox", update the *bbox_to_anchor* parameter. |
| 69 | """ |
| 70 | self.legend = legend |
| 71 | |
| 72 | _api.check_in_list(["loc", "bbox"], update=update) |
| 73 | self._update = update |
| 74 | |
| 75 | super().__init__(legend, legend._legend_box, use_blit=use_blit) |
| 76 | |
| 77 | def finalize_offset(self): |
| 78 | if self._update == "loc": |
| 79 | self._update_loc(self.get_loc_in_canvas()) |
| 80 | elif self._update == "bbox": |
| 81 | self._update_bbox_to_anchor(self.get_loc_in_canvas()) |
| 82 | |
| 83 | def _update_loc(self, loc_in_canvas): |
| 84 | bbox = self.legend.get_bbox_to_anchor() |
| 85 | # if bbox has zero width or height, the transformation is |
| 86 | # ill-defined. Fall back to the default bbox_to_anchor. |
| 87 | if bbox.width == 0 or bbox.height == 0: |
| 88 | self.legend.set_bbox_to_anchor(None) |
| 89 | bbox = self.legend.get_bbox_to_anchor() |
| 90 | _bbox_transform = BboxTransformFrom(bbox) |
| 91 | self.legend._loc = tuple(_bbox_transform.transform(loc_in_canvas)) |
| 92 | |
| 93 | def _update_bbox_to_anchor(self, loc_in_canvas): |
| 94 | loc_in_bbox = self.legend.axes.transAxes.transform(loc_in_canvas) |
| 95 | self.legend.set_bbox_to_anchor(loc_in_bbox) |
| 96 | |
| 97 | |
| 98 | _legend_kw_doc_base = """ |
no outgoing calls
no test coverage detected
searching dependent graphs…