Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plot option to transpose layouts #1100

Merged
merged 3 commits into from
Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion holoviews/plotting/bokeh/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def _init_layout(self, layout):
layout_subplots, layouts, paths = {}, {}, {}
for r, c in self.coords:
# Get view at layout position and wrap in AdjointLayout
key, view = layout_items.get((r, c), (None, None))
key, view = layout_items.get((c, r) if self.transpose else (r, c), (None, None))
view = view if isinstance(view, AdjointLayout) else AdjointLayout([view])
layouts[(r, c)] = view
paths[r, c] = key
Expand Down
4 changes: 3 additions & 1 deletion holoviews/plotting/mpl/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ def _compute_gridspec(self, layout):
col_widthratios, row_heightratios = {}, {}
for (r, c) in self.coords:
# Get view at layout position and wrap in AdjointLayout
_, view = layout_items.get((r, c), (None, None))
_, view = layout_items.get((c, r) if self.transpose else (r, c), (None, None))
layout_view = view if isinstance(view, AdjointLayout) else AdjointLayout([view])
layouts[(r, c)] = layout_view

Expand Down Expand Up @@ -876,6 +876,8 @@ def _compute_gridspec(self, layout):
empty = isinstance(obj.main, Empty)
if empty:
obj = AdjointLayout([])
elif self.transpose:
layout_count = (c*self.rows+(r+1))
else:
layout_count += 1
subaxes = [plt.subplot(self.gs[ind], projection=proj)
Expand Down
7 changes: 6 additions & 1 deletion holoviews/plotting/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,11 @@ class GenericLayoutPlot(GenericCompositePlot):
displays the elements in a cartesian grid in scanline order.
"""

transpose = param.Boolean(default=False, doc="""
Whether to transpose the layout when plotting. Switches
from row-based left-to-right and top-to-bottom scanline order
to column-based top-to-bottom and left-to-right order.""")

def __init__(self, layout, **params):
if not isinstance(layout, (NdLayout, Layout)):
raise ValueError("GenericLayoutPlot only accepts Layout objects.")
Expand All @@ -1038,6 +1043,6 @@ def __init__(self, layout, **params):

super(GenericLayoutPlot, self).__init__(layout, **params)
self.subplots = {}
self.rows, self.cols = layout.shape
self.rows, self.cols = layout.shape[::-1] if self.transpose else layout.shape
self.coords = list(product(range(self.rows),
range(self.cols)))
2 changes: 1 addition & 1 deletion holoviews/plotting/plotly/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _init_layout(self, layout):
inserts_cols = []
for r, c in self.coords:
# Get view at layout position and wrap in AdjointLayout
key, view = layout_items.get((r, c), (None, None))
key, view = layout_items.get((c, r) if self.transpose else (r, c), (None, None))
view = view if isinstance(view, AdjointLayout) else AdjointLayout([view])
layouts[(r, c)] = view
paths[r, c] = key
Expand Down
51 changes: 51 additions & 0 deletions tests/testplotinstantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,28 @@ def test_curve_heterogeneous_datetime_types_with_pd_overlay(self):
plot = mpl_renderer.get_plot(curve_dt*curve_dt64*curve_pd)
self.assertEqual(plot.handles['axis'].get_xlim(), (735964.0, 735976.0))

def test_layout_instantiate_subplots(self):
layout = (Curve(range(10)) + Curve(range(10)) + Image(np.random.rand(10,10)) +
Curve(range(10)) + Curve(range(10)))
plot = mpl_renderer.get_plot(layout)
positions = [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3)]
self.assertEqual(sorted(plot.subplots.keys()), positions)
for i, pos in enumerate(positions):
adjoint = plot.subplots[pos]
if 'main' in adjoint.subplots:
self.assertEqual(adjoint.subplots['main'].layout_num, i+1)

def test_layout_instantiate_subplots_transposed(self):
layout = (Curve(range(10)) + Curve(range(10)) + Image(np.random.rand(10,10)) +
Curve(range(10)) + Curve(range(10)))
plot = mpl_renderer.get_plot(layout(plot=dict(transpose=True)))
positions = [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1)]
self.assertEqual(sorted(plot.subplots.keys()), positions)
nums = [1, 5, 2, 6, 3, 7, 4, 8]
for pos, num in zip(positions, nums):
adjoint = plot.subplots[pos]
if 'main' in adjoint.subplots:
self.assertEqual(adjoint.subplots['main'].layout_num, num)


class TestBokehPlotInstantiation(ComparisonTestCase):
Expand Down Expand Up @@ -643,6 +665,21 @@ def test_curve_heterogeneous_datetime_types_with_pd_overlay(self):
self.assertEqual(plot.handles['x_range'].start, np.datetime64(dt.datetime(2016, 1, 1)))
self.assertEqual(plot.handles['x_range'].end, np.datetime64(dt.datetime(2016, 1, 13)))

def test_layout_instantiate_subplots(self):
layout = (Curve(range(10)) + Curve(range(10)) + Image(np.random.rand(10,10)) +
Curve(range(10)) + Curve(range(10)))
plot = bokeh_renderer.get_plot(layout)
positions = [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3)]
self.assertEqual(sorted(plot.subplots.keys()), positions)

def test_layout_instantiate_subplots_transposed(self):
layout = (Curve(range(10)) + Curve(range(10)) + Image(np.random.rand(10,10)) +
Curve(range(10)) + Curve(range(10)))
plot = bokeh_renderer.get_plot(layout(plot=dict(transpose=True)))
positions = [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1)]
self.assertEqual(sorted(plot.subplots.keys()), positions)



class TestPlotlyPlotInstantiation(ComparisonTestCase):

Expand Down Expand Up @@ -725,3 +762,17 @@ def history_callback(x, history=deque(maxlen=10)):
state = plot.state
self.assertEqual(state['data'][0]['x'], np.arange(10))
self.assertEqual(state['data'][0]['y'], np.arange(10, 20))

def test_layout_instantiate_subplots(self):
layout = (Curve(range(10)) + Curve(range(10)) + Image(np.random.rand(10,10)) +
Curve(range(10)) + Curve(range(10)))
plot = plotly_renderer.get_plot(layout)
positions = [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3)]
self.assertEqual(sorted(plot.subplots.keys()), positions)

def test_layout_instantiate_subplots_transposed(self):
layout = (Curve(range(10)) + Curve(range(10)) + Image(np.random.rand(10,10)) +
Curve(range(10)) + Curve(range(10)))
plot = plotly_renderer.get_plot(layout(plot=dict(transpose=True)))
positions = [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1)]
self.assertEqual(sorted(plot.subplots.keys()), positions)