本文整理匯總了Python中matplotlib.text.Annotation方法的典型用法代碼示例。如果您正苦於以下問題:Python text.Annotation方法的具體用法?Python text.Annotation怎麽用?Python text.Annotation使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類matplotlib.text
的用法示例。
在下文中一共展示了text.Annotation方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_arrow_annotation_get_window_extent
# 需要導入模塊: from matplotlib import text [as 別名]
# 或者: from matplotlib.text import Annotation [as 別名]
def test_arrow_annotation_get_window_extent():
figure = Figure(dpi=100)
figure.set_figwidth(2.0)
figure.set_figheight(2.0)
renderer = RendererAgg(200, 200, 100)
# Text annotation with arrow
annotation = Annotation(
'', xy=(0.0, 50.0), xytext=(50.0, 50.0), xycoords='figure pixels',
arrowprops={
'facecolor': 'black', 'width': 8, 'headwidth': 10, 'shrink': 0.0})
annotation.set_figure(figure)
annotation.draw(renderer)
bbox = annotation.get_window_extent()
points = bbox.get_points()
eq_(bbox.width, 50.0)
assert_almost_equal(bbox.height, 10.0 / 0.72)
eq_(points[0, 0], 0.0)
eq_(points[0, 1], 50.0 - 5 / 0.72)
示例2: test_empty_annotation_get_window_extent
# 需要導入模塊: from matplotlib import text [as 別名]
# 或者: from matplotlib.text import Annotation [as 別名]
def test_empty_annotation_get_window_extent():
figure = Figure(dpi=100)
figure.set_figwidth(2.0)
figure.set_figheight(2.0)
renderer = RendererAgg(200, 200, 100)
# Text annotation with arrow
annotation = Annotation(
'', xy=(0.0, 50.0), xytext=(0.0, 50.0), xycoords='figure pixels')
annotation.set_figure(figure)
annotation.draw(renderer)
bbox = annotation.get_window_extent()
points = bbox.get_points()
eq_(points[0, 0], 0.0)
eq_(points[1, 0], 0.0)
eq_(points[1, 1], 50.0)
eq_(points[0, 1], 50.0)
示例3: _init_offsetText
# 需要導入模塊: from matplotlib import text [as 別名]
# 或者: from matplotlib.text import Annotation [as 別名]
def _init_offsetText(self, direction):
x,y,va,ha = self._offsetText_pos[direction]
self.offsetText = mtext.Annotation("",
xy=(x,y), xycoords="axes fraction",
xytext=(0,0), textcoords="offset points",
#fontproperties = fp,
color = rcParams['xtick.color'],
verticalalignment=va,
horizontalalignment=ha,
)
self.offsetText.set_transform(IdentityTransform())
self.axes._set_artist_props(self.offsetText)
示例4: test_text_annotation_get_window_extent
# 需要導入模塊: from matplotlib import text [as 別名]
# 或者: from matplotlib.text import Annotation [as 別名]
def test_text_annotation_get_window_extent():
figure = Figure(dpi=100)
renderer = RendererAgg(200, 200, 100)
# Only text annotation
annotation = Annotation('test', xy=(0, 0))
annotation.set_figure(figure)
text = Text(text='test', x=0, y=0)
text.set_figure(figure)
bbox = annotation.get_window_extent(renderer=renderer)
text_bbox = text.get_window_extent(renderer=renderer)
eq_(bbox.width, text_bbox.width)
eq_(bbox.height, text_bbox.height)
_, _, d = renderer.get_text_width_height_descent(
'text', annotation._fontproperties, ismath=False)
_, _, lp_d = renderer.get_text_width_height_descent(
'lp', annotation._fontproperties, ismath=False)
below_line = max(d, lp_d)
# These numbers are specific to the current implementation of Text
points = bbox.get_points()
eq_(points[0, 0], 0.0)
eq_(points[1, 0], text_bbox.width)
eq_(points[0, 1], -below_line)
eq_(points[1, 1], text_bbox.height - below_line)
示例5: _init_offsetText
# 需要導入模塊: from matplotlib import text [as 別名]
# 或者: from matplotlib.text import Annotation [as 別名]
def _init_offsetText(self, direction):
x,y,va,ha = self._offsetText_pos[direction]
self.offsetText = mtext.Annotation("",
xy=(x,y), xycoords="axes fraction",
xytext=(0,0), textcoords="offset points",
color = rcParams['xtick.color'],
verticalalignment=va,
horizontalalignment=ha,
)
self.offsetText.set_transform(IdentityTransform())
self.axes._set_artist_props(self.offsetText)
示例6: _init_offsetText
# 需要導入模塊: from matplotlib import text [as 別名]
# 或者: from matplotlib.text import Annotation [as 別名]
def _init_offsetText(self, direction):
x, y, va, ha = self._offsetText_pos[direction]
self.offsetText = mtext.Annotation(
"",
xy=(x, y), xycoords="axes fraction",
xytext=(0, 0), textcoords="offset points",
color=rcParams['xtick.color'],
horizontalalignment=ha, verticalalignment=va,
)
self.offsetText.set_transform(IdentityTransform())
self.axes._set_artist_props(self.offsetText)
示例7: export
# 需要導入模塊: from matplotlib import text [as 別名]
# 或者: from matplotlib.text import Annotation [as 別名]
def export(self, decision_tree, ax=None):
import matplotlib.pyplot as plt
from matplotlib.text import Annotation
if ax is None:
ax = plt.gca()
ax.clear()
ax.set_axis_off()
my_tree = self._make_tree(0, decision_tree.tree_)
draw_tree = buchheim(my_tree)
# important to make sure we're still
# inside the axis after drawing the box
# this makes sense because the width of a box
# is about the same as the distance between boxes
max_x, max_y = draw_tree.max_extents() + 1
ax_width = ax.get_window_extent().width
ax_height = ax.get_window_extent().height
scale_x = ax_width / max_x
scale_y = ax_height / max_y
self.recurse(draw_tree, decision_tree.tree_, ax,
scale_x, scale_y, ax_height)
anns = [ann for ann in ax.get_children()
if isinstance(ann, Annotation)]
# update sizes of all bboxes
renderer = ax.figure.canvas.get_renderer()
for ann in anns:
ann.update_bbox_position_size(renderer)
if self.fontsize is None:
# get figure to data transform
# adjust fontsize to avoid overlap
# get max box width and height
try:
extents = [ann.get_bbox_patch().get_window_extent()
for ann in anns]
max_width = max([extent.width for extent in extents])
max_height = max([extent.height for extent in extents])
# width should be around scale_x in axis coordinates
size = anns[0].get_fontsize() * min(scale_x / max_width,
scale_y / max_height)
for ann in anns:
ann.set_fontsize(size)
except AttributeError:
# matplotlib < 1.5
warnings.warn("Automatic scaling of tree plots requires "
"matplotlib 1.5 or higher. Please specify "
"fontsize.")
return anns
示例8: on_pick
# 需要導入模塊: from matplotlib import text [as 別名]
# 或者: from matplotlib.text import Annotation [as 別名]
def on_pick(self, event):
artist = event.artist
if not artist.get_visible():
return
for idx, artist_set in enumerate(self.pick_sets):
if artist in artist_set:
self.pick_funs[idx](artist)
return
if isinstance(artist, Line2D):
mevent = event.mouseevent
# figure out if we picked marker or line
self.drag_idx = self._get_idx_under_point(mevent)
if self.drag_idx >= 0:
# picked marker.
ax = mevent.inaxes
an, pt, _, _ = self.markers[self.drag_idx]
an.set_visible(False)
pt.set_visible(False)
self.canvas.draw()
self.markers[self.drag_idx][-1] = self.canvas.copy_from_bbox(ax.bbox)
an.set_visible(True)
pt.set_visible(True)
ax.draw_artist(an)
ax.draw_artist(pt)
self.canvas.blit(ax.bbox)
else:
# save data to plot marker later
mxval = mevent.xdata
button = mevent.button
if mxval is not None and button == 1 and not self.marker_line_info:
self.marker_line_info = (artist, mxval, mevent.ydata,
button, mevent.inaxes)
elif isinstance(artist, Annotation):
# delete marker.
mevent = event.mouseevent
if mevent.button == 3:
targ_idx = None
for idx, (an, pt, _, _) in enumerate(self.markers):
if an is artist:
targ_idx = idx
break
if targ_idx is not None:
an, pt, _, _ = self.markers[targ_idx]
del self.markers[targ_idx]
an.set_visible(False)
pt.set_visible(False)
self.canvas.draw()
示例9: add_labels
# 需要導入模塊: from matplotlib import text [as 別名]
# 或者: from matplotlib.text import Annotation [as 別名]
def add_labels(self, labels: List[str]) -> List["Text"]:
"""
Updates background, adds text.
Do NOT call after calling self.add_lines().
"""
nlabel = len(labels)
if nlabel != self.nplots:
raise ValueError(
f"incorrect labels: {self.nplots} plots but {nlabel} labels"
)
cfg = self.cfg
color = cfg.get_label_color
size_pt = cfg.label_font.size
distance_px = cfg.label_padding_ratio * size_pt
@attr.dataclass
class AxisPosition:
pos_axes: float
offset_px: float
align: str
xpos = cfg.label_position.x.match(
left=AxisPosition(0, distance_px, "left"),
right=AxisPosition(1, -distance_px, "right"),
)
ypos = cfg.label_position.y.match(
bottom=AxisPosition(0, distance_px, "bottom"),
top=AxisPosition(1, -distance_px, "top"),
)
pos_axes = (xpos.pos_axes, ypos.pos_axes)
offset_pt = (xpos.offset_px, ypos.offset_px)
out: List["Text"] = []
for label_text, ax in zip(labels, self._axes_mono):
# https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.annotate.html
# Annotation subclasses Text.
text: "Annotation" = ax.annotate(
label_text,
# Positioning
xy=pos_axes,
xycoords="axes fraction",
xytext=offset_pt,
textcoords="offset points",
horizontalalignment=xpos.align,
verticalalignment=ypos.align,
# Cosmetics
color=color,
fontsize=px_from_points(size_pt),
fontfamily=cfg.label_font.family,
fontweight=("bold" if cfg.label_font.bold else "normal"),
fontstyle=("italic" if cfg.label_font.italic else "normal"),
)
out.append(text)
self._save_background()
return out
# Output frames