| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4096,6 +4096,11 @@ def pick(self, *args): | |||
| 4096 | 4096 | martist.Artist.pick(self, args[0]) | |
| 4097 | 4097 | ||
| 4098 | 4098 | def get_default_bbox_extra_artists(self): | |
| 4099 | + """ | ||
| 4100 | + Return a default list of artists that are used for the bounding box | ||
| 4101 | + calculation. Artists are excluded either by not being visible | ||
| 4102 | + or ``artist.inbbox = False``. | ||
| 4103 | + """ | ||
| 4099 | 4104 | return [artist for artist in self.get_children() | |
| 4100 | 4105 | if (artist.get_visible() and artist.inbbox)] | |
| 4101 | 4106 | ||
@@ -4105,8 +4110,8 @@ def get_tightbbox(self, renderer, call_axes_locator=True, | |||
| 4105 | 4110 | Return the tight bounding box of the axes, including axis and their | |
| 4106 | 4111 | decorators (xlabel, title, etc). | |
| 4107 | 4112 | ||
| 4108 | - Artists that have ``artist.inbbox=False`` are not included | ||
| 4109 | - in the bbox. | ||
| 4113 | + Artists that have ``artist.inbbox = False`` are not included | ||
| 4114 | + in the bbox. | ||
| 4110 | 4115 | ||
| 4111 | 4116 | Parameters | |
| 4112 | 4117 | ---------- | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2002,6 +2002,9 @@ def get_tightbbox(self, renderer, bbox_extra_artists=None): | |||
| 2002 | 2002 | """ | |
| 2003 | 2003 | Return a (tight) bounding box of the figure in inches. | |
| 2004 | 2004 | ||
| 2005 | + Artists that have ``artist.inbbox = False`` are not included | ||
| 2006 | + in the bbox. | ||
| 2007 | + | ||
| 2005 | 2008 | Parameters | |
| 2006 | 2009 | ---------- | |
| 2007 | 2010 | renderer : `.RendererBase` instance | |
@@ -2084,6 +2087,10 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None, | |||
| 2084 | 2087 | """ | |
| 2085 | 2088 | Automatically adjust subplot parameters to give specified padding. | |
| 2086 | 2089 | ||
| 2090 | + To exclude an artist on the axes from the bounding box calculation | ||
| 2091 | + that determines the subplot parameters (i.e. legend, or annotation), | ||
| 2092 | + then set `a.inbbox=False` for that artist. | ||
| 2093 | + | ||
| 2087 | 2094 | Parameters | |
| 2088 | 2095 | ---------- | |
| 2089 | 2096 | pad : float | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -172,7 +172,7 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 172 | 172 | ||
| 173 | 173 | fig, ax = plt.subplots(constrained_layout=True) | |
| 174 | 174 | ax.plot(np.arange(10), label='This is a plot') | |
| 175 | - ax.legend(loc='center left', bbox_to_anchor=(0.9, 0.5)) | ||
| 175 | + ax.legend(loc='center left', bbox_to_anchor=(0.8, 0.5)) | ||
| 176 | 176 | ||
| 177 | 177 | ############################################# | |
| 178 | 178 | # However, this will steal space from a subplot layout: | |
@@ -181,7 +181,39 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 181 | 181 | for ax in axs.flatten()[:-1]: | |
| 182 | 182 | ax.plot(np.arange(10)) | |
| 183 | 183 | axs[1, 1].plot(np.arange(10), label='This is a plot') | |
| 184 | - axs[1, 1].legend(loc='center left', bbox_to_anchor=(0.9, 0.5)) | ||
| 184 | + axs[1, 1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5)) | ||
| 185 | + | ||
| 186 | + ############################################# | ||
| 187 | + # In order for a legend or other artist to *not* steal space | ||
| 188 | + # from the subplot layout, it can have its ``inbbox`` property | ||
| 189 | + # set to ``False``. Of course this can mean the legend ends up | ||
| 190 | + # cropped, but can be useful if the plot is subsequently called | ||
| 191 | + # with ``fig.savefig('outname.png', bbox_inches='tight')``. Note, | ||
| 192 | + # however, that the legend's inbbox status will have to be | ||
| 193 | + # toggled again to make the saved file work: | ||
| 194 | + | ||
| 195 | + fig, axs = plt.subplots(2, 2, constrained_layout=True) | ||
| 196 | + for ax in axs.flatten()[:-1]: | ||
| 197 | + ax.plot(np.arange(10)) | ||
| 198 | + axs[1, 1].plot(np.arange(10), label='This is a plot') | ||
| 199 | + leg = axs[1, 1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5)) | ||
| 200 | + leg.inbbox = False | ||
| 201 | + wanttoprint = False | ||
| 202 | + if wanttoprint: | ||
| 203 | + leg.inbbox = True | ||
| 204 | + fig.do_constrained_layout(False) | ||
| 205 | + fig.savefig('outname.png', bbox_inches='tight') | ||
| 206 | + | ||
| 207 | + ############################################# | ||
| 208 | + # A better way to get around this awkwardness is to simply | ||
| 209 | + # use a legend for the figure: | ||
| 210 | + fig, axs = plt.subplots(2, 2, constrained_layout=True) | ||
| 211 | + for ax in axs.flatten()[:-1]: | ||
| 212 | + ax.plot(np.arange(10)) | ||
| 213 | + lines = axs[1, 1].plot(np.arange(10), label='This is a plot') | ||
| 214 | + labels = [l.get_label() for l in lines] | ||
| 215 | + leg = fig.legend(lines, labels, loc='center left', | ||
| 216 | + bbox_to_anchor=(0.8, 0.5), bbox_transform=axs[1, 1].transAxes) | ||
| 185 | 217 | ||
| 186 | 218 | ############################################################################### | |
| 187 | 219 | # Padding and Spacing | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,10 @@ | |||
| 10 | 10 | feature and may not work for some cases. It only checks the extents | |
| 11 | 11 | of ticklabels, axis labels, and titles. | |
| 12 | 12 | ||
| 13 | + See also constrained_layout for a different way to do the same things as | ||
| 14 | + tight-layout: | ||
| 15 | + :doc:`/tutorials/intermediate/constrainedlayout_guide` | ||
| 16 | + | ||
| 13 | 17 | ||
| 14 | 18 | Simple Example | |
| 15 | 19 | ============== | |
@@ -280,6 +284,35 @@ def example_plot(ax, fontsize=12): | |||
| 280 | 284 | None, 1 - (gs2.top-top)], | |
| 281 | 285 | h_pad=0.5) | |
| 282 | 286 | ||
| 287 | + ############################################################################### | ||
| 288 | + # Legends and Annotations | ||
| 289 | + # ======================= | ||
| 290 | + # | ||
| 291 | + # Pre Matplotlih 2.2, legends and annotations were excluded from the bounding | ||
| 292 | + # box calculations that decide the layout. Subsequently these artists were | ||
| 293 | + # added to the calculation, but sometimes it is undesirable to include them. | ||
| 294 | + # For instance in this case it might be good to have the axes shring a bit | ||
| 295 | + # to make room for the legend: | ||
| 296 | + | ||
| 297 | + fig, ax = plt.subplots(figsize=(4, 3)) | ||
| 298 | + lines = ax.plot(range(10), label='A simple plot') | ||
| 299 | + ax.legend(bbox_to_anchor=(0.7, 0.5), loc='center left',) | ||
| 300 | + fig.tight_layout() | ||
| 301 | + plt.show() | ||
| 302 | + | ||
| 303 | + ############################################################################### | ||
| 304 | + # However, sometimes this is not desired (quite often when using | ||
| 305 | + # ``fig.savefig('outname.png', bbox_inches='tight')``). In order to | ||
| 306 | + # remove the legend from the bounding box calculation, we simply set its | ||
| 307 | + # bounding ``inbbox=False`` and the legend will be ignored. | ||
| 308 | + | ||
| 309 | + fig, ax = plt.subplots(figsize=(4, 3)) | ||
| 310 | + lines = ax.plot(range(10), label='B simple plot') | ||
| 311 | + leg = ax.legend(bbox_to_anchor=(0.7, 0.5), loc='center left',) | ||
| 312 | + leg.inbbox = False | ||
| 313 | + fig.tight_layout() | ||
| 314 | + plt.show() | ||
| 315 | + | ||
| 283 | 316 | ############################################################################### | |
| 284 | 317 | # Use with AxesGrid1 | |
| 285 | 318 | # ================== | |
| Back | FazBrowse Home | New Git URL |
0 commit comments