Skip to content

Commit

Permalink
Ensure HoloViews plot ranges are merged correctly (#1495)
Browse files Browse the repository at this point in the history
* Ensure HoloViews plot ranges are merged correctly

* Add test

* Fixed matplotlib bug

* Fixed test
  • Loading branch information
philippjfr committed Sep 17, 2020
1 parent 5635060 commit 6329b07
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
git:
depth: 100

language: generic
language: python

os:
- linux
Expand All @@ -18,7 +18,6 @@ addons:

env:
global:
- PYENV_VERSION=3.7
- CHANS_DEV="-c pyviz/label/dev -c bokeh/label/dev -c conda-forge"
- CHANS_REL="-c pyviz -c bokeh -c conda-forge"
- LABELS_DEV="--label dev"
Expand Down
14 changes: 14 additions & 0 deletions examples/reference/panes/Matplotlib.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@
"metadata": {},
"outputs": [],
"source": [
"fig = Figure(figsize=(8, 6))\n",
"ax = fig.add_subplot(111)\n",
"\n",
"dx, dy = 0.05, 0.05\n",
"\n",
"# generate 2 2d grids for the x & y bounds\n",
"y, x = np.mgrid[slice(1, 5 + dy, dy),\n",
" slice(1, 5 + dx, dx)]\n",
"\n",
"z = np.sin(x)**10 + np.cos(10 + y*x) * np.cos(x)\n",
"\n",
"cf = ax.contourf(x + dx/2., y + dy/2., z)\n",
"fig.colorbar(cf, ax=ax)\n",
"\n",
"pn.pane.Matplotlib(fig, interactive=True, dpi=72)"
]
},
Expand Down
17 changes: 12 additions & 5 deletions panel/pane/holoviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import param

from bokeh.models import Spacer as _BkSpacer
from bokeh.models import Spacer as _BkSpacer, Range1d

from ..io import state, unlocked
from ..layout import Column, WidgetBox, HSpacer, VSpacer, Row
Expand Down Expand Up @@ -276,12 +276,12 @@ def _render(self, doc, comm, root):
if backend == 'bokeh' and mode != renderer.mode:
renderer = renderer.instance(mode=mode)

kwargs = {'margin': self.margin}
if backend == 'bokeh' or LooseVersion(str(hv.__version__)) >= str('1.13.0'):
kwargs = {'doc': doc, 'root': root}
kwargs['doc'] = doc
kwargs['root'] = root
if comm:
kwargs['comm'] = comm
else:
kwargs = {}

return renderer.get_plot(self.object, **kwargs)

Expand Down Expand Up @@ -520,7 +520,7 @@ def link_axes(root_view, root_model):
return

from holoviews.core.options import Store
from holoviews.core.util import unique_iterator
from holoviews.core.util import unique_iterator, max_range
from holoviews.plotting.bokeh.element import ElementPlot

ref = root_model.ref['id']
Expand Down Expand Up @@ -548,6 +548,13 @@ def link_axes(root_view, root_model):

for (tag), axes in range_map.items():
fig, p, ax, axis = axes[0]
if isinstance(axis, Range1d):
start, end = max_range([
(ax[-1].start, ax[-1].end) for ax in axes
if isinstance(ax[-1], Range1d)
])
axis.start = start
axis.end = end
for fig, p, pax, _ in axes[1:]:
changed = []
if type(ax) is not type(pax):
Expand Down
2 changes: 2 additions & 0 deletions panel/pane/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class Matplotlib(PNG, IPyWidget):
Automatically adjust the figure size to fit the
subplots and other artist elements.""")

_rename = {'object': 'text', 'interactive': None, 'dpi': None, 'tight': None}

_rerender_params = PNG._rerender_params + ['object', 'dpi', 'tight']

@classmethod
Expand Down
17 changes: 17 additions & 0 deletions panel/tests/pane/test_holoviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,23 @@ def test_holoviews_linked_axes(document, comm):
assert p1.y_range is p2.y_range


@hv_available
def test_holoviews_linked_axes_merged_ranges(document, comm):
c1 = hv.Curve([1, 2, 3])
c2 = hv.Curve([0, 1, 2, 3, 4])

layout = Row(HoloViews(c1, backend='bokeh'), HoloViews(c2, backend='bokeh'))

row_model = layout.get_root(document, comm=comm)

p1, p2 = row_model.select({'type': Figure})

assert p1.x_range is p2.x_range
assert p1.y_range is p2.y_range
assert p1.y_range.start == -0.4
assert p1.y_range.end == 4.4


@hv_available
def test_holoviews_linked_x_axis(document, comm):
c1 = hv.Curve([1, 2, 3])
Expand Down

0 comments on commit 6329b07

Please sign in to comment.