Skip to content

Commit 11aa422

Browse files
committed
Set layout engine in _create_figure() instead of draw()
Only the figure creator should own the layout engine. Moving set_layout_engine into _create_figure() ensures this — the existing hasattr(self, "figure") guard prevents embedded plots/compositions from setting it.
1 parent 4bd2378 commit 11aa422

2 files changed

Lines changed: 4 additions & 5 deletions

File tree

plotnine/composition/_compose.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,13 @@ def _create_figure(self):
466466
import matplotlib.pyplot as plt
467467

468468
from plotnine._mpl.gridspec import p9GridSpec
469+
from plotnine._mpl.layout_manager import PlotnineLayoutEngine
469470

470471
figure = plt.figure()
471472
self._generate_gridspecs(
472473
figure, p9GridSpec(1, 1, figure, nest_into=None)
473474
)
475+
figure.set_layout_engine(PlotnineLayoutEngine(self))
474476

475477
def _generate_gridspecs(self, figure: Figure, container_gs: p9GridSpec):
476478
from plotnine import ggplot
@@ -532,7 +534,6 @@ def draw(self, *, show: bool = False) -> Figure:
532534
:
533535
Matplotlib figure
534536
"""
535-
from .._mpl.layout_manager import PlotnineLayoutEngine
536537

537538
def _draw(cmp):
538539
figure = cmp._setup()
@@ -557,7 +558,6 @@ def _draw(cmp):
557558
self._draw_annotation()
558559
self._draw_composition_background()
559560
self.theme.apply()
560-
figure.set_layout_engine(PlotnineLayoutEngine(self))
561561

562562
return figure
563563

plotnine/ggplot.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,6 @@ def draw(self, *, show: bool = False) -> Figure:
346346
:
347347
Matplotlib figure
348348
"""
349-
from ._mpl.layout_manager import PlotnineLayoutEngine
350-
351349
with plot_context(self, show=show):
352350
figure = self._setup()
353351
self._build()
@@ -373,7 +371,6 @@ def draw(self, *, show: bool = False) -> Figure:
373371

374372
# Artist object theming
375373
self.theme.apply()
376-
figure.set_layout_engine(PlotnineLayoutEngine(self))
377374

378375
return figure
379376

@@ -395,9 +392,11 @@ def _create_figure(self):
395392
import matplotlib.pyplot as plt
396393

397394
from ._mpl.gridspec import p9GridSpec
395+
from ._mpl.layout_manager import PlotnineLayoutEngine
398396

399397
self.figure = plt.figure()
400398
self._gridspec = p9GridSpec(1, 1, self.figure)
399+
self.figure.set_layout_engine(PlotnineLayoutEngine(self))
401400

402401
def _build(self):
403402
"""

0 commit comments

Comments
 (0)