Skip to content

Commit

Permalink
replace bbox_inches='tight' with fig.tight_layout
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Aug 6, 2023
1 parent b9ca27b commit e62a722
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Fixed
'''''''
- Errors associated with using keyword arguments in ``plt.gca()`` deprecated in matplotlib 3.4.
- 3D plots now use matplotlib standard for setting equal aspect ratios.
- Replaced use of ``bbox`` with ``bbox_inches``.
- Replaced use of ``bbox`` and ``bbox_inches`` with ``fig.tight_layout()``.


`1.5.6`_ - 2022-09-01
Expand Down
6 changes: 4 additions & 2 deletions docs/source/sphinx_gallery/plot_demos.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def create_welcome():

sub_fname = 'welcome_examples.png'
sub_filename = os.path.join(example_dir, sub_fname)
plt.savefig(sub_filename, pad_inches=0, bbox_inches='tight', dpi=200)
fig.tight_layout()
plt.savefig(sub_filename, pad_inches=0, dpi=200)
plt.close('all')


Expand All @@ -92,7 +93,8 @@ def seed_poly_tri(filepath):

sub_fname = 'joined.png'
sub_filename = os.path.join(ex_path, sub_fname)
plt.savefig(sub_filename, pad_inches=0, bbox_inches='tight', dpi=300)
fig.tight_layout()
plt.savefig(sub_filename, pad_inches=0, dpi=300)
plt.close('all')


Expand Down
15 changes: 9 additions & 6 deletions src/microstructpy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,12 +735,13 @@ def plot_seeds(seeds, phases, domain, plot_files=[], plot_axes=True,
_misc.axisEqual3D(plt.gca())

# Save plot
fig.tight_layout()
for fname in plot_files:
if n_dim == 3:
fig.subplots_adjust(**_misc.plt_3d_adj)
plt.savefig(fname, bbox_inches='tight', pad_inches=0.15)
plt.savefig(fname, pad_inches=0.15)
else:
plt.savefig(fname, bbox_inches='tight', pad_inches=0)
plt.savefig(fname, pad_inches=0)

plt.close('all')

Expand Down Expand Up @@ -860,12 +861,13 @@ def plot_poly(pmesh, phases, plot_files=['polymesh.png'], plot_axes=True,
pmesh.plot(facecolors=fcs, index_by='seed', **edge_kwargs)

# save plot
fig.tight_layout()
for fname in plot_files:
if n_dim == 3:
fig.subplots_adjust(**_misc.plt_3d_adj)
plt.savefig(fname, bbox_inches='tight', pad_inches=0.15)
plt.savefig(fname, pad_inches=0.15)
else:
plt.savefig(fname, bbox_inches='tight', pad_inches=0)
plt.savefig(fname, pad_inches=0)
plt.close('all')


Expand Down Expand Up @@ -1036,12 +1038,13 @@ def plot_tri(tmesh, phases, seeds, pmesh, plot_files=[], plot_axes=True,
tmesh.plot(facecolors=fcs, index_by='attribute', **edge_kwargs)

# save plot
fig.tight_layout()
for fname in plot_files:
if n_dim == 3:
fig.subplots_adjust(**_misc.plt_3d_adj)
plt.savefig(fname, bbox_inches='tight', pad_inches=0.15)
plt.savefig(fname, pad_inches=0.15)
else:
plt.savefig(fname, bbox_inches='tight', pad_inches=0)
plt.savefig(fname, pad_inches=0)

plt.close('all')

Expand Down
7 changes: 4 additions & 3 deletions src/microstructpy/examples/docs_banner.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def main():
k = 0.12
len_x = 3 * domain.length + 4 * off
len_y = domain.width + 2 * off
plt.figure(figsize=(k * len_x, k * len_y))
fig = plt.figure(figsize=(k * len_x, k * len_y))

# Plot Seeds
seed_colors = [phases[s.phase]['color'] for s in seeds]
Expand Down Expand Up @@ -123,8 +123,9 @@ def main():
plt.axis(list(xlim) + list(ylim))

fname = os.path.join(dirname, 'banner.png')
plt.savefig(fname, bbox_inches='tight', pad_inches=0)
plt.savefig(fname.replace('.png', '.pdf'), bbox_inches='tight', pad_inches=0)
fig.tight_layout()
plt.savefig(fname, pad_inches=0)
plt.savefig(fname.replace('.png', '.pdf'), pad_inches=0)


def ordered_kps(pairs):
Expand Down
3 changes: 2 additions & 1 deletion src/microstructpy/examples/foam.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ def main():
plt.gca().set_axis_off()
plt.gca().get_xaxis().set_visible(False)
plt.gca().get_yaxis().set_visible(False)
plt.gcf().tight_layout()

xlim, ylim = domain.limits
plt.axis([xlim[0], xlim[1], ylim[0], ylim[1]])

for ext in ['png', 'pdf']:
fname = os.path.join(dirname, 'trimesh.' + ext)
plt.savefig(fname, bbox_inches='tight', pad_inches=0)
plt.savefig(fname, pad_inches=0)


def pick_edge(void_tess):
Expand Down
5 changes: 3 additions & 2 deletions src/microstructpy/examples/from_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
else:
fnum_bottom = facet_bottom[i, j]

# region
# update region
region = (fnum_top, fnum_left, fnum_bottom, fnum_right)
regions[k_regions] = region
region_phases[k_regions] = bin_nums[i, j]
Expand Down Expand Up @@ -137,6 +137,7 @@
dirs = os.path.dirname(filename)
if not os.path.exists(dirs):
os.makedirs(dirs)
plt.savefig(filename, bbox_inches='tight', pad_inches=0)
plt.gcf().tight_layout()
plt.savefig(filename, pad_inches=0)

shutil.copy(image_filename, dirs)
3 changes: 2 additions & 1 deletion src/microstructpy/examples/grain_neighborhoods.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@
dirs = os.path.dirname(filename)
if not os.path.exists(dirs):
os.makedirs(dirs)
plt.savefig(filename, bbox_inches='tight', pad_inches=0)
plt.gcf().tight_layout()
plt.savefig(filename, pad_inches=0)
3 changes: 2 additions & 1 deletion src/microstructpy/examples/logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ def main(n_seeds, size_rng, pos_rng, k_lw):
horizontalalignment='center',
verticalalignment='center')
plt.draw()
plt.savefig(social_filename, bbox_inches='tight')
fig_social.tight_layout()
plt.savefig(social_filename)
plt.close('all')


Expand Down
3 changes: 2 additions & 1 deletion src/microstructpy/examples/standard_voronoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
dirs = os.path.dirname(filename)
if not os.path.exists(dirs):
os.makedirs(dirs)
plt.savefig(filename, bbox_inches='tight', pad_inches=0)
plt.gcf().tight_layout()
plt.savefig(filename, pad_inches=0)
3 changes: 2 additions & 1 deletion src/microstructpy/examples/uniform_seeding.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@
dirs = os.path.dirname(filename)
if not os.path.exists(dirs):
os.makedirs(dirs)
plt.savefig(filename, bbox_inches='tight', pad_inches=0)
plt.gcf().tight_layout()
plt.savefig(filename, pad_inches=0)

0 comments on commit e62a722

Please sign in to comment.