Skip to content

Commit 6e52461

Browse files
committed
BUG: Deepcopy the plot saved as last_plot
plot_context stored the in-progress plot via a shallow copy, so the object returned by last_plot() shared mutable nested state (e.g. the theme) with the plot being built. Building mutated that shared state, leaving last_plot() referencing a half-built plot. As a result, code like last_plot() + labs(subtitle="ab") failed to left-align the title, because the title's alignment had already been altered in the shared state during the previous build. Use deepcopy so last_plot() is an independent, pristine snapshot.
1 parent 0614d19 commit 6e52461

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

plotnine/_utils/context.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from copy import deepcopy
34
from dataclasses import dataclass
45
from typing import TYPE_CHECKING
56

@@ -69,6 +70,7 @@ def __enter__(self) -> Self:
6970
Enclose in matplolib & pandas environments
7071
"""
7172

73+
self._last_plot = deepcopy(self.plot)
7274
self.rc_context.__enter__()
7375
if PANDAS_LT_3:
7476
self.pd_option_context.__enter__()

0 commit comments

Comments
 (0)