From 67fc88b9d8ef01cdf912edc1b79356c3e043b2ac Mon Sep 17 00:00:00 2001 From: Hassan Kibirige Date: Mon, 8 Jun 2026 16:26:15 +0300 Subject: [PATCH] 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. --- plotnine/_utils/context.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plotnine/_utils/context.py b/plotnine/_utils/context.py index 4b7a34fa0..a330adfdd 100644 --- a/plotnine/_utils/context.py +++ b/plotnine/_utils/context.py @@ -1,6 +1,6 @@ from __future__ import annotations -from copy import copy +from copy import copy, deepcopy from dataclasses import dataclass from typing import TYPE_CHECKING @@ -73,7 +73,7 @@ def __enter__(self) -> Self: Enclose in matplolib & pandas environments """ - self._last_plot = copy(self.plot) + self._last_plot = deepcopy(self.plot) self.rc_context.__enter__() if PANDAS_LT_3: self.pd_option_context.__enter__()