Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: restore users custom rcparams (#2410)

Akshay Agrawalc875ee59cc76
diff --git a/marimo/_output/formatters/matplotlib_formatters.py b/marimo/_output/formatters/matplotlib_formatters.py
index dfb2e35d67..f18d89f56c 100644
--- a/marimo/_output/formatters/matplotlib_formatters.py
+++ b/marimo/_output/formatters/matplotlib_formatters.py
@@ -60,6 +60,13 @@ class MatplotlibFormatter(FormatterFactory):
def apply_theme(self, theme: Theme) -> None:
import matplotlib.style # type: ignore
- matplotlib.style.use(
- "dark_background" if theme == "dark" else "default"
- )
+ # Note: we don't set to "default", because that overwrites all
+ # rcParams.
+ #
+ # We also don't try to restore from an rcParams file, because that
+ # may overwrite other rcParams that the user set.
+ #
+ # This means that the style can't be switched from dark to light
+ # without restarting the kernel.
+ if theme == "dark":
+ matplotlib.style.use("dark_background")
diff --git a/tests/_output/formatters/test_matplotlib.py b/tests/_output/formatters/test_matplotlib.py
new file mode 100644
index 0000000000..b903a50150
--- /dev/null
+++ b/tests/_output/formatters/test_matplotlib.py
@@ -0,0 +1,67 @@
+from __future__ import annotations
+
+import pytest
+
+from marimo._dependencies.dependencies import DependencyManager
+from marimo._runtime.runtime import Kernel
+from tests.conftest import ExecReqProvider
+
+HAS_MPL = DependencyManager.matplotlib.has()
+
+
[email protected](not HAS_MPL, reason="optional dependencies not installed")
+async def test_matplotlib_rc_light(
+ executing_kernel: Kernel, exec_req: ExecReqProvider
+) -> None:
+ import matplotlib.pyplot as plt # type: ignore
+
+ from marimo._output.formatters.formatters import register_formatters
+
+ plt.rcParams["font.family"] = ["monospace"]
+
+ register_formatters(theme="light")
+
+ await executing_kernel.run(
+ [
+ exec_req.get(
+ """
+ import matplotlib.pyplot as plt
+
+ rcParams = plt.rcParams
+ """
+ )
+ ]
+ )
+
+ rcParams = executing_kernel.globals["rcParams"]
+ assert rcParams["font.family"] == ["monospace"]
+ assert rcParams["figure.facecolor"] == "white"
+
+
[email protected](not HAS_MPL, reason="optional dependencies not installed")
+async def test_matplotlib_rc_dark(
+ executing_kernel: Kernel, exec_req: ExecReqProvider
+) -> None:
+ import matplotlib.pyplot as plt # type: ignore
+
+ from marimo._output.formatters.formatters import register_formatters
+
+ plt.rcParams["font.family"] = ["monospace"]
+
+ register_formatters(theme="dark")
+
+ await executing_kernel.run(
+ [
+ exec_req.get(
+ """
+ import matplotlib.pyplot as plt
+
+ rcParams = plt.rcParams
+ """
+ )
+ ]
+ )
+
+ rcParams = executing_kernel.globals["rcParams"]
+ assert rcParams["font.family"] == ["monospace"]
+ assert rcParams["figure.facecolor"] == "black"