Closed YDX-2147483647 closed 1 month ago
Thanks for reporting. It's not by design. We can look into this. If you're familiar with matplotlib and think you can help (that would be appreciated!) you can look at our matplotlib formatting code here: https://github.com/marimo-team/marimo/blob/3f6fcb4cc3571680a6de8e432c87362c5fbf97d5/marimo/_output/formatters/matplotlib_formatters.py. If not, no worries
Wow, thanks for showing the way.
I add if key == 'mathtext.fontset': print(f"{key} = {val}")
in RcParams._set(self, key, val)
https://github.com/matplotlib/matplotlib/blob/a254b687df97cda8c6affa37a1dfcf213f8e6c3a/lib/matplotlib/__init__.py#L692, and find something weird: rcParams
is set three times! And marimo somehow changes the order…
def foo():
print("Outside:")
from matplotlib import rcParams, matplotlib_fname
foo()
import marimo
app = marimo.App()
@app.cell
def __():
print("Before import?")
from matplotlib import rcParams, matplotlib_fname
print("In app:")
app.run()
Outside:
mathtext.fontset = dejavusans
mathtext.fontset = cm
mathtext.fontset = cm
In app:
mathtext.fontset = cm
mathtext.fontset = cm
mathtext.fontset = dejavusans
Before import?
Update: The first two are set in register(self)
https://github.com/marimo-team/marimo/blob/3f6fcb4cc3571680a6de8e432c87362c5fbf97d5/marimo/_output/formatters/matplotlib_formatters.py#L14
and the third in apply_theme(self, theme: Theme)
.
https://github.com/marimo-team/marimo/blob/3f6fcb4cc3571680a6de8e432c87362c5fbf97d5/marimo/_output/formatters/matplotlib_formatters.py#L63-L65
Thank you for investigating! So it looks like apply_theme
, specifically matplotlib.style.use
, is causing the rcParams to be reset?
matplotlib.style.use("dark_background")
is OK, but matplotlib.style.use("default")
overrides the rc file. https://github.com/matplotlib/matplotlib/blob/a254b687df97cda8c6affa37a1dfcf213f8e6c3a/lib/matplotlib/style/core.py#L112-L120
Change apply_theme(self, theme: Theme)
to the following would fix the issue.
if theme == "dark":
matplotlib.style.use("dark_background")
But if apply_theme
multiple times, first dark then light, it won't change.
Awesome, thank you so much for looking into this! I will make a PR with a fix.
Wait! If apply_theme
is called multiple times, the “fix” might break things.
if theme == "dark":
matplotlib.style.use("dark_background")
else:
matplotlib.style.use("default")
matplotlib.style.use(matplotlib_fname()) # Apply rc file given by the user
This might work, but not tested yet. It's really late in my timezone…
Thanks! I can test. Or, if you'd like to make the PR yourself (you're more than welcome to!), I can hold off and leave this for you. Either works 🙂
It’s nice of you, please go ahead and get it fixed. It looks like that you are more familiar with the code base, and therefore more capable of it.
Describe the bug
Matplotlib uses
matplotlibrc
configuration files to customize properties. For example, you can addmathtext.fontset: cm
into the filematplotlibrc
, and all math texts in plots will now in Computer Modern.However, if you matplotlib in a marimo cell, the rc file takes no effect.
Environment
Matplotlib version: 3.9.2
Code to reproduce
matplotlibrc
:test.py
:python test.py
:Question
Is it by design for reproducibility? I have found nothing in the doc or discord…
Workaround
Set
rcParams["mathtext.fontset"] = 'cm'
dynamically in the cell.Runtime rc settings — Customizing Matplotlib with style sheets and rcParams — Matplotlib 3.9.2 documentation