text_rotation.py
来自「非原创。很好的python例子」· Python 代码 · 共 42 行
PY
42 行
#!/usr/bin/env python"""The way matplotlib does text layout is counter-intuituve to some, sothis example is designed to make it a little clearer. The text isaligned by it's bounding box (the rectangular box that surrounds theink rectangle). The order of operations is basically rotation thenalignment, rather than alignment then rotation. Basically, the textis centered at your x,y location, rotated around this point, and thenaligned according to the bounding box of the rotated text.So if you specify left, bottom alignment, the bottom left of thebounding box of the rotated text will be at the x,y coordinate of thetext.But a picture is worth a thousand words!"""from pylab import *def addtext(props): text(0.5, 0.5, 'text 0', props, rotation=0) text(1.5, 0.5, 'text 45', props, rotation=45) text(2.5, 0.5, 'text 135', props, rotation=135) text(3.5, 0.5, 'text 225', props, rotation=225) text(4.5, 0.5, 'text -45', props, rotation=-45) yticks([0,.5,1]) grid(True)# the text bounding boxbbox = {'fc':0.8, 'pad':0} subplot(211)addtext({'ha':'center', 'va':'center', 'bbox':bbox})xlim(0,5)xticks(arange(0, 5.1, 0.5), [])ylabel('center / center')subplot(212)addtext({'ha':'left', 'va':'bottom', 'bbox':bbox})xlim(0,5)xticks(arange(0, 5.1, 0.5))ylabel('left / bottom')show()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?