|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import matplotlib.patches as patches |
| 3 | + |
| 4 | +# build a rectangle in axes coords |
| 5 | +left, width = .25, .5 |
| 6 | +bottom, height = .25, .5 |
| 7 | +right = left + width |
| 8 | +top = bottom + height |
| 9 | + |
| 10 | +fig = plt.figure() |
| 11 | +ax = fig.add_subplot(111) |
| 12 | + |
| 13 | +# axes coordinates are 0,0 is bottom left and 1,1 is upper right |
| 14 | +p = patches.Rectangle( |
| 15 | + (left, bottom), width, height, |
| 16 | + fill=False, transform=ax.transAxes, clip_on=False |
| 17 | + ) |
| 18 | + |
| 19 | +ax.add_patch(p) |
| 20 | + |
| 21 | +ax.text(left, bottom, 'left top', |
| 22 | + horizontalalignment='left', |
| 23 | + verticalalignment='top', |
| 24 | + transform=ax.transAxes) |
| 25 | + |
| 26 | +ax.text(left, bottom, 'left bottom', |
| 27 | + horizontalalignment='left', |
| 28 | + verticalalignment='bottom', |
| 29 | + transform=ax.transAxes) |
| 30 | + |
| 31 | +ax.text(right, top, 'right bottom', |
| 32 | + horizontalalignment='right', |
| 33 | + verticalalignment='bottom', |
| 34 | + transform=ax.transAxes) |
| 35 | + |
| 36 | +ax.text(right, top, 'right top', |
| 37 | + horizontalalignment='right', |
| 38 | + verticalalignment='top', |
| 39 | + transform=ax.transAxes) |
| 40 | + |
| 41 | +ax.text(right, bottom, 'center top', |
| 42 | + horizontalalignment='center', |
| 43 | + verticalalignment='top', |
| 44 | + transform=ax.transAxes) |
| 45 | + |
| 46 | +ax.text(left, 0.5*(bottom+top), 'right center', |
| 47 | + horizontalalignment='right', |
| 48 | + verticalalignment='center', |
| 49 | + rotation='vertical', |
| 50 | + transform=ax.transAxes) |
| 51 | + |
| 52 | +ax.text(left, 0.5*(bottom+top), 'left center', |
| 53 | + horizontalalignment='left', |
| 54 | + verticalalignment='center', |
| 55 | + rotation='vertical', |
| 56 | + transform=ax.transAxes) |
| 57 | + |
| 58 | +ax.text(0.5*(left+right), 0.5*(bottom+top), 'middle', |
| 59 | + horizontalalignment='center', |
| 60 | + verticalalignment='center', |
| 61 | + fontsize=20, color='red', |
| 62 | + transform=ax.transAxes) |
| 63 | + |
| 64 | +ax.text(right, 0.5*(bottom+top), 'centered', |
| 65 | + horizontalalignment='center', |
| 66 | + verticalalignment='center', |
| 67 | + rotation='vertical', |
| 68 | + transform=ax.transAxes) |
| 69 | + |
| 70 | +ax.text(left, top, 'rotated\nwith newlines', |
| 71 | + horizontalalignment='center', |
| 72 | + verticalalignment='center', |
| 73 | + rotation=45, |
| 74 | + transform=ax.transAxes) |
| 75 | + |
| 76 | +ax.set_axis_off() |
| 77 | +plt.show() |
0 commit comments