Skip to content

Commit

Permalink
Use constrained_layout instead of manual tight_layout (#700)
Browse files Browse the repository at this point in the history
constrained_layout is stable since matplotlib 3.6.0 and is better than
tight_layout.

Fixes issues with resizing or zooming plots cutting off parts of the
axes.

Closes #699
  • Loading branch information
MichaelGrupp authored Oct 30, 2024
1 parent a6a2e6c commit a43e66c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 6 deletions.
1 change: 0 additions & 1 deletion doc/alignment_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,4 @@
fig.axes.append(ax)
plt.title('only scaled')

fig.tight_layout()
plt.show()
7 changes: 3 additions & 4 deletions evo/tools/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def apply_settings(settings: SettingsContainer = SETTINGS):
"legend.loc": settings.plot_legend_loc,
"lines.linewidth": settings.plot_linewidth,
"text.usetex": settings.plot_usetex,
# NOTE: don't call tight_layout manually anymore. See warning here:
# https://matplotlib.org/stable/users/explain/axes/constrainedlayout_guide.html
"figure.constrained_layout.use": True,
"font.family": settings.plot_fontfamily,
"pgf.texsystem": settings.plot_texsystem
})
Expand Down Expand Up @@ -123,7 +126,6 @@ def __str__(self) -> str:
return self.title + " (" + str(len(self.figures)) + " figure(s))"

def add_figure(self, name: str, fig: Figure) -> None:
fig.tight_layout()
self.figures[name] = fig

@staticmethod
Expand Down Expand Up @@ -179,7 +181,6 @@ def tabbed_tk_window(self) -> None:
nb = ttk.Notebook(self.root_window)
nb.grid(row=1, column=0, sticky='NESW')
for name, fig in self.figures.items():
fig.tight_layout()
tab = ttk.Frame(nb)
canvas = FigureCanvasTkAgg(self.figures[name], master=tab)
canvas.draw()
Expand Down Expand Up @@ -231,7 +232,6 @@ def export(self, file_path: str, confirm_overwrite: bool = True) -> None:
import matplotlib.backends.backend_pdf
pdf = matplotlib.backends.backend_pdf.PdfPages(file_path)
for name, fig in self.figures.items():
# fig.tight_layout() # TODO
pdf.savefig(fig)
pdf.close()
logger.info("Plots saved to " + file_path)
Expand All @@ -241,7 +241,6 @@ def export(self, file_path: str, confirm_overwrite: bool = True) -> None:
if confirm_overwrite and not user.check_and_confirm_overwrite(
dest):
return
fig.tight_layout()
fig.savefig(dest)
logger.info("Plot saved to " + dest)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ requires-python = ">=3.8"

dependencies = [
"numpy>=1.18.5",
"matplotlib",
"matplotlib>=3.6.0",
"scipy>=1.2",
"pandas",
"numexpr>=2.7.3",
Expand Down

0 comments on commit a43e66c

Please sign in to comment.