Skip to content

Commit

Permalink
ENH: Add limitsize option
Browse files Browse the repository at this point in the history
closes #807
  • Loading branch information
has2k1 committed Jun 24, 2024
1 parent 50daff4 commit 6bf7fe7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
16 changes: 9 additions & 7 deletions doc/changelog.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@ title: Changelog
- [](:class:`~plotnine.ggtitle`) now accepts `title` and `subtitle` as
keyword arguments. ({{<issue 804 >}})

- Gained the option [](:attr:`~plotnine.options.limitsize`) that makes
it possible to display plots of any size.

```python
from plotnine.options import set_option

set_option("figure_format", False)
```

### New Features

- [](:class:`~plotnine.geom_text`) has gained new aesthetics
`fontvariant` and `fontstretch`.

- [](:stat:`~plotnine.stat_density`) has gained a new parameter `bounds`
that you can use remove asymptotic boundary effects that arise from
density estimates on an infinite domain. ({{< issue 796 >}})

### Bug Fixes

- Fix layers 3 and above not to overlap the axis lines if there are any
({{< issue 798 >}}).

- Fix [](:stat:`~plotnine.stat_pointdensity`) to work with facetting. It is
possible aftected other stats. ({{< issue 808 >}})

## v0.13.6
(2024-05-09)

Expand Down
27 changes: 15 additions & 12 deletions plotnine/ggplot.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from __future__ import annotations

import typing
from collections.abc import Sequence
from copy import copy, deepcopy
from io import BytesIO
from itertools import chain
from pathlib import Path
from types import SimpleNamespace as NS
from typing import Any, Dict, Iterable, Optional
from typing import TYPE_CHECKING, Any, Dict, Iterable, Optional, cast
from warnings import warn

from ._utils import (
Expand Down Expand Up @@ -36,7 +35,7 @@
from .scales.scales import Scales
from .themes.theme import theme, theme_get

if typing.TYPE_CHECKING:
if TYPE_CHECKING:
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from typing_extensions import Self
Expand Down Expand Up @@ -551,7 +550,7 @@ def save_helper(
height: Optional[float] = None,
units: str = "in",
dpi: Optional[float] = None,
limitsize: bool = True,
limitsize: bool | None = None,
verbose: bool = True,
**kwargs: Any,
) -> mpl_save_view:
Expand All @@ -565,6 +564,9 @@ def save_helper(
"""
fig_kwargs: Dict[str, Any] = {"format": format, **kwargs}

if limitsize is None:
limitsize = cast(bool, get_option("limitsize"))

# filename, depends on the object
if filename is None:
ext = format if format else "pdf"
Expand All @@ -587,10 +589,10 @@ def save_helper(
width is not None and height is None
):
raise PlotnineError("You must specify both width and height")

width, height = self.theme.getp("figure_size")
assert width is not None
assert height is not None
else:
width, height = cast(
tuple[float, float], self.theme.getp("figure_size")
)

if limitsize and (width > 25 or height > 25):
raise PlotnineError(
Expand Down Expand Up @@ -621,7 +623,7 @@ def save(
height: Optional[float] = None,
units: str = "in",
dpi: Optional[int] = None,
limitsize: bool = True,
limitsize: bool | None = None,
verbose: bool = True,
**kwargs: Any,
):
Expand Down Expand Up @@ -652,9 +654,10 @@ def save(
DPI to use for raster graphics. If None, defaults to using
the `dpi` of theme, if none is set then a `dpi` of 100.
limitsize :
If `True` (the default), ggsave will not save images
larger than 50x50 inches, to prevent the common error
of specifying dimensions in pixels.
If `True` (the default), save will not save images
larger than 25x25 inches, to prevent the common error
of specifying dimensions in pixels. The default value
is from the option `plotine.options.limitsize`.
verbose :
If `True`, print the saving information.
kwargs :
Expand Down
9 changes: 8 additions & 1 deletion plotnine/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

figure_size: tuple[float, float] = (640 / dpi, 480 / dpi)
"""
Default figure size inches
Default figure size in inches
"""

figure_format: Optional[FigureFormat] = None
Expand All @@ -60,6 +60,13 @@
is used by some themes to determine other margins
"""

limitsize: bool = True
"""
If `True` (the default), images larger than 25x25 inches will not
be drawn or saved. This is to prevent the common error of specifying
dimensions in pixels.
"""


def get_option(name: str) -> Any:
"""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_ggsave.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from plotnine.data import mtcars
from plotnine.exceptions import PlotnineError, PlotnineWarning
from plotnine.options import set_option

p = ggplot(mtcars, aes(x="wt", y="mpg", label="name")) + geom_text()

Expand Down Expand Up @@ -92,6 +93,13 @@ def test_save_big(self):
p.save(fn, width=26, height=26, limitsize=False, verbose=False)
assert_exist_and_clean(fn, "big height and width")

# Using the global option
fn = next(filename_gen)
set_option("limitsize", False)
p.save(fn, width=26, height=26, verbose=False)
set_option("limitsize", True)
assert_exist_and_clean(fn, "big height and width")

def test_dpi_theme_xkcd(self):
fn1 = next(filename_gen)
fn2 = next(filename_gen)
Expand Down

0 comments on commit 6bf7fe7

Please sign in to comment.