Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tight(er) whitespace around figure when saving #101

Merged
merged 2 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 30 additions & 20 deletions docs/source/example_notebooks/figsizes.ipynb

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/source/getting_started/application_icml2022.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ is `plt.rcParams.update(bundles.icml2022())`:
>>> import matplotlib.pyplot as plt
>>> from tueplots import bundles
>>> bundles.icml2022()
{'text.usetex': True, 'font.family': 'serif', 'text.latex.preamble': '\\usepackage{times} ', 'figure.figsize': (3.25, 2.0086104634371584), 'figure.constrained_layout.use': True, 'figure.autolayout': False, 'font.size': 8, 'axes.labelsize': 8, 'legend.fontsize': 6, 'xtick.labelsize': 6, 'ytick.labelsize': 6, 'axes.titlesize': 8}
{'text.usetex': True, 'font.family': 'serif', 'text.latex.preamble': '\\usepackage{times} ', 'figure.figsize': (3.25, 2.0086104634371584), 'figure.constrained_layout.use': True, 'figure.autolayout': False, 'savefig.bbox': 'tight', 'savefig.pad_inches': 0.015, 'font.size': 8, 'axes.labelsize': 8, 'legend.fontsize': 6, 'xtick.labelsize': 6, 'ytick.labelsize': 6, 'axes.titlesize': 8}
>>> bundles.icml2022(family="sans-serif", usetex=False, column="full", nrows=2)
{'text.usetex': False, 'font.serif': ['Times'], 'mathtext.fontset': 'stix', 'mathtext.rm': 'Times', 'mathtext.it': 'Times:italic', 'mathtext.bf': 'Times:bold', 'font.family': 'sans-serif', 'figure.figsize': (6.75, 8.343458848123582), 'figure.constrained_layout.use': True, 'figure.autolayout': False, 'font.size': 8, 'axes.labelsize': 8, 'legend.fontsize': 6, 'xtick.labelsize': 6, 'ytick.labelsize': 6, 'axes.titlesize': 8}
{'text.usetex': False, 'font.serif': ['Times'], 'mathtext.fontset': 'stix', 'mathtext.rm': 'Times', 'mathtext.it': 'Times:italic', 'mathtext.bf': 'Times:bold', 'font.family': 'sans-serif', 'figure.figsize': (6.75, 8.343458848123582), 'figure.constrained_layout.use': True, 'figure.autolayout': False, 'savefig.bbox': 'tight', 'savefig.pad_inches': 0.015, 'font.size': 8, 'axes.labelsize': 8, 'legend.fontsize': 6, 'xtick.labelsize': 6, 'ytick.labelsize': 6, 'axes.titlesize': 8}
>>>
>>> # Plug any of those into either the rcParams or into an rc_context:
>>> plt.rcParams.update(bundles.icml2022())
Expand All @@ -27,9 +27,9 @@ If you don't want a pre-packaged solution, at least fix your figure- and font-si
```python
>>> from tueplots import figsizes, fontsizes, fonts
>>> figsizes.icml2022_full()
{'figure.figsize': (6.75, 2.0858647120308955), 'figure.constrained_layout.use': True, 'figure.autolayout': False}
{'figure.figsize': (6.75, 2.0858647120308955), 'figure.constrained_layout.use': True, 'figure.autolayout': False, 'savefig.bbox': 'tight', 'savefig.pad_inches': 0.015}
>>> figsizes.icml2022_half(nrows=2, constrained_layout=True, tight_layout=False)
{'figure.figsize': (3.25, 4.017220926874317), 'figure.constrained_layout.use': True, 'figure.autolayout': False}
{'figure.figsize': (3.25, 4.017220926874317), 'figure.constrained_layout.use': True, 'figure.autolayout': False, 'savefig.bbox': 'tight', 'savefig.pad_inches': 0.015}
>>> fontsizes.icml2022()
{'font.size': 8, 'axes.labelsize': 8, 'legend.fontsize': 6, 'xtick.labelsize': 6, 'ytick.labelsize': 6, 'axes.titlesize': 8}
>>> fonts.icml2022()
Expand Down
2 changes: 1 addition & 1 deletion docs/source/getting_started/usage_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ within one module, the functions have a unified interface (wherever possible)
>>>
>>> # The full output:
>>> figsizes.icml2022_full(nrows=4)
{'figure.figsize': (6.75, 8.343458848123582), 'figure.constrained_layout.use': True, 'figure.autolayout': False}
{'figure.figsize': (6.75, 8.343458848123582), 'figure.constrained_layout.use': True, 'figure.autolayout': False, 'savefig.bbox': 'tight', 'savefig.pad_inches': 0.015}

```

Expand Down
36 changes: 35 additions & 1 deletion tueplots/figsizes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

# Some useful constants
_GOLDEN_RATIO = (5.0**0.5 - 1.0) / 2.0
"""Default height-to-width ratio for subplots."""

_POINTS_PER_INCH = 72.27
"""Translate pt to inches and back."""


_PAD_INCHES = 0.015
"""Default whitespace around figures when saving."""


# Double-column formats

Expand Down Expand Up @@ -34,6 +42,7 @@ def _icml2022_and_aistats2022_half(
constrained_layout=True,
tight_layout=False,
height_to_width_ratio=_GOLDEN_RATIO,
pad_inches=_PAD_INCHES,
):
figsize = _from_base_in(
base_width_in=3.25,
Expand All @@ -46,6 +55,7 @@ def _icml2022_and_aistats2022_half(
figsize=figsize,
constrained_layout=constrained_layout,
tight_layout=tight_layout,
pad_inches=pad_inches,
)


Expand All @@ -57,6 +67,7 @@ def _icml2022_and_aistats2022_full(
constrained_layout=True,
tight_layout=False,
height_to_width_ratio=_GOLDEN_RATIO,
pad_inches=_PAD_INCHES,
):
figsize = _from_base_in(
base_width_in=6.75,
Expand All @@ -69,6 +80,7 @@ def _icml2022_and_aistats2022_full(
figsize=figsize,
constrained_layout=constrained_layout,
tight_layout=tight_layout,
pad_inches=pad_inches,
)


Expand All @@ -79,6 +91,7 @@ def cvpr2022_half(
constrained_layout=True,
tight_layout=False,
height_to_width_ratio=_GOLDEN_RATIO,
pad_inches=_PAD_INCHES,
):
"""Double-column (half-width) figures for CVPR 2022."""

Expand All @@ -93,6 +106,7 @@ def cvpr2022_half(
figsize=figsize,
constrained_layout=constrained_layout,
tight_layout=tight_layout,
pad_inches=pad_inches,
)


Expand All @@ -104,6 +118,7 @@ def cvpr2022_full(
constrained_layout=True,
tight_layout=False,
height_to_width_ratio=_GOLDEN_RATIO,
pad_inches=_PAD_INCHES,
):
"""Single-column (full-width) figures for CVPR 2022."""

Expand All @@ -118,6 +133,7 @@ def cvpr2022_full(
figsize=figsize,
constrained_layout=constrained_layout,
tight_layout=tight_layout,
pad_inches=pad_inches,
)


Expand All @@ -132,6 +148,7 @@ def jmlr2001(
constrained_layout=True,
tight_layout=False,
height_to_width_ratio=_GOLDEN_RATIO,
pad_inches=_PAD_INCHES,
):
"""JMLR figure size.

Expand All @@ -151,6 +168,7 @@ def jmlr2001(
figsize=figsize,
constrained_layout=constrained_layout,
tight_layout=tight_layout,
pad_inches=pad_inches,
)


Expand All @@ -162,6 +180,7 @@ def neurips2021(
constrained_layout=True,
tight_layout=False,
height_to_width_ratio=_GOLDEN_RATIO,
pad_inches=_PAD_INCHES,
):
"""Neurips 2021 figure size."""
return _neurips_common(
Expand All @@ -171,6 +190,7 @@ def neurips2021(
constrained_layout=constrained_layout,
tight_layout=tight_layout,
height_to_width_ratio=height_to_width_ratio,
pad_inches=pad_inches,
)


Expand All @@ -182,6 +202,7 @@ def neurips2022(
constrained_layout=True,
tight_layout=False,
height_to_width_ratio=_GOLDEN_RATIO,
pad_inches=_PAD_INCHES,
):
"""Neurips 2022 figure size."""
return _neurips_common(
Expand All @@ -191,6 +212,7 @@ def neurips2022(
constrained_layout=constrained_layout,
tight_layout=tight_layout,
height_to_width_ratio=height_to_width_ratio,
pad_inches=pad_inches,
)


Expand All @@ -202,6 +224,7 @@ def _neurips_common(
constrained_layout=True,
tight_layout=False,
height_to_width_ratio=_GOLDEN_RATIO,
pad_inches=_PAD_INCHES,
):
"""Neurips figure size defaults."""

Expand All @@ -216,6 +239,7 @@ def _neurips_common(
figsize=figsize,
constrained_layout=constrained_layout,
tight_layout=tight_layout,
pad_inches=pad_inches,
)


Expand All @@ -241,6 +265,7 @@ def beamer_169(
rel_height=0.6,
constrained_layout=True,
tight_layout=False,
pad_inches=_PAD_INCHES,
):
"""Beamer figure size for `aspectratio=169`."""
figsize = _from_base_pt(
Expand All @@ -255,12 +280,21 @@ def beamer_169(
figsize=figsize,
constrained_layout=constrained_layout,
tight_layout=tight_layout,
pad_inches=pad_inches,
)


def _figsize_to_output_dict(*, figsize, constrained_layout, tight_layout):
def _figsize_to_output_dict(
*,
figsize,
constrained_layout,
tight_layout,
pad_inches,
):
return {
"figure.figsize": figsize,
"figure.constrained_layout.use": constrained_layout,
"figure.autolayout": tight_layout,
"savefig.bbox": "tight",
"savefig.pad_inches": pad_inches,
}