Skip to content

Commit 183fee5

Browse files
committed
add gridstyle
1 parent 854e10b commit 183fee5

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

doc/ref/plotting_options/grid_legend.ipynb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,76 @@
6969
"plot1 + plot2"
7070
]
7171
},
72+
{
73+
"cell_type": "markdown",
74+
"id": "1cee746c",
75+
"metadata": {},
76+
"source": [
77+
"Use `grid=\"x\"` to draw vertical lines or `grid=\"y\"` to draw horizontal lines only."
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": null,
83+
"id": "91cb84f5",
84+
"metadata": {},
85+
"outputs": [],
86+
"source": [
87+
"import hvplot.pandas # noqa\n",
88+
"import hvsampledata\n",
89+
"\n",
90+
"df = hvsampledata.stocks(\"pandas\", engine_kwargs={\"index_col\" : \"date\"})\n",
91+
"\n",
92+
"plot1 = df.hvplot(group_label=\"Company\", grid=\"x\", width=400, title=\"grid='x'\")\n",
93+
"plot2 = df.hvplot(group_label=\"Company\", grid=\"y\", width=400, title=\"grid='y'\")\n",
94+
"\n",
95+
"plot1 + plot2"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"id": "d426fda0",
101+
"metadata": {},
102+
"source": [
103+
"Additionally, `'dashed'`, `'dotted'`, `'dotdash'`, or `'dashdot'` can be suffixed to the `grid` string to achieve a non-solid line."
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": null,
109+
"id": "7b76bff4",
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"import hvplot.pandas # noqa\n",
114+
"import hvsampledata\n",
115+
"\n",
116+
"df = hvsampledata.stocks(\"pandas\", engine_kwargs={\"index_col\" : \"date\"})\n",
117+
"\n",
118+
"solid_plot = df.hvplot(group_label=\"Company\", grid=\"x\", width=400, title=\"grid='x'\")\n",
119+
"for line_style in [\"dashed\", \"dotted\", \"dotdash\", \"dashdot\"]:\n",
120+
" non_solid_plot = df.hvplot(group_label=\"Company\", grid=f\"x_{line_style}\", width=400, title=f\"grid='x{line_style}'\")\n",
121+
" solid_plot += non_solid_plot"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"id": "0cbb3df0",
128+
"metadata": {},
129+
"outputs": [],
130+
"source": [
131+
"import hvplot.pandas # noqa\n",
132+
"import hvsampledata\n",
133+
"\n",
134+
"df = hvsampledata.stocks(\"pandas\", engine_kwargs={\"index_col\" : \"date\"})\n",
135+
"\n",
136+
"plot1 = df.hvplot(group_label=\"Company\", grid=\"x\", width=400, title=\"grid='x'\")\n",
137+
"plot2 = df.hvplot(group_label=\"Company\", grid=\"y\", width=400, title=\"grid='y'\")\n",
138+
"\n",
139+
"plot1 + plot2"
140+
]
141+
},
72142
{
73143
"cell_type": "markdown",
74144
"id": "7c6cc816-2cc3-4af6-94a5-ed04b3bfe3ce",

hvplot/converter.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,16 @@ def __init__(
975975
plot_opts['logy'] = logy
976976

977977
if grid is not None:
978-
plot_opts['show_grid'] = grid
978+
if isinstance(grid, str):
979+
gridstyle = {}
980+
axis = grid[0]
981+
other_axis = 'x' if axis == 'y' else 'y'
982+
if len(grid) > 0:
983+
line_dash = grid[1:].lstrip('-').lstrip('.').lstrip('_')
984+
gridstyle[f'{axis}grid_line_dash'] = line_dash
985+
gridstyle[f'{other_axis}grid_line_alpha'] = 0
986+
plot_opts['gridstyle'] = gridstyle
987+
plot_opts['show_grid'] = bool(grid)
979988

980989
if legend is not None:
981990
plot_opts['show_legend'] = bool(legend)

hvplot/tests/testoptions.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,31 @@ def test_bgcolor(self, df, backend):
528528
opts = Store.lookup_options(backend, plot, 'plot')
529529
assert opts.kwargs['bgcolor'] == 'black'
530530

531+
@pytest.mark.parametrize('grid_bool', [True, False])
532+
def test_grid_boolean(self, df, backend, grid_bool):
533+
plot = df.hvplot('x', 'y', grid=grid_bool)
534+
opts = Store.lookup_options(backend, plot, 'plot')
535+
assert opts.kwargs['grid'] is grid_bool
536+
537+
def test_grid_x(self, df, backend):
538+
plot = df.hvplot('x', 'y', grid='x')
539+
opts = Store.lookup_options(backend, plot, 'plot')
540+
assert opts.kwargs['grid'] is True
541+
assert opts.kwargs['gridstyle'] == {'ygrid_line_alpha': 0}
542+
543+
def test_grid_y(self, df, backend):
544+
plot = df.hvplot('x', 'y', grid='y')
545+
opts = Store.lookup_options(backend, plot, 'plot')
546+
assert opts.kwargs['grid'] is True
547+
assert opts.kwargs['gridstyle'] == {'xgrid_line_alpha': 0}
548+
549+
@pytest.mark.parameterize('grid_str', ['x-dashed', 'xdashed', 'x.dashed', 'x_dashed'])
550+
def test_grid_line_dash(self, df, backend, grid_str):
551+
plot = df.hvplot('x', 'y', grid=grid_str)
552+
opts = Store.lookup_options(backend, plot, 'plot')
553+
assert opts.kwargs['grid'] is True
554+
assert opts.kwargs['gridstyle'] == {'ygrid_line_alpha': 0, 'xgrid_line_dash': 'dashed'}
555+
531556

532557
@pytest.fixture(scope='module')
533558
def da():

0 commit comments

Comments
 (0)