Skip to content

Conversation

ahuang11
Copy link
Collaborator

@ahuang11 ahuang11 commented Sep 29, 2025

Enables:
"""
To show grid only on the x-axis, use grid='x'. To show grid only on the y-axis, use grid='y'. To change the grid line style, suffix with 'dashed', 'dotted', 'dotdash', or 'dashdot', e.g. grid='x-dashed'.
"""

image

Closes: #1440

Copy link
Member

@maximlt maximlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it allow to style both the x and y axes at the same time? If not, how about using another approach (e.g. dict from axis to style, list of tuples)?

grid : bool, str, or None, default=None
Whether to show a grid. If True, shows grid on both axes.
If ``'x'`` or ``'y'``, shows grid only on the specified axis.
Suffix with ``'dashed'``, ``'dotted'``, ``'dotdash'``, or ``'dashdot'``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all these options supported by Matplotlib too? I recently looked into what line_dash (bokeh) / linestyle (matplotlib) supported and documented it there https://hvplot.holoviz.org/en/docs/latest/ref/api/manual/hvplot.hvPlot.line.html#line-dash.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all these options supported by Matplotlib too? I recently looked into what line_dash (bokeh) / linestyle (matplotlib) supported and documented it there

Thanks, I totally neglected matplotlib (and plotly); I'll see if there's a way to convert it; if not just the dash or dotted should be sufficient.

Does it allow to style both the x and y axes at the same time? If not, how about using another approach (e.g. dict from axis to style, list of tuples)?

I personally think at that point, people should use plot.opts(gridstyle={...}), else typing the {"line_dash": "dashed"} would be tedious. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I totally neglected matplotlib (and plotly); I'll see if there's a way to convert it; if not just the dash or dotted should be sufficient.

You can ignore Plotly.

Copy link
Collaborator Author

@ahuang11 ahuang11 Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, holoviews mpl does not support gridstyle so I guess I need to implement it in hv first...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh :/ Had a discussion recently on Discord where I said HoloViews isn't a plotting library. That's exactly what I meant :D

I just realized something. When it comes to plot styling options, hvPlot usually acts as a pass-through. One advantage of that is that the keys and their allowed values are common between hvPlot and HoloViews (so it's easier for users to go from one to the other). One disadvantage is that the keys and their values are backend-dependent. As a result, we haven't attempted to build a "unified styling API" in hvPlot, that would work for all the supported backends.

It looks like this is where we are heading in this PR with e.g. grid="x-dashed". Can I have your thoughts on that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, holoviews mpl does not support gridstyle
holoviz/holoviews#6700

It looks like this is where we are heading in this PR with e.g. grid="x-dashed". Can I have your thoughts on that?.

I actually just added a clause for dict support, so all of grid='x', 'grid='x-dashed', grid={'grid_line_color': 'red'} works.

hvPlot usually acts as a pass-through

To me, hvplot is not only a pass-through--(me being a PM for a second) it's a stress-free, direct plotting experience (at least for me), without having to deal with complex dict structures. Every time I have to use legend_opts or gridstyle, I have to pause and remember the valid key value pairs, which is why I like having a string like x-dashed or x.dashed or xdashed.

Copy link
Collaborator Author

@ahuang11 ahuang11 Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a result, we haven't attempted to build a "unified styling API" in hvPlot, that would work for all the supported backends.

Oh and I also don't fully understand the backend_compat, so I dumped directly in the function, but I imagine there could be a better place if you can help me understand the flow of converter.py, maybe in _process_style?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm not sure where to do that to be honest. I think you're pretty much introducing a new pattern with a unified plot API for multiple backends. Probably not _process_style as gridstyle is a plot option in HoloViews. But having that in the __init__.py doesn't feel right either?

Copy link
Member

@maximlt maximlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to clearly answer:

  1. Is styling the grid a common enough thing to expose it in the API?
  2. If so, is the suggested str config API covering a subset that overlaps with the most common usage patterns?
  3. Is the suggested str config API easy to discover and remember? (if not, there's no need for it and we can just rely on good documentation)

Imo:

  1. Yes
  2. I'm not really sure as there are many ways to style a grid (line style, color, width, alpha; full or only x or only x; + minor grids and their styling). I tend to think that setting the line style ranks high (as an easy way to make the grid less prominent, instead of fiddling with color/alpha/width). @ahuang11 shouldn't grid also simply allow 'dotted'/etc. to style the whole grid?
  3. I think it's OK? I'm not sure why - was chosen (x-dashed) instead of other characters but have no strong opinion about it. I also prefer the word-style (dashed over --).

I think we also need to define the scope of the str config API upfront, in case users come and ask for it to be extended (width, color, alpha, etc.).

"id": "59315b89",
"metadata": {},
"source": [
" A dictionary of grid style options may also be supplied, e.g. for bokeh, ``{'grid_line_color': 'red', 'grid_line_alpha': 0.5}``."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update one of the examples to make it a bit more advanced? I'm thinking that one with minor grid lines would be pretty nice.

"cell_type": "markdown",
"id": "59315b89",
"metadata": {},
"source": [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you link to Bokeh's docs? So users have an easy way to check all the options they can set.

axis = grid[0]
other_axis = 'x' if axis == 'y' else 'y'
if len(grid) > 1:
line_dash = grid[1:].lstrip('-').lstrip('.').lstrip('_')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why lstrip('.').lstrip('_')?

grid : bool, str, or None, default=None
Whether to show a grid. If True, shows grid on both axes.
If ``'x'`` or ``'y'``, shows grid only on the specified axis.
Suffix with ``'dashed'``, ``'dotted'``, ``'dotdash'``, or ``'dashdot'``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm not sure where to do that to be honest. I think you're pretty much introducing a new pattern with a unified plot API for multiple backends. Probably not _process_style as gridstyle is a plot option in HoloViews. But having that in the __init__.py doesn't feel right either?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow setting grid="x" / "y"

2 participants