|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import matplotlib.transforms as mtransforms |
| 3 | +from matplotlib.patches import FancyBboxPatch |
| 4 | + |
| 5 | + |
| 6 | +# Bbox object around which the fancy box will be drawn. |
| 7 | +bb = mtransforms.Bbox([[0.3, 0.4], [0.7, 0.6]]) |
| 8 | + |
| 9 | +def draw_bbox(ax, bb): |
| 10 | + # boxstyle=square with pad=0, i.e. bbox itself. |
| 11 | + p_bbox = FancyBboxPatch((bb.xmin, bb.ymin), |
| 12 | + abs(bb.width), abs(bb.height), |
| 13 | + boxstyle="square,pad=0.", |
| 14 | + ec="k", fc="none", zorder=10., |
| 15 | + ) |
| 16 | + ax.add_patch(p_bbox) |
| 17 | + |
| 18 | +def test1(ax): |
| 19 | + |
| 20 | + # a fancy box with round corners. pad=0.1 |
| 21 | + p_fancy = FancyBboxPatch((bb.xmin, bb.ymin), |
| 22 | + abs(bb.width), abs(bb.height), |
| 23 | + boxstyle="round,pad=0.1", |
| 24 | + fc=(1., .8, 1.), |
| 25 | + ec=(1., 0.5, 1.)) |
| 26 | + |
| 27 | + |
| 28 | + ax.add_patch(p_fancy) |
| 29 | + |
| 30 | + ax.text(0.1, 0.8, |
| 31 | + r' boxstyle="round,pad=0.1"', |
| 32 | + size=10, transform=ax.transAxes) |
| 33 | + |
| 34 | + # draws control points for the fancy box. |
| 35 | + #l = p_fancy.get_path().vertices |
| 36 | + #ax.plot(l[:,0], l[:,1], ".") |
| 37 | + |
| 38 | + # draw the original bbox in black |
| 39 | + draw_bbox(ax, bb) |
| 40 | + |
| 41 | + |
| 42 | +def test2(ax): |
| 43 | + |
| 44 | + # bbox=round has two optional argument. pad and rounding_size. |
| 45 | + # They can be set during the initiallization. |
| 46 | + p_fancy = FancyBboxPatch((bb.xmin, bb.ymin), |
| 47 | + abs(bb.width), abs(bb.height), |
| 48 | + boxstyle="round,pad=0.1", |
| 49 | + fc=(1., .8, 1.), |
| 50 | + ec=(1., 0.5, 1.)) |
| 51 | + |
| 52 | + |
| 53 | + ax.add_patch(p_fancy) |
| 54 | + |
| 55 | + # boxstyle and its argument can be later modified with |
| 56 | + # set_boxstyle method. Note that the old attributes are simply |
| 57 | + # forgotten even if the boxstyle name is same. |
| 58 | + |
| 59 | + p_fancy.set_boxstyle("round,pad=0.1, rounding_size=0.2") |
| 60 | + #or |
| 61 | + #p_fancy.set_boxstyle("round", pad=0.1, rounding_size=0.2) |
| 62 | + |
| 63 | + ax.text(0.1, 0.8, |
| 64 | + ' boxstyle="round,pad=0.1\n rounding\\_size=0.2"', |
| 65 | + size=10, transform=ax.transAxes) |
| 66 | + |
| 67 | + # draws control points for the fancy box. |
| 68 | + #l = p_fancy.get_path().vertices |
| 69 | + #ax.plot(l[:,0], l[:,1], ".") |
| 70 | + |
| 71 | + draw_bbox(ax, bb) |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +def test3(ax): |
| 76 | + |
| 77 | + # mutation_scale determine overall scale of the mutation, |
| 78 | + # i.e. both pad and rounding_size is scaled according to this |
| 79 | + # value. |
| 80 | + p_fancy = FancyBboxPatch((bb.xmin, bb.ymin), |
| 81 | + abs(bb.width), abs(bb.height), |
| 82 | + boxstyle="round,pad=0.1", |
| 83 | + mutation_scale=2., |
| 84 | + fc=(1., .8, 1.), |
| 85 | + ec=(1., 0.5, 1.)) |
| 86 | + |
| 87 | + |
| 88 | + ax.add_patch(p_fancy) |
| 89 | + |
| 90 | + ax.text(0.1, 0.8, |
| 91 | + ' boxstyle="round,pad=0.1"\n mutation\\_scale=2', |
| 92 | + size=10, transform=ax.transAxes) |
| 93 | + |
| 94 | + # draws control points for the fancy box. |
| 95 | + #l = p_fancy.get_path().vertices |
| 96 | + #ax.plot(l[:,0], l[:,1], ".") |
| 97 | + |
| 98 | + draw_bbox(ax, bb) |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +def test4(ax): |
| 103 | + |
| 104 | + # When the aspect ratio of the axes is not 1, the fancy box may |
| 105 | + # not be what you expected (green) |
| 106 | + |
| 107 | + p_fancy = FancyBboxPatch((bb.xmin, bb.ymin), |
| 108 | + abs(bb.width), abs(bb.height), |
| 109 | + boxstyle="round,pad=0.2", |
| 110 | + fc="none", |
| 111 | + ec=(0., .5, 0.), zorder=4) |
| 112 | + |
| 113 | + |
| 114 | + ax.add_patch(p_fancy) |
| 115 | + |
| 116 | + |
| 117 | + # You can compensate this by setting the mutation_aspect (pink). |
| 118 | + p_fancy = FancyBboxPatch((bb.xmin, bb.ymin), |
| 119 | + abs(bb.width), abs(bb.height), |
| 120 | + boxstyle="round,pad=0.3", |
| 121 | + mutation_aspect=.5, |
| 122 | + fc=(1., 0.8, 1.), |
| 123 | + ec=(1., 0.5, 1.)) |
| 124 | + |
| 125 | + |
| 126 | + ax.add_patch(p_fancy) |
| 127 | + |
| 128 | + ax.text(0.1, 0.8, |
| 129 | + ' boxstyle="round,pad=0.3"\n mutation\\_aspect=.5', |
| 130 | + size=10, transform=ax.transAxes) |
| 131 | + |
| 132 | + draw_bbox(ax, bb) |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +def test_all(): |
| 137 | + plt.clf() |
| 138 | + |
| 139 | + ax = plt.subplot(2, 2, 1) |
| 140 | + test1(ax) |
| 141 | + ax.set_xlim(0., 1.) |
| 142 | + ax.set_ylim(0., 1.) |
| 143 | + ax.set_title("test1") |
| 144 | + ax.set_aspect(1.) |
| 145 | + |
| 146 | + |
| 147 | + ax = plt.subplot(2, 2, 2) |
| 148 | + ax.set_title("test2") |
| 149 | + test2(ax) |
| 150 | + ax.set_xlim(0., 1.) |
| 151 | + ax.set_ylim(0., 1.) |
| 152 | + ax.set_aspect(1.) |
| 153 | + |
| 154 | + ax = plt.subplot(2, 2, 3) |
| 155 | + ax.set_title("test3") |
| 156 | + test3(ax) |
| 157 | + ax.set_xlim(0., 1.) |
| 158 | + ax.set_ylim(0., 1.) |
| 159 | + ax.set_aspect(1) |
| 160 | + |
| 161 | + ax = plt.subplot(2, 2, 4) |
| 162 | + ax.set_title("test4") |
| 163 | + test4(ax) |
| 164 | + ax.set_xlim(-0.5, 1.5) |
| 165 | + ax.set_ylim(0., 1.) |
| 166 | + ax.set_aspect(2.) |
| 167 | + |
| 168 | + plt.draw() |
| 169 | + plt.show() |
| 170 | + |
| 171 | +test_all() |
0 commit comments