<jupyter_start><jupyter_text>Styling Matplotlib for PanelIn this example we will show how to style Matplotlib charts with Panel for both the `default` and the `dark` theme.![MatplotlibStyle.gif](https://assets.holoviews.org/panel/thumbnails/gallery/styles/matplotlib-styles.gif)  Get or set the themeWhen we use the Fast templates the `theme` can be found in the `session_args`.<jupyter_code>def get_theme():
    return pn.state.session_args.get("theme", [b'default'])[0].decode()
theme=get_theme()
theme<jupyter_output><empty_output><jupyter_text>Select a nice accent colorBelow we create some functionality to *cycle through* a list of nice accent colors. You would probably just set the `accent_color` and `color` for your specific use case.<jupyter_code>nice_accent_colors = [
    ("#00A170", "white"), # Mint
    ("#DAA520", "white"), # Golden Rod
    ("#F08080", "white"), # Light Coral
    ("#4099da", "white"), # Summery Sky
    ("#2F4F4F", "white"), # Dark Slate Grey
    ("#A01346", "white"), # Fast
]
def get_nice_accent_color():
    """Returns the 'next' nice accent color"""
    if not "color_index" in pn.state.cache:
        pn.state.cache["color_index"]=0
    elif pn.state.cache["color_index"]==len(nice_accent_colors)-1:
        pn.state.cache["color_index"]=0
    else:
        pn.state.cache["color_index"]+=1
    return nice_accent_colors[pn.state.cache["color_index"]]
accent_color, color = get_nice_accent_color()
pn.pane.Markdown(f"# Color: {accent_color}", background=accent_color, height=70, margin=0, style={"color": color, "padding": "10px"})<jupyter_output><empty_output><jupyter_text>MatplotlibThere are nearly 30 builtin styles to matplotlib that can be activated with the `plt.style.use` function. The style names are available in the `plt.style.available` list.Let's define a [`Select`](https://panel.holoviz.org/reference/widgets/Select.html) widget to explore the templates.<jupyter_code>style=pn.widgets.Select(options=[style for style in sorted(plt.style.available) if not style.startswith("_")])<jupyter_output><empty_output><jupyter_text>If the theme is `dark` we will use the `dark_background` style as the default value.<jupyter_code>if theme=="dark":
    style.value="dark_background"<jupyter_output><empty_output><jupyter_text>Lets define a plot<jupyter_code>x = np.arange(-2, 8, .1)
y = .1 * x ** 3 - x ** 2 + 3 * x + 2

def get_plot(theme="default", accent_color=accent_color, style=style.value):
    plt.style.use("default") # Resets to default style. Just in case it was styled to something non-default somewhere else in your app
    plt.style.use(style) # changes to the specified style
    fig0 = Figure(figsize=(12, 6))
    ax0 = fig0.subplots()
    ax0.plot(x, y, linewidth=10.0, color=accent_color)
    ax0.set_title(f'Matplotlib Style: {style}');
    plt.style.use("default") # Resets to default style. Do this if you are styling matplotlib in multiple places in your app.
    return fig0<jupyter_output><empty_output><jupyter_text>Lets [bind](https://panel.holoviz.org/user_guide/APIs.htmlreactive-functions) `get_plot` to the `style` widet and lay out the two in a `Column`.<jupyter_code>get_plot=pn.bind(get_plot, style=style)
matplotlib_component = pn.Column(style,pn.panel(get_plot, height=500, sizing_mode="scale_width"))
matplotlib_component.servable()<jupyter_output><empty_output><jupyter_text>For more check out the [Matplotlib style sheets reference](https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html) and the alternative themes [dracula theme](https://draculatheme.com/matplotlib) and [gadfly](https://towardsdatascience.com/a-new-plot-theme-for-matplotlib-gadfly-2cffc745ff84).  Style the app template<jupyter_code>pn.state.template.param.update(accent_base_color=accent_color, header_background=accent_color)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13119457485654668, 'fraction_numerical': 0.017214397496087636, 'mean_word_length': 5.8238434163701065, 'pattern_counts': {'":': 3, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '645107', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1288, 'n_words': 308, 'path': 'examples/gallery/styles/MatplotlibStyle.ipynb', 'repo_name': 'sthagen/holoviz-panel', 'size': '3834'}