From c3678ec2b2a3398392362a5ff9df5cde00478c8d Mon Sep 17 00:00:00 2001 From: Hassan Kibirige Date: Thu, 13 Jun 2024 22:21:06 +0300 Subject: [PATCH 1/2] Fix layers 3 & above overlapping the axis lines fixes #798 --- doc/changelog.qmd | 4 ++++ plotnine/themes/themeable.py | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/doc/changelog.qmd b/doc/changelog.qmd index 1b6869cb3..11cde42a7 100644 --- a/doc/changelog.qmd +++ b/doc/changelog.qmd @@ -15,6 +15,10 @@ title: Changelog - [](: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) diff --git a/plotnine/themes/themeable.py b/plotnine/themes/themeable.py index 92cf8d52b..f222a00cf 100644 --- a/plotnine/themes/themeable.py +++ b/plotnine/themes/themeable.py @@ -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) @@ -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) From a3d893f7c30e654ea5a249c83c77ea532f23380f Mon Sep 17 00:00:00 2001 From: Hassan Kibirige Date: Wed, 12 Jun 2024 19:57:29 +0300 Subject: [PATCH 2/2] ENH: Read quarto fig-width, fig-height, fig-format --- doc/changelog.qmd | 3 +++ plotnine/options.py | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/doc/changelog.qmd b/doc/changelog.qmd index 11cde42a7..88198c4a4 100644 --- a/doc/changelog.qmd +++ b/doc/changelog.qmd @@ -10,6 +10,9 @@ 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 diff --git a/plotnine/options.py b/plotnine/options.py index df7b1fd0f..bc39b7b8d 100644 --- a/plotnine/options.py +++ b/plotnine/options.py @@ -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 @@ -105,3 +106,35 @@ def set_option(name: str, value: Any) -> Any: 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(): + """ + Set options from quarto + """ + global dpi, figure_size, figure_format + + dpi = int(os.environ["QUARTO_FIG_DPI"]) + figure_size = ( + 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 + + _set_options_from_quarto()