本文整理汇总了Python中matplotlib.offsetbox.AnchoredOffsetbox.set_figure方法的典型用法代码示例。如果您正苦于以下问题:Python AnchoredOffsetbox.set_figure方法的具体用法?Python AnchoredOffsetbox.set_figure怎么用?Python AnchoredOffsetbox.set_figure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matplotlib.offsetbox.AnchoredOffsetbox
的用法示例。
在下文中一共展示了AnchoredOffsetbox.set_figure方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: draw
# 需要导入模块: from matplotlib.offsetbox import AnchoredOffsetbox [as 别名]
# 或者: from matplotlib.offsetbox.AnchoredOffsetbox import set_figure [as 别名]
def draw(self, renderer, *args, **kwargs):
if not self.get_visible():
return
if self.dx == 0:
return
# Get parameters
from matplotlib import rcParams # late import
def _get_value(attr, default):
value = getattr(self, attr)
if value is None:
value = rcParams.get('scalebar.' + attr, default)
return value
length_fraction = _get_value('length_fraction', 0.2)
height_fraction = _get_value('height_fraction', 0.01)
location = _get_value('location', 'upper right')
if isinstance(location,six.string_types):
location = self._LOCATIONS[location]
pad = _get_value('pad', 0.2)
border_pad = _get_value('border_pad', 0.1)
sep = _get_value('sep', 5)
frameon = _get_value('frameon', True)
color = _get_value('color', 'k')
box_color = _get_value('box_color', 'w')
box_alpha = _get_value('box_alpha', 1.0)
scale_loc = _get_value('scale_loc', 'bottom')
label_loc = _get_value('label_loc', 'top')
font_properties = self.font_properties
if font_properties is None:
textprops = {'color': color}
else:
textprops = {'color': color, 'fontproperties': font_properties}
ax = self.axes
xlim, ylim = ax.get_xlim(), ax.get_ylim()
label = self.label
# Create label
if label:
txtlabel = TextArea(label, minimumdescent=False, textprops=textprops)
else:
txtlabel = None
# Create sizebar
length_px = abs(xlim[1] - xlim[0]) * length_fraction
length_px, scale_label = self._calculate_length(length_px)
size_vertical = abs(ylim[1] - ylim[0]) * height_fraction
sizebar = AuxTransformBox(ax.transData)
sizebar.add_artist(Rectangle((0, 0), length_px, size_vertical,
fill=True, facecolor=color,
edgecolor=color))
txtscale = TextArea(scale_label, minimumdescent=False, textprops=textprops)
if scale_loc in ['bottom', 'right']:
children = [sizebar, txtscale]
else:
children = [txtscale, sizebar]
if scale_loc in ['bottom', 'top']:
Packer = VPacker
else:
Packer = HPacker
boxsizebar = Packer(children=children, align='center', pad=0, sep=sep)
# Create final offset box
if txtlabel:
if label_loc in ['bottom', 'right']:
children = [boxsizebar, txtlabel]
else:
children = [txtlabel, boxsizebar]
if label_loc in ['bottom', 'top']:
Packer = VPacker
else:
Packer = HPacker
child = Packer(children=children, align='center', pad=0, sep=sep)
else:
child = boxsizebar
box = AnchoredOffsetbox(loc=location,
pad=pad,
borderpad=border_pad,
child=child,
frameon=frameon)
box.axes = ax
box.set_figure(self.get_figure())
box.patch.set_color(box_color)
box.patch.set_alpha(box_alpha)
box.draw(renderer)
示例2: draw
# 需要导入模块: from matplotlib.offsetbox import AnchoredOffsetbox [as 别名]
# 或者: from matplotlib.offsetbox.AnchoredOffsetbox import set_figure [as 别名]
#.........这里部分代码省略.........
colorbarbox.add_artist(col)
# Create outline
if orientation == 'horizontal':
outline = Rectangle((0, 0), length_fraction, width_fraction,
fill=False, ec=color)
else:
outline = Rectangle((0, 0), width_fraction, length_fraction,
fill=False, ec=color)
colorbarbox.add_artist(outline)
# Create ticks and tick labels
w10th = width_fraction / 10.0
ticklines = []
ticktexts = []
for tick, ticklabel in zip(ticks, ticklabels):
if ticklocation == 'bottom':
x0 = x1 = xtext = tick
y0 = w10th
y1 = -w10th
ytext = -2 * w10th
ha = 'center'
va = 'top'
elif ticklocation == 'top':
x0 = x1 = xtext = tick
y0 = width_fraction - w10th
y1 = width_fraction + w10th
ytext = width_fraction + 2 * w10th
ha = 'center'
va = 'bottom'
elif ticklocation == 'left':
x0 = w10th
x1 = -w10th
xtext = -2 * w10th
y0 = y1 = ytext = tick
ha = 'right'
va = 'center'
elif ticklocation == 'right':
x0 = width_fraction - w10th
x1 = width_fraction + w10th
xtext = width_fraction + 2 * w10th
y0 = y1 = ytext = tick
ha = 'left'
va = 'center'
ticklines.append([(x0, y0), (x1, y1)])
ticklabel = offset_string + ticklabel
ticktext = Text(xtext, ytext, ticklabel,
color=color,
fontproperties=font_properties,
horizontalalignment=ha,
verticalalignment=va)
ticktexts.append(ticktext)
col = LineCollection(ticklines, color=color)
colorbarbox.add_artist(col)
for ticktext in ticktexts:
colorbarbox.add_artist(ticktext)
# Create label
if label:
labelbox = AuxTransformBox(ax.transAxes)
va = 'baseline' if orientation == 'horizontal' else 'center'
text = Text(0, 0, label,
fontproperties=font_properties,
verticalalignment=va,
rotation=orientation,
color=color)
labelbox.add_artist(text)
else:
labelbox = None
# Create final offset box
if ticklocation == 'bottom':
children = [colorbarbox, labelbox] if labelbox else [colorbarbox]
child = VPacker(children=children, align='center', pad=0, sep=sep)
elif ticklocation == 'top':
children = [labelbox, colorbarbox] if labelbox else [colorbarbox]
child = VPacker(children=children, align='center', pad=0, sep=sep)
elif ticklocation == 'left':
children = [labelbox, colorbarbox] if labelbox else [colorbarbox]
child = HPacker(children=children, align='center', pad=0, sep=sep)
elif ticklocation == 'right':
children = [colorbarbox, labelbox] if labelbox else [colorbarbox]
child = HPacker(children=children, align='center', pad=0, sep=sep)
#
box = AnchoredOffsetbox(loc=location,
pad=pad,
borderpad=border_pad,
child=child,
frameon=frameon)
box.axes = ax
box.set_figure(self.get_figure())
box.patch.set_color(box_color)
box.patch.set_alpha(box_alpha)
box.draw(renderer)
示例3: draw
# 需要导入模块: from matplotlib.offsetbox import AnchoredOffsetbox [as 别名]
# 或者: from matplotlib.offsetbox.AnchoredOffsetbox import set_figure [as 别名]
#.........这里部分代码省略.........
patches = []
for x in np.arange(0, length, step_length):
if orientation == 'horizontal':
patch = Rectangle((x, 0), step_length, width)
else:
patch = Rectangle((0, x), width, step_length)
patches.append(patch)
values = np.linspace(np.min(array), np.max(array), nbins)
minvalue, maxvalue = values[0], values[-1]
col = PatchCollection(patches, cmap=cmap,
edgecolors='none')
col.set_array(values)
colorbarbox.add_artist(col)
if orientation == 'horizontal':
patch = Rectangle((0, 0), length, width, fill=False, ec=color)
else:
patch = Rectangle((0, 0), width, length, fill=False, ec=color)
colorbarbox.add_artist(patch)
children.append(colorbarbox)
# Create ticks
tickbox = AuxTransformBox(ax.transData)
if ticks is None:
ticks = [minvalue, maxvalue] # default
if not ticklabels:
ticklabels = ticks[:] # tick label by default
if minvalue not in ticks: # little hack to get right layout position
ticks.append(minvalue)
ticklabels.append('') # no label for this extra tick
if maxvalue not in ticks: # little hack to get right layout position
ticks.append(maxvalue)
ticklabels.append('') # no label for this extra tick
for itick, tick in enumerate(ticks):
if tick > maxvalue or tick < minvalue:
continue # ignore it
# Fraction of colorbar depending of min and max values of colorbar
a = 1 / (maxvalue - minvalue)
b = -a * minvalue
tickfrac = a * tick + b
if orientation == 'horizontal':
tickx = tickfrac * length
ticky = 0
ha = 'center'
va = 'top'
else:
tickx = width
ticky = tickfrac * length
ha = 'left'
va = 'center'
ticktext = Text(tickx, ticky, ticklabels[itick],
color=color,
fontproperties=font_properties,
horizontalalignment=ha,
verticalalignment=va)
tickbox.add_artist(ticktext)
children.append(tickbox)
# Create label
if label:
labelbox = AuxTransformBox(ax.transData)
va = 'baseline' if orientation == 'horizontal' else 'center'
text = Text(0, 0, label,
fontproperties=font_properties,
verticalalignment=va,
rotation=orientation)
labelbox.add_artist(text)
children.insert(0, labelbox)
# Create final offset box
Packer = VPacker if orientation == 'horizontal' else HPacker
child = Packer(children=children, align="center", pad=0, sep=sep)
box = AnchoredOffsetbox(loc=location,
pad=pad,
borderpad=border_pad,
child=child,
frameon=frameon)
box.axes = ax
box.set_figure(self.get_figure())
box.patch.set_color(box_color)
box.patch.set_alpha(box_alpha)
box.draw(renderer)