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

ENH: Read quarto fig-width, fig-height, fig-format #801

Merged
merged 2 commits into from
Jun 14, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/changelog.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ title: Changelog
- The `family`, `fontstyle` and `fontweight` parameters of
[](:class:`~plotnine.geom_text`) are now aesthetics ({{< issue 790 >}}).

- plotnine now responds to the `fig-width`, `fig-height` and `fig-format`
settings in the meta section of a quarto document.

### New Features

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

### Bug Fixes

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

## v0.13.6
(2024-05-09)
Expand Down
37 changes: 35 additions & 2 deletions plotnine/options.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

import typing
import os
from typing import TYPE_CHECKING

if typing.TYPE_CHECKING:
if TYPE_CHECKING:
from typing import Any, Literal, Optional, Type

from plotnine import theme
Expand Down Expand Up @@ -105,3 +106,35 @@
old = d[name]
d[name] = value
return old


# Quarto sets environment variables for the figure dpi, size and format
# for the project or document.
#
# https://quarto.org/docs/computations/execution-options.html#figure-options
#
# If we are in quarto, we read those and make them the default values for
# the options.
# Note that, reading the variables and setting them in a context manager
# cannot not work since the option values would be set after the original
# defaults have been used by the theme.
if "QUARTO_FIG_WIDTH" in os.environ:

def _set_options_from_quarto():

Check warning on line 123 in plotnine/options.py

View check run for this annotation

Codecov / codecov/patch

plotnine/options.py#L123

Added line #L123 was not covered by tests
"""
Set options from quarto
"""
global dpi, figure_size, figure_format

dpi = int(os.environ["QUARTO_FIG_DPI"])
figure_size = (

Check warning on line 130 in plotnine/options.py

View check run for this annotation

Codecov / codecov/patch

plotnine/options.py#L129-L130

Added lines #L129 - L130 were not covered by tests
float(os.environ["QUARTO_FIG_WIDTH"]),
float(os.environ["QUARTO_FIG_HEIGHT"]),
)

# quarto verifies the format
# If is retina, it doubles the original dpi and changes the
# format to png
figure_format = os.environ["QUARTO_FIG_FORMAT"] # pyright: ignore

Check warning on line 138 in plotnine/options.py

View check run for this annotation

Codecov / codecov/patch

plotnine/options.py#L138

Added line #L138 was not covered by tests

_set_options_from_quarto()

Check warning on line 140 in plotnine/options.py

View check run for this annotation

Codecov / codecov/patch

plotnine/options.py#L140

Added line #L140 was not covered by tests
14 changes: 12 additions & 2 deletions plotnine/themes/themeable.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,13 @@ class axis_line_x(themeable):

def apply_ax(self, ax: Axes):
super().apply_ax(ax)
properties = self.properties
# MPL has a default zorder of 2.5 for spines
# so layers 3+ would be drawn on top of the spines
if "zorder" not in properties:
properties["zorder"] = 10000
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set(**self.properties)
ax.spines["bottom"].set(**properties)

def blank_ax(self, ax: Axes):
super().blank_ax(ax)
Expand All @@ -916,8 +921,13 @@ class axis_line_y(themeable):

def apply_ax(self, ax: Axes):
super().apply_ax(ax)
properties = self.properties
# MPL has a default zorder of 2.5 for spines
# so layers 3+ would be drawn on top of the spines
if "zorder" not in properties:
properties["zorder"] = 10000
ax.spines["right"].set_visible(False)
ax.spines["left"].set(**self.properties)
ax.spines["left"].set(**properties)

def blank_ax(self, ax: Axes):
super().blank_ax(ax)
Expand Down