Open MarcSkovMadsen opened 3 years ago
I don't get the "seaborn-dark" theme applied in the below.
import panel as pn
import numpy as np
from matplotlib.figure import Figure
from matplotlib import cm
from matplotlib.backends.backend_agg import FigureCanvas # not needed for mpl >= 3.1
import matplotlib.pyplot as plt
pn.extension(sizing_mode="stretch_width")
MPL_CMAPS = {
"Autumn": cm.autumn,
'Spring': cm.spring,
'Summer': cm.summer,
'Winter': cm.winter,
}
MPL_THEME = {
pn.template.DefaultTheme: "default",
pn.template.DarkTheme: "seaborn-dark",
}
template=pn.template.FastListTemplate(
title="Matplotlib Theming",
main_max_width="80%",
)
plt.style.use("default") # reset
mpl_theme=MPL_THEME[template.theme]
print(template.theme, mpl_theme)
plt.style.use(mpl_theme)
def get_plot(cmap="autumn"):
Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U*U + V*V)
fig0 = Figure(figsize=(20, 12))
ax0 = fig0.subplots()
# FigureCanvas(fig0) # not needed for mpl >= 3.1
strm = ax0.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=MPL_CMAPS[cmap])
fig0.colorbar(strm.lines)
return fig0
select = pn.widgets.Select(name="Color Map", options=list(MPL_CMAPS.keys()))
get_plot_interactive=pn.bind(get_plot, cmap=select)
component=pn.Column(
select,
pn.panel(get_plot_interactive, sizing_mode="stretch_both", loading_indicator=True),
sizing_mode="stretch_both"
)
template.main.append(component)
template.servable()
This one seems to work. It's just the seaborn-dark
that does not work apparently.
import panel as pn
import numpy as np
from matplotlib.figure import Figure
from matplotlib import cm
from matplotlib.backends.backend_agg import FigureCanvas # not needed for mpl >= 3.1
import matplotlib.pyplot as plt
pn.extension(sizing_mode="stretch_width")
MPL_CMAPS = {
"Autumn": cm.autumn,
'Spring': cm.spring,
'Summer': cm.summer,
'Winter': cm.winter,
}
MPL_THEME = {
pn.template.DefaultTheme: "default",
pn.template.DarkTheme: "dark_background",
}
template=pn.template.FastListTemplate(
title="Matplotlib Theming",
main_max_width="80%",
)
plt.style.use("default") # reset
plt.style.use(MPL_THEME[template.theme])
def get_plot(cmap="autumn"):
Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U*U + V*V)
fig0 = Figure(figsize=(20, 12))
ax0 = fig0.subplots()
# FigureCanvas(fig0) # not needed for mpl >= 3.1
strm = ax0.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=MPL_CMAPS[cmap])
fig0.colorbar(strm.lines)
return fig0
select = pn.widgets.Select(name="Color Map", options=list(MPL_CMAPS.keys()))
get_plot_interactive=pn.bind(get_plot, cmap=select)
component=pn.Column(
select,
pn.panel(get_plot_interactive, sizing_mode="stretch_both", loading_indicator=True),
sizing_mode="stretch_both"
)
template.main.append(component)
template.servable()
Even though I can get something working I think the proposed solution would still be quite valuable.
Just looked at this and unfortunately there doesn't seem to be a nice way to do this because of the way Matplotlib theming works. Matplotlib themes work by setting the rcParams, which then determine the defaults during figure and artist creation, once created it is not easily possible to apply a new theme. So to be able to set and unset the theme we would have use context managers around the user code or manually, record, set and restore the rcParams.
The only way I can possibly see this working is:
While possible that's pretty awful to implement.
I would like it to be easy to use Matplotlib with Panel. One of the things that is not easy (to me) is theming.
I've tried to apply the steps from the Matplotlib Customizing documentation. I seem to be able to change the theme. But only once. If I try to change the theme multiple times (for example if toggling default/ dark theme of Fast template) it does not change again.
I cannot find anything about theming in the Panel Matplotlib Reference Guide to help me.
Thus its hard to use Matplotlib with a dark themed template.
Solution
style
ortheme
parameter to the Matplotlib pane. if specified this should be applied to the pane.style
ortheme
is specified but the Theme of the Template is "dark" then apply a dark theme for Matplotlib.