Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1a34f89

Browse files
story645tacaswell
andcommitted
document how to use text.Annotation for figure annotations and rainbow texts
Co-authored-by: Thomas A Caswell <tcaswell@gmail.com>
1 parent3f2925b commit1a34f89

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

‎galleries/examples/text_labels_and_annotations/rainbow_text.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
importmatplotlib.pyplotasplt
1414

1515
plt.rcParams["font.size"]=20
16+
1617
ax=plt.figure().add_subplot(xticks=[],yticks=[])
1718

1819
# The first word, created with text().
@@ -29,3 +30,42 @@
2930
color="blue",family="serif")# custom properties
3031

3132
plt.show()
33+
34+
# %%
35+
#
36+
# Figure Text
37+
# -----------
38+
#
39+
# Figure text can be concatenated in a similar manner by creating a `~.text.Annotation`
40+
# object and adding it to the figure:
41+
42+
43+
importmatplotlib.textasmtext
44+
45+
fig,ax=plt.subplots(subplot_kw=dict(visible=False))
46+
47+
text=fig.text(.1,.5,"Matplotlib",color="red")
48+
49+
text=mtext.Annotation(" says,",xycoords=text,xy=(1,0),
50+
va="bottom",color="gold",weight="bold")
51+
fig.add_artist(text)# manually add artist to figure
52+
53+
text=mtext.Annotation(" hello",xycoords=text,xy=(1,0),
54+
va="bottom",color="green",style="italic")
55+
fig.add_artist(text)
56+
57+
text=mtext.Annotation(" world!",xycoords=text,xy=(1,0),
58+
va="bottom",color="blue",family="serif")
59+
fig.add_artist(text)
60+
61+
# %%
62+
#
63+
# .. admonition:: References
64+
#
65+
# The use of the following functions, methods, classes and modules is shown
66+
# in this example:
67+
#
68+
# - `matplotlib.axes.Axes.annotate`
69+
# - `matplotlib.text.Annotation`
70+
#
71+
# .. tags:: component: annotation, component: text, styling: color

‎galleries/users_explain/text/annotations.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,33 @@ def __call__(self, x0, y0, width, height, mutation_size):
586586
connectionstyle="arc3,rad=-0.2",
587587
relpos=(1.,0.),
588588
fc="w"))
589+
# %%
590+
# Create Annotation Artist
591+
# ^^^^^^^^^^^^^^^^^^^^^^^^
592+
#
593+
# An annotation artist may also be created using `.text.Annotation` and then added to
594+
# the figure. This can be used to annotate other artists that are on the figure. For
595+
# example:
596+
597+
importmatplotlib.patchesasmpatches
598+
importmatplotlib.textasmtext
599+
600+
fig,ax=plt.subplots(subplot_kw=dict(visible=False),figsize=(3,3))
601+
602+
# add circle to figure
603+
smile=mpatches.Circle((.5,.5),radius=.5,fc='gold',ec='k')
604+
fig.add_artist(smile)
605+
606+
# position text relative to artist
607+
eyes=mtext.Annotation("O o",xy=(.5,.5),xycoords=smile,
608+
fontsize=77,va='bottom',ha='center')
609+
fig.add_artist(eyes)
589610

611+
# connect two endpoints positioned relative to artists
612+
mouth=mtext.Annotation("",xy=(.25,.25),xytext=(.75,.35),
613+
xycoords=smile,textcoords=smile,
614+
arrowprops=dict(arrowstyle="-",lw=5))
615+
fig.add_artist(mouth)
590616
# %%
591617
# Placing Artist at anchored Axes locations
592618
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

‎pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ convention = "numpy"
179179
"galleries/examples/style_sheets/bmh.py" = ["E501"]
180180
"galleries/examples/subplots_axes_and_figures/demo_constrained_layout.py" = ["E402"]
181181
"galleries/examples/text_labels_and_annotations/custom_legends.py" = ["E402"]
182+
"galleries/examples/text_labels_and_annotations/rainbow_text.py" = ["E402"]
182183
"galleries/examples/ticks/date_concise_formatter.py" = ["E402"]
183184
"galleries/examples/ticks/date_formatters_locators.py" = ["F401"]
184185
"galleries/examples/user_interfaces/embedding_in_gtk3_panzoom_sgskip.py" = ["E402"]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp