Skip to content

Commit 4c1fb74

Browse files
committed
Allow specifying grid
1 parent aeb6f51 commit 4c1fb74

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

doc/ref/plotting_options/styling.ipynb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,30 @@
311311
"plot2 = df.hvplot(group_label=\"Company\", grid=True, width=400, title=\"grid=True\")\n",
312312
"(plot1 + plot2).cols(1)"
313313
]
314+
},
315+
{
316+
"cell_type": "markdown",
317+
"id": "56cdf3e2",
318+
"metadata": {},
319+
"source": [
320+
"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'``."
321+
]
322+
},
323+
{
324+
"cell_type": "code",
325+
"execution_count": null,
326+
"id": "7e38dee4",
327+
"metadata": {},
328+
"outputs": [],
329+
"source": [
330+
"import hvplot.pandas # noqa\n",
331+
"\n",
332+
"df = hvplot.sampledata.stocks(\"pandas\", engine_kwargs={\"index_col\" : \"date\"})\n",
333+
"\n",
334+
"plot1 = df.hvplot(group_label=\"Company\", grid='x', width=400, title=\"grid='x'\")\n",
335+
"plot2 = df.hvplot(group_label=\"Company\", grid='y-dashed', width=400, title=\"grid='y-dashed'\")\n",
336+
"(plot1 + plot2).cols(1)"
337+
]
314338
}
315339
],
316340
"metadata": {

hvplot/converter.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,11 @@ class HoloViewsConverter:
475475
fontsize : number or dict or None, default=None
476476
Set title, label and legend text to the same fontsize. Finer control
477477
by using a dict: ``{'title': '15pt', 'ylabel': '5px', 'ticks': 20}``.
478-
grid : bool or None, default=None
479-
Whether to show a grid.
478+
grid : bool, str, or None, default=None
479+
Whether to show a grid. If True, shows grid on both axes.
480+
If ``'x'`` or ``'y'``, shows grid only on the specified axis.
481+
Suffix with ``'dashed'``, ``'dotted'``, ``'dotdash'``, or ``'dashdot'``
482+
to change the grid line style, e.g. ``'x-dashed'``.
480483
481484
Resampling Options
482485
------------------
@@ -1059,7 +1062,16 @@ def __init__(
10591062
plot_opts['logy'] = logy
10601063

10611064
if grid is not None:
1062-
plot_opts['show_grid'] = grid
1065+
if isinstance(grid, str):
1066+
gridstyle = {}
1067+
axis = grid[0]
1068+
other_axis = 'x' if axis == 'y' else 'y'
1069+
if len(grid) > 0:
1070+
line_dash = grid[1:].lstrip('-').lstrip('.').lstrip('_')
1071+
gridstyle[f'{axis}grid_line_dash'] = line_dash
1072+
gridstyle[f'{other_axis}grid_line_alpha'] = 0
1073+
plot_opts['gridstyle'] = gridstyle
1074+
plot_opts['show_grid'] = bool(grid)
10631075

10641076
if legend is not None:
10651077
plot_opts['show_legend'] = bool(legend)

hvplot/tests/testoptions.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,31 @@ def test_legend_opts(self, df, backend):
635635
opts = Store.lookup_options(backend, plot, 'plot')
636636
assert opts.kwargs['legend_opts'] == lo
637637

638+
@pytest.mark.parametrize('grid_bool', [True, False])
639+
def test_grid_boolean(self, df, backend, grid_bool):
640+
plot = df.hvplot('x', 'y', grid=grid_bool)
641+
opts = Store.lookup_options(backend, plot, 'plot')
642+
assert opts.kwargs['grid'] is grid_bool
643+
644+
def test_grid_x(self, df, backend):
645+
plot = df.hvplot('x', 'y', grid='x')
646+
opts = Store.lookup_options(backend, plot, 'plot')
647+
assert opts.kwargs['grid'] is True
648+
assert opts.kwargs['gridstyle'] == {'ygrid_line_alpha': 0}
649+
650+
def test_grid_y(self, df, backend):
651+
plot = df.hvplot('x', 'y', grid='y')
652+
opts = Store.lookup_options(backend, plot, 'plot')
653+
assert opts.kwargs['grid'] is True
654+
assert opts.kwargs['gridstyle'] == {'xgrid_line_alpha': 0}
655+
656+
@pytest.mark.parameterize('grid_str', ['x-dashed', 'xdashed', 'x.dashed', 'x_dashed'])
657+
def test_grid_line_dash(self, df, backend, grid_str):
658+
plot = df.hvplot('x', 'y', grid=grid_str)
659+
opts = Store.lookup_options(backend, plot, 'plot')
660+
assert opts.kwargs['grid'] is True
661+
assert opts.kwargs['gridstyle'] == {'ygrid_line_alpha': 0, 'xgrid_line_dash': 'dashed'}
662+
638663

639664
@pytest.fixture(scope='module')
640665
def da():

0 commit comments

Comments
 (0)