|
| 1 | +""" |
| 2 | +The way matplotlib does text layout is counter-intuituve to some, so |
| 3 | +this example is designed to make it a little clearer. The text is |
| 4 | +aligned by it's bounding box (the rectangular box that surrounds the |
| 5 | +ink rectangle). The order of opertations is basically rotation then |
| 6 | +alignment, rather than alignment then rotation. Basically, the text |
| 7 | +is centered at your x,y location, rotated around this point, and then |
| 8 | +aligned according to the bounding box of the rotated text. |
| 9 | +
|
| 10 | +So if you specify left, bottom alignment, the bottom left of the |
| 11 | +bounding box of the rotated text will be at the x,y coordinate of the |
| 12 | +text. |
| 13 | +
|
| 14 | +But a picture is worth a thousand words! |
| 15 | +""" |
| 16 | +from pylab import * |
| 17 | + |
| 18 | +def addtext(props): |
| 19 | + text(0.5, 0.5, 'text 0', props, rotation=0) |
| 20 | + text(1.5, 0.5, 'text 45', props, rotation=45) |
| 21 | + text(2.5, 0.5, 'text 135', props, rotation=135) |
| 22 | + text(3.5, 0.5, 'text 225', props, rotation=225) |
| 23 | + text(4.5, 0.5, 'text -45', props, rotation=-45) |
| 24 | + yticks([0,.5,1]) |
| 25 | + grid(True) |
| 26 | + |
| 27 | +# the text bounding box |
| 28 | +bbox = {'fc':0.8, 'pad':0} |
| 29 | + |
| 30 | +subplot(211) |
| 31 | +addtext({'ha':'center', 'va':'center', 'bbox':bbox}) |
| 32 | +xlim(0,5) |
| 33 | +xticks(arange(0, 5.1, 0.5), []) |
| 34 | +ylabel('center / center') |
| 35 | +subplot(212) |
| 36 | +addtext({'ha':'left', 'va':'bottom', 'bbox':bbox}) |
| 37 | +xlim(0,5) |
| 38 | +xticks(arange(0, 5.1, 0.5)) |
| 39 | +ylabel('left / bottom') |
| 40 | +show() |
0 commit comments