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

Commitf57a3c2

Browse files
committed
Don't use ImageGrid in demo_text_rotation_mode.
The example can be written without involving axes_grid.
1 parentbc5d5b8 commitf57a3c2

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

‎examples/text_labels_and_annotations/demo_text_rotation_mode.py

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,62 +17,69 @@
1717
the bounding box of the rotated text.
1818
- ``rotation_mode='anchor'`` aligns the unrotated text and then rotates the
1919
text around the point of alignment.
20-
2120
"""
21+
2222
importmatplotlib.pyplotasplt
23-
frommpl_toolkits.axes_grid1.axes_gridimportImageGrid
23+
importnumpyasnp
2424

2525

2626
deftest_rotation_mode(fig,mode,subplot_location):
2727
ha_list= ["left","center","right"]
2828
va_list= ["top","center","baseline","bottom"]
29-
grid=ImageGrid(fig,subplot_location,
30-
nrows_ncols=(len(va_list),len(ha_list)),
31-
share_all=True,aspect=True,cbar_mode=None)
29+
axs=np.empty((len(va_list),len(ha_list)),object)
30+
gs=subplot_location.subgridspec(*axs.shape,hspace=0,wspace=0)
31+
axs[0,0]=fig.add_subplot(gs[0,0])
32+
foriinrange(len(va_list)):
33+
forjinrange(len(ha_list)):
34+
if (i,j)== (0,0):
35+
continue# Already set.
36+
axs[i,j]=fig.add_subplot(
37+
gs[i,j],sharex=axs[0,0],sharey=axs[0,0])
38+
foraxinaxs.flat:
39+
ax.set(aspect=1)
3240

3341
# labels and title
34-
forha,axinzip(ha_list,grid.axes_row[-1]):
35-
ax.axis["bottom"].label.set_text(ha)
36-
forva,axinzip(va_list,grid.axes_column[0]):
37-
ax.axis["left"].label.set_text(va)
38-
grid.axes_row[0][1].set_title(f"rotation_mode='{mode}'",size="large")
42+
forha,axinzip(ha_list,axs[-1, :]):
43+
ax.set_xlabel(ha)
44+
forva,axinzip(va_list,axs[:,0]):
45+
ax.set_ylabel(va)
46+
axs[0,1].set_title(f"rotation_mode='{mode}'",size="large")
3947

40-
ifmode=="default":
41-
kw=dict()
42-
else:
43-
kw=dict(
44-
bbox=dict(boxstyle="square,pad=0.",ec="none",fc="C1",alpha=0.3))
48+
kw= (
49+
{}ifmode=="default"else
50+
{"bbox":dict(boxstyle="square,pad=0.",ec="none",fc="C1",alpha=0.3)}
51+
)
4552

4653
# use a different text alignment in each axes
47-
texts= []
48-
for (va,ha),axinzip([(x,y)forxinva_listforyinha_list],grid):
49-
# prepare axes layout
50-
foraxisinax.axis.values():
51-
axis.toggle(ticks=False,ticklabels=False)
52-
ax.axvline(0.5,color="skyblue",zorder=0)
53-
ax.axhline(0.5,color="skyblue",zorder=0)
54-
ax.plot(0.5,0.5,color="C0",marker="o",zorder=1)
55-
56-
# add text with rotation and alignment settings
57-
tx=ax.text(0.5,0.5,"Tpg",
58-
size="x-large",rotation=40,
59-
horizontalalignment=ha,verticalalignment=va,
60-
rotation_mode=mode,**kw)
61-
texts.append(tx)
54+
fori,vainenumerate(va_list):
55+
forj,hainenumerate(ha_list):
56+
ax=axs[i,j]
57+
# prepare axes layout
58+
ax.set(xticks=[],yticks=[])
59+
ax.axvline(0.5,color="skyblue",zorder=0)
60+
ax.axhline(0.5,color="skyblue",zorder=0)
61+
ax.plot(0.5,0.5,color="C0",marker="o",zorder=1)
62+
# add text with rotation and alignment settings
63+
tx=ax.text(0.5,0.5,"Tpg",
64+
size="x-large",rotation=40,
65+
horizontalalignment=ha,verticalalignment=va,
66+
rotation_mode=mode,**kw)
6267

6368
ifmode=="default":
6469
# highlight bbox
6570
fig.canvas.draw()
66-
forax,txinzip(grid,texts):
67-
bb=tx.get_window_extent().transformed(ax.transData.inverted())
71+
foraxinaxs.flat:
72+
text,=ax.texts
73+
bb=text.get_window_extent().transformed(ax.transData.inverted())
6874
rect=plt.Rectangle((bb.x0,bb.y0),bb.width,bb.height,
6975
facecolor="C1",alpha=0.3,zorder=2)
7076
ax.add_patch(rect)
7177

7278

73-
fig=plt.figure(figsize=(8,6))
74-
test_rotation_mode(fig,"default",121)
75-
test_rotation_mode(fig,"anchor",122)
79+
fig=plt.figure(figsize=(8,5))
80+
gs=fig.add_gridspec(1,2)
81+
test_rotation_mode(fig,"default",gs[0])
82+
test_rotation_mode(fig,"anchor",gs[1])
7683
plt.show()
7784

7885

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp