Skip to content

Commit

Permalink
get_plot_size heuristics moved out of display_hooks and into plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
jlstevens committed Feb 25, 2015
1 parent 81a3c8b commit 5198218
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
28 changes: 5 additions & 23 deletions holoviews/ipython/display_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
NdOverlay, GridSpace, Layout, Overlay
from ..core.traversal import unique_dimkeys, bijective
from ..element import Raster
from ..plotting import LayoutPlot, GridPlot, RasterGridPlot, Plot, ANIMATION_OPTS, opts
from ..plotting import LayoutPlot, GridPlot, RasterGridPlot, Plot, ANIMATION_OPTS, opts, get_plot_size
from .magics import ViewMagic, OptsMagic
from .widgets import IPySelectionWidget, SelectionWidget, ScrubberWidget

Expand All @@ -42,12 +42,6 @@
#==================#


def get_plot_size(size):
factor = size / 100.0
return (Plot.figure_size[0] * factor,
Plot.figure_size[1] * factor)


def animate(anim, dpi, writer, mime_type, anim_kwargs, extra_args, tag):
if extra_args != []:
anim_kwargs = dict(anim_kwargs, extra_args=extra_args)
Expand Down Expand Up @@ -211,7 +205,7 @@ def view_display(view, size, **kwargs):
if magic_info: return magic_info
if view.__class__ not in Store.defaults: return None
fig = Store.defaults[view.__class__](view,
**opts(view, get_plot_size(size)))()
**opts(view, get_plot_size(view, size)))()
return display_figure(fig)


Expand All @@ -222,7 +216,7 @@ def map_display(vmap, size, map_format, max_frames, widget_mode, **kwargs):
if magic_info: return magic_info
if vmap.type not in Store.defaults: return None
mapplot = Store.defaults[vmap.type](vmap,
**opts(vmap.last, get_plot_size(size)))
**opts(vmap.last, get_plot_size(vmap,size)))
if len(mapplot) == 0:
return sanitized_repr(vmap)
elif len(mapplot) > max_frames:
Expand All @@ -246,10 +240,7 @@ def layout_display(layout, size, map_format, max_frames, max_branches, widget_mo
shape = layout.shape
magic_info = process_cell_magics(layout)
if magic_info: return magic_info
grid_size = (shape[1]*get_plot_size(size)[1],
shape[0]*get_plot_size(size)[0])

layoutplot = LayoutPlot(layout, **opts(layout, grid_size))
layoutplot = LayoutPlot(layout, **opts(layout, get_plot_size(layout, size)))
if isinstance(layout, Layout):
if layout._display == 'auto':
branches = len(set([path[0] for path in list(layout.data.keys())]))
Expand All @@ -271,15 +262,6 @@ def layout_display(layout, size, map_format, max_frames, max_branches, widget_mo
@display_hook
def grid_display(grid, size, map_format, max_frames, max_branches, widget_mode, **kwargs):
if not isinstance(grid, GridSpace): return None
max_dim = max(grid.shape)
# Reduce plot size as GridSpace gets larger
shape_factor = 1. / max_dim
# Expand small grids to a sensible viewing size
expand_factor = 1 + (max_dim - 1) * 0.1
scale_factor = expand_factor * shape_factor
grid_size = (scale_factor * grid.shape[0] * get_plot_size(size)[0],
scale_factor * grid.shape[1] * get_plot_size(size)[1])

magic_info = process_cell_magics(grid)
if magic_info: return magic_info

Expand All @@ -289,7 +271,7 @@ def grid_display(grid, size, map_format, max_frames, max_branches, widget_mode,
plot_type = RasterGridPlot
else:
plot_type = GridPlot
gridplot = plot_type(grid, **opts(grid, grid_size))
gridplot = plot_type(grid, **opts(grid, get_plot_size(grid, size)))

if len(gridplot) > max_frames:
max_frame_warning(max_frames)
Expand Down
34 changes: 34 additions & 0 deletions holoviews/plotting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import param

from ..core.options import Cycle, Options, Store
from ..core import Layout, NdLayout, GridSpace
from .annotation import * # pyflakes:ignore (API import)
from .chart import * # pyflakes:ignore (API import)
from .chart3d import * # pyflakes:ignore (API import)
Expand All @@ -16,6 +17,39 @@
def opts(el, size):
"Returns the plot options with supplied size (if not overridden)"
return dict(figure_size=size, **Store.lookup_options(el, 'plot').options)

def get_plot_size(obj, percent_size):
"""
Given a holoviews object and a percentage size, apply heuristics
to compute a suitable figure size. For instance, scaling layouts
and grids linearly can result in unwieldy figure sizes when there
are a large number of elements. As ad hoc heuristics are used,
this functionality is kept separate from the plotting classes
themselves.
Used by the IPython Notebook display hooks and the save
utility. Note that this can be overridden explicitly per object
using the figure_size and size plot options.
"""
def rescale_figure(percent_size):
factor = percent_size / 100.0
return (Plot.figure_size[0] * factor,
Plot.figure_size[1] * factor)

if isinstance(obj, (Layout, NdLayout)):
return (obj.shape[1]*rescale_figure(percent_size)[1],
obj.shape[0]*rescale_figure(percent_size)[0])
elif isinstance(obj, GridSpace):
max_dim = max(obj.shape)
# Reduce plot size as GridSpace gets larger
shape_factor = 1. / max_dim
# Expand small grids to a sensible viewing size
expand_factor = 1 + (max_dim - 1) * 0.1
scale_factor = expand_factor * shape_factor
return (scale_factor * obj.shape[0] * rescale_figure(percent_size)[0],
scale_factor * obj.shape[1] * rescale_figure(percent_size)[1])
else:
return rescale_figure(percent_size)
GIF_TAG = "<center><img src='data:image/gif;base64,{b64}' style='max-width:100%'/><center/>"
VIDEO_TAG = """
<center><video controls style='max-width:100%'>
Expand Down

0 comments on commit 5198218

Please sign in to comment.