本文整理匯總了Python中matplotlib.legend.Legend方法的典型用法代碼示例。如果您正苦於以下問題:Python legend.Legend方法的具體用法?Python legend.Legend怎麽用?Python legend.Legend使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類matplotlib.legend
的用法示例。
在下文中一共展示了legend.Legend方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_legend
# 需要導入模塊: from matplotlib import legend [as 別名]
# 或者: from matplotlib.legend import Legend [as 別名]
def get_legend(self):
"""Return the `Legend` instance, or None if no legend is defined."""
return self.legend_
示例2: get_axall_tightbbox
# 需要導入模塊: from matplotlib import legend [as 別名]
# 或者: from matplotlib.legend import Legend [as 別名]
def get_axall_tightbbox(ax, renderer):
'''
Get the tight_bbox of the axis ax, and any dependent decorations, like
a `Legend` instance.
'''
# main bbox of the axis....
bbox = ax.get_tightbbox(renderer=renderer)
# now add the possibility of the legend...
for child in ax.get_children():
if isinstance(child, Legend):
bboxn = child._legend_box.get_window_extent(renderer)
bbox = transforms.Bbox.union([bbox, bboxn])
# add other children here....
return bbox
示例3: get_positions
# 需要導入模塊: from matplotlib import legend [as 別名]
# 或者: from matplotlib.legend import Legend [as 別名]
def get_positions(self) -> (int, int, int, int):
""" get the current position of the target Artist """
points = []
if isinstance(self.target, Rectangle):
points.append(self.target.get_xy())
p2 = (self.target.get_x() + self.target.get_width(), self.target.get_y() + self.target.get_height())
points.append(p2)
elif isinstance(self.target, Ellipse):
c = self.target.center
w = self.target.width
h = self.target.height
points.append((c[0] - w / 2, c[1] - h / 2))
points.append((c[0] + w / 2, c[1] + h / 2))
elif isinstance(self.target, FancyArrowPatch):
points.append(self.target._posA_posB[0])
points.append(self.target._posA_posB[1])
points.extend(self.target.get_path().vertices)
elif isinstance(self.target, Text):
points.append(self.target.get_position())
if checkXLabel(self.target):
points[0] = (points[0][0], self.label_y)
elif checkYLabel(self.target):
points[0] = (self.label_x, points[0][1])
if getattr(self.target, "xy", None) is not None:
points.append(self.target.xy)
bbox = self.target.get_bbox_patch()
if bbox:
points.append(bbox.get_transform().transform((bbox.get_x(), bbox.get_y())))
points.append(
bbox.get_transform().transform((bbox.get_x() + bbox.get_width(), bbox.get_y() + bbox.get_height())))
points[-2:] = self.transform_inverted_points(points[-2:])
elif isinstance(self.target, Axes):
p1, p2 = np.array(self.target.get_position())
points.append(p1)
points.append(p2)
elif isinstance(self.target, Legend):
bbox = self.target.get_frame().get_bbox()
if isinstance(self.target._get_loc(), int):
# if the legend doesn't have a location yet, use the left bottom corner of the bounding box
self.target._set_loc(tuple(self.target.axes.transAxes.inverted().transform(tuple([bbox.x0, bbox.y0]))))
points.append(self.target.axes.transAxes.transform(self.target._get_loc()))
# add points to span bounding box around the frame
points.append([bbox.x0, bbox.y0])
points.append([bbox.x1, bbox.y1])
return self.transform_points(points)
示例4: set_positions
# 需要導入模塊: from matplotlib import legend [as 別名]
# 或者: from matplotlib.legend import Legend [as 別名]
def set_positions(self, points: (int, int)):
""" set the position of the target Artist """
points = self.transform_inverted_points(points)
if isinstance(self.target, Rectangle):
self.target.set_xy(points[0])
self.target.set_width(points[1][0] - points[0][0])
self.target.set_height(points[1][1] - points[0][1])
if self.target.get_label() is None or not self.target.get_label().startswith("_rect"):
self.figure.change_tracker.addChange(self.target, ".set_xy([%f, %f])" % tuple(self.target.get_xy()))
self.figure.change_tracker.addChange(self.target, ".set_width(%f)" % self.target.get_width())
self.figure.change_tracker.addChange(self.target, ".set_height(%f)" % self.target.get_height())
elif isinstance(self.target, Ellipse):
self.target.center = np.mean(points, axis=0)
self.target.width = points[1][0] - points[0][0]
self.target.height = points[1][1] - points[0][1]
self.figure.change_tracker.addChange(self.target, ".center = (%f, %f)" % tuple(self.target.center))
self.figure.change_tracker.addChange(self.target, ".width = %f" % self.target.width)
self.figure.change_tracker.addChange(self.target, ".height = %f" % self.target.height)
elif isinstance(self.target, FancyArrowPatch):
self.target.set_positions(points[0], points[1])
self.figure.change_tracker.addChange(self.target,
".set_positions(%s, %s)" % (tuple(points[0]), tuple(points[1])))
elif isinstance(self.target, Text):
if checkXLabel(self.target):
axes = checkXLabel(self.target)
axes.xaxis.labelpad = -(points[0][1] - self.target.pad_offset) / self.label_factor
self.figure.change_tracker.addChange(axes,
".xaxis.labelpad = %f" % axes.xaxis.labelpad)
self.target.set_position(points[0])
self.label_y = points[0][1]
elif checkYLabel(self.target):
axes = checkYLabel(self.target)
axes.yaxis.labelpad = -(points[0][0] - self.target.pad_offset) / self.label_factor
self.figure.change_tracker.addChange(axes,
".yaxis.labelpad = %f" % axes.yaxis.labelpad)
self.target.set_position(points[0])
self.label_x = points[0][0]
else:
self.target.set_position(points[0])
self.figure.change_tracker.addChange(self.target,
".set_position([%f, %f])" % self.target.get_position())
if getattr(self.target, "xy", None) is not None:
self.target.xy = points[1]
self.figure.change_tracker.addChange(self.target, ".xy = (%f, %f)" % tuple(self.target.xy))
elif isinstance(self.target, Legend):
point = self.target.axes.transAxes.inverted().transform(self.transform_inverted_points(points)[0])
self.target._loc = tuple(point)
self.figure.change_tracker.addChange(self.target, "._set_loc((%f, %f))" % tuple(point))
elif isinstance(self.target, Axes):
position = np.array([points[0], points[1] - points[0]]).flatten()
if self.fixed_aspect:
position[3] = position[2] * self.target.get_position().height / self.target.get_position().width
self.target.set_position(position)
self.figure.change_tracker.addChange(self.target, ".set_position([%f, %f, %f, %f])" % tuple(
np.array([points[0], points[1] - points[0]]).flatten()))
示例5: getSnaps
# 需要導入模塊: from matplotlib import legend [as 別名]
# 或者: from matplotlib.legend import Legend [as 別名]
def getSnaps(targets: List[TargetWrapper], dir: int, no_height=False) -> List[SnapBase]:
""" get all snap objects for the target and the direction """
snaps = []
targets = [t.target for t in targets]
for target in targets:
if isinstance(target, Legend):
continue
if isinstance(target, Text):
if checkXLabel(target):
snaps.append(SnapCenterWith(target, checkXLabel(target), 0))
elif checkYLabel(target):
snaps.append(SnapCenterWith(target, checkYLabel(target), 1))
for ax in target.figure.axes + [target.figure]:
for txt in ax.texts:
# for other texts
if txt in targets or not txt.get_visible():
continue
# snap to the x and the y coordinate
x, y = txt.get_transform().transform(txt.get_position())
snaps.append(SnapSamePos(target, txt, 0))
snaps.append(SnapSamePos(target, txt, 1))
continue
for index, axes in enumerate(target.figure.axes):
if axes not in targets and axes.get_visible():
# axes edged
if dir & DIR_X0:
snaps.append(SnapSameEdge(target, axes, 0))
if dir & DIR_Y0:
snaps.append(SnapSameEdge(target, axes, 1))
if dir & DIR_X1:
snaps.append(SnapSameEdge(target, axes, 2))
if dir & DIR_Y1:
snaps.append(SnapSameEdge(target, axes, 3))
# snap same dimensions
if not no_height:
if dir & DIR_X0:
snaps.append(SnapSameDimension(target, axes, 0))
if dir & DIR_X1:
snaps.append(SnapSameDimension(target, axes, 2))
if dir & DIR_Y0:
snaps.append(SnapSameDimension(target, axes, 1))
if dir & DIR_Y1:
snaps.append(SnapSameDimension(target, axes, 3))
for axes2 in target.figure.axes:
if axes2 != axes and axes2 not in targets and axes2.get_visible():
snaps.append(SnapSameBorder(target, axes, axes2, dir))
return snaps
示例6: get_tightbbox
# 需要導入模塊: from matplotlib import legend [as 別名]
# 或者: from matplotlib.legend import Legend [as 別名]
def get_tightbbox(self, renderer, call_axes_locator=True):
"""
Return the tight bounding box of the axes.
The dimension of the Bbox in canvas coordinate.
If *call_axes_locator* is *False*, it does not call the
_axes_locator attribute, which is necessary to get the correct
bounding box. ``call_axes_locator==False`` can be used if the
caller is only intereted in the relative size of the tightbbox
compared to the axes bbox.
"""
bb = []
if not self.get_visible():
return None
locator = self.get_axes_locator()
if locator and call_axes_locator:
pos = locator(self, renderer)
self.apply_aspect(pos)
else:
self.apply_aspect()
bb.append(self.get_window_extent(renderer))
if self.title.get_visible():
bb.append(self.title.get_window_extent(renderer))
if self._left_title.get_visible():
bb.append(self._left_title.get_window_extent(renderer))
if self._right_title.get_visible():
bb.append(self._right_title.get_window_extent(renderer))
bb_xaxis = self.xaxis.get_tightbbox(renderer)
if bb_xaxis:
bb.append(bb_xaxis)
bb_yaxis = self.yaxis.get_tightbbox(renderer)
if bb_yaxis:
bb.append(bb_yaxis)
for child in self.get_children():
if isinstance(child, OffsetBox) and child.get_visible():
bb.append(child.get_window_extent(renderer))
elif isinstance(child, Legend) and child.get_visible():
bb.append(child._legend_box.get_window_extent(renderer))
_bbox = mtransforms.Bbox.union(
[b for b in bb if b.width != 0 or b.height != 0])
return _bbox