Skip to content

Make it easy to set global opts defaults #6767

@MarcSkovMadsen

Description

@MarcSkovMadsen

I wanted to document to the LLM in holoviz-mcp how to change the defaults to create publication ready plots.

I couldn't find a description anywhere for changing global defaults. What I could find is descriptions for specific plot types like opts.Bars.

You can also see a user asking in https://discourse.holoviz.org/t/set-default-plot-size-for-all-plot-types/5722. And even core developers struggle providing a uniform, non-hacky answer.

You can see the ways I found below. The most obvious one (method_1) is not working and raising an error though.

Please

  • Explain to me here what the recommended way of updating global defaults is.
  • Make it easier for users by either
    • Adding documentation describing how-to update global defaults
    • Optionally implementing functionality to make this easier.
"""Reference publication quality hvplot code following best practices."""

import hvsampledata
import hvplot.pandas  # noqa: F401
import panel as pn
from holoviews.plotting.bokeh import ElementPlot
from bokeh.models.formatters import NumeralTickFormatter
import holoviews as hv

def method_1():
    """Will raise exception: Exception: opts.defaults only accepts "backend" keyword argument"""
    hv.opts.defaults(
        autohide_toolbar=True,
        yformatter = NumeralTickFormatter(format='0a')
    )

def method_2():
    """Works. No autocompletion or tooltips. No good explanation why have to split across multiple calls."""
    hv.opts.defaults(autohide_toolbar=True,)
    hv.opts.defaults(yformatter = NumeralTickFormatter(format='0a'))

def method_3():
    """Works. Autocompletion or tooltips."""
    ElementPlot.autohide_toolbar = True
    ElementPlot.yformatter = NumeralTickFormatter(format='0a')

def method_4():
    """Works. No Autocompletion or tooltips."""
    ElementPlot.param.autohide_toolbar.default = True
    ElementPlot.param.yformatter.default = NumeralTickFormatter(format='0a')
    

def method_5():
    """Works. No autocompletion or tooltips."""
    ElementPlot.param.update(
        autohide_toolbar=True,
        yformatter = NumeralTickFormatter(format='0a')
    )

method_3() # Makes most sense to me.

# Extract
data = hvsampledata.earthquakes("pandas")

# Transform: Group by mag_class and count occurrences
mag_class_counts = data.groupby('mag_class').size().reset_index(name='counts')

# Plot: Use hvplot
plot = mag_class_counts.hvplot.bar(
    # DO define what to show on x axes
    x='mag_class',
    # DO define what to show on y axes. Can be single element or list for multiple
    y='counts',
    # DO add an explicit title
)
if pn.state.served: # Only run when served with `panel serve`
    pn.extension()

    pn.panel(plot, sizing_mode="stretch_both").servable()

Metadata

Metadata

Assignees

No one assigned

    Labels

    TRIAGENeeds triaging

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions