Skip to content

Commit

Permalink
Provide full control over bokeh grid styles (#2488)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored and jlstevens committed Mar 28, 2018
1 parent 8967f81 commit 7b6a401
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
35 changes: 35 additions & 0 deletions examples/user_guide/Plotting_with_Bokeh.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,41 @@
"errorbars * overlay * hv.Curve(errorbars) * global_mean"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Grid lines"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Grid lines can be controlled throught the combination of ``show_grid`` and ``gridstyle`` parameters. The ``gridstyle`` allows specifying a number of options including:\n",
"\n",
"* ``grid_line_color``\n",
"* ``grid_line_alpha``\n",
"* ``grid_line_dash``\n",
"* ``grid_line_width``\n",
"* ``grid_bounds``\n",
"* ``grid_band``\n",
"\n",
"These options may also be applied to minor grid lines by prepending the ``'minor_'`` prefix and may be applied to a specific axis by replacing ``'grid_`` with ``'xgrid_'`` or ``'ygrid_'``. Here we combine some of these options to generate a complex grid pattern:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grid_style = {'grid_line_color': 'black', 'grid_line_width': 1.5, 'ygrid_bounds': (0.3, 0.7),\n",
" 'minor_xgrid_line_color': 'lightgray', 'xgrid_line_dash': [4, 4]}\n",
"\n",
"hv.Points(np.random.rand(10, 2)).options(gridstyle=grid_style, show_grid=True, size=5, width=600)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
17 changes: 17 additions & 0 deletions holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class ElementPlot(BokehPlot, GenericElementPlot):
{'ticks': '20pt', 'title': '15pt', 'ylabel': '5px', 'xlabel': '5px'}""")

gridstyle = param.Dict(default={}, doc="""
Allows customizing the grid style, e.g. grid_line_color defines
the line color for both grids while xgrid_line_color exclusively
customizes the x-axis grid lines.""")

labelled = param.List(default=['x', 'y'], doc="""
Whether to plot the 'x' and 'y' labels.""")

Expand Down Expand Up @@ -457,6 +462,18 @@ def _update_plot(self, key, plot, element=None):
if not self.show_grid:
plot.xgrid.grid_line_color = None
plot.ygrid.grid_line_color = None
else:
replace = ['bounds', 'bands']
style_items = list(self.gridstyle.items())
both = {k: v for k, v in style_items if k.startswith('grid_') or k.startswith('minor_grid')}
xgrid = {k.replace('xgrid', 'grid'): v for k, v in style_items if 'xgrid' in k}
ygrid = {k.replace('ygrid', 'grid'): v for k, v in style_items if 'ygrid' in k}
xopts = {k.replace('grid_', '') if any(r in k for r in replace) else k: v
for k, v in dict(both, **xgrid).items()}
yopts = {k.replace('grid_', '') if any(r in k for r in replace) else k: v
for k, v in dict(both, **ygrid).items()}
plot.xgrid[0].update(**xopts)
plot.ygrid[0].update(**yopts)


def _update_ranges(self, element, ranges):
Expand Down
12 changes: 12 additions & 0 deletions tests/plotting/bokeh/testelementplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ def formatter(x):
plot = bokeh_renderer.get_plot(curve).state
self.assertIsInstance(plot.yaxis[0].formatter, FuncTickFormatter)

def test_element_grid_options(self):
grid_style = {'grid_line_color': 'blue', 'grid_line_width': 1.5, 'ygrid_bounds': (0.3, 0.7),
'minor_xgrid_line_color': 'lightgray', 'xgrid_line_dash': [4, 4]}
curve = Curve(range(10)).options(show_grid=True, gridstyle=grid_style)
plot = bokeh_renderer.get_plot(curve)
self.assertEqual(plot.state.xgrid[0].grid_line_color, 'blue')
self.assertEqual(plot.state.xgrid[0].grid_line_width, 1.5)
self.assertEqual(plot.state.xgrid[0].grid_line_dash, [4, 4])
self.assertEqual(plot.state.xgrid[0].minor_grid_line_color, 'lightgray')
self.assertEqual(plot.state.ygrid[0].grid_line_color, 'blue')
self.assertEqual(plot.state.ygrid[0].grid_line_width, 1.5)
self.assertEqual(plot.state.ygrid[0].bounds, (0.3, 0.7))

0 comments on commit 7b6a401

Please sign in to comment.