|
| 1 | +""" |
| 2 | +You can precisely layout text in data or axes (0,1) coordinates. This |
| 3 | +example shows you some of the alignment and rotation specifications to |
| 4 | +layout text |
| 5 | +""" |
| 6 | + |
| 7 | +from matplotlib.matlab import * |
| 8 | +from matplotlib.lines import Line2D |
| 9 | +from matplotlib.patches import Rectangle |
| 10 | +from matplotlib.transforms import Transform |
| 11 | + |
| 12 | +# build a rectangle in axes coords |
| 13 | +left, width = .25, .5 |
| 14 | +bottom, height = .25, .5 |
| 15 | +right = left + width |
| 16 | +top = bottom + height |
| 17 | +ax = gca() |
| 18 | +p = Rectangle(ax.dpi, ax.bbox, |
| 19 | + (left, bottom), width, height, |
| 20 | + fill=False, |
| 21 | + transx=ax.xaxis.transAxis, |
| 22 | + transy=ax.yaxis.transAxis) |
| 23 | +p.set_clip_on(False) |
| 24 | +ax.add_patch(p) |
| 25 | + |
| 26 | + |
| 27 | +ax.text(left, bottom, 'left top', |
| 28 | + horizontalalignment='left', |
| 29 | + verticalalignment='top', |
| 30 | + transx=ax.xaxis.transAxis, |
| 31 | + transy=ax.yaxis.transAxis) |
| 32 | + |
| 33 | +ax.text(right, top, 'right bottom', |
| 34 | + horizontalalignment='right', |
| 35 | + verticalalignment='bottom', |
| 36 | + transx=ax.xaxis.transAxis, |
| 37 | + transy=ax.yaxis.transAxis) |
| 38 | + |
| 39 | +ax.text(right, top, 'right top', |
| 40 | + horizontalalignment='right', |
| 41 | + verticalalignment='top', |
| 42 | + transx=ax.xaxis.transAxis, |
| 43 | + transy=ax.yaxis.transAxis) |
| 44 | + |
| 45 | +ax.text(right, bottom, 'center top', |
| 46 | + horizontalalignment='center', |
| 47 | + verticalalignment='top', |
| 48 | + transx=ax.xaxis.transAxis, |
| 49 | + transy=ax.yaxis.transAxis) |
| 50 | + |
| 51 | +ax.text(left, 0.5*(bottom+top), 'right center', |
| 52 | + horizontalalignment='right', |
| 53 | + verticalalignment='center', |
| 54 | + rotation='vertical', |
| 55 | + transx=ax.xaxis.transAxis, |
| 56 | + transy=ax.yaxis.transAxis) |
| 57 | + |
| 58 | +ax.text(0.5*(left+right), 0.5*(bottom+top), 'middle', |
| 59 | + horizontalalignment='center', |
| 60 | + verticalalignment='center', |
| 61 | + transx=ax.xaxis.transAxis, |
| 62 | + transy=ax.yaxis.transAxis) |
| 63 | + |
| 64 | +ax.text(right, 0.5*(bottom+top), 'centered', |
| 65 | + horizontalalignment='center', |
| 66 | + verticalalignment='center', |
| 67 | + rotation='vertical', |
| 68 | + transx=ax.xaxis.transAxis, |
| 69 | + transy=ax.yaxis.transAxis) |
| 70 | + |
| 71 | +axis('off') |
| 72 | +savefig('alignment_test') |
| 73 | +show() |
0 commit comments