Skip to content

Commit

Permalink
Making things faster
Browse files Browse the repository at this point in the history
  • Loading branch information
jsignell authored and jbednar committed Apr 3, 2019
1 parent 556edb6 commit 2258f5e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
44 changes: 31 additions & 13 deletions colorcet/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,52 @@ def swatch(name, cmap=None, bounds=None, array=array, **kwargs):
plot = hv.Image(array, bounds=bounds, group=title)
backends = hv.Store.loaded_backends()
if 'bokeh' in backends:
plot.opts(opts.Image(backend='bokeh', width=900, height=100, toolbar='above',
width = kwargs.pop('width', 900)
height = kwargs.pop('height', 100)
plot.opts(opts.Image(backend='bokeh', width=width, height=height, toolbar='above',
default_tools=['xwheel_zoom', 'xpan', 'save', 'reset'],
cmap=cmap or palette[name]))
if 'matplotlib' in backends:
plot.opts(opts.Image(backend='matplotlib', aspect=15, fig_size=350,
aspect = kwargs.pop('aspect', 15)
fig_size = kwargs.pop('fig_size', 350)
plot.opts(opts.Image(backend='matplotlib', aspect=aspect, fig_size=fig_size,
cmap=cmap or cm[name]))
return plot.opts(opts.Image(xaxis=None, yaxis=None), opts.Image(**kwargs))

def swatches(*args, group=None, not_group=None, only_aliased=False, cols=1, **kwargs):
def swatches(*args, group=None, not_group=None, only_aliased=False, cols=None, **kwargs):
"""Show swatches for given names or names in group"""
args = args or all_original_names(group=group, not_group=not_group,
only_aliased=only_aliased)
plot = hv.Layout([
swatch(arg, **kwargs) if isinstance(arg, str) else
swatch(*arg, **kwargs) for
arg in args]).cols(cols)
if not cols:
cols = 3 if len(args) >= 3 else 1

backends = hv.Store.loaded_backends()
if 'matplotlib' in backends:
if 'aspect' not in kwargs:
kwargs['aspect'] = 12 // cols
if 'fig_size' not in kwargs:
kwargs['fig_size'] = 500 // cols
if 'bokeh' in backends:
if 'height' not in kwargs:
kwargs['height'] = 100
if 'width' not in kwargs:
kwargs['width'] = (9 * kwargs['height']) // 3

plot = hv.Layout([
swatch(arg, **kwargs) if isinstance(arg, str) else
swatch(*arg, **kwargs) for
arg in args]).cols(cols)

if 'matplotlib' in backends:
plot.opts(opts.Layout(backend='matplotlib', sublabel_format=None,
fig_size=kwargs.get('fig_size', 350)))
fig_size=kwargs.get('fig_size', 150)))
return plot

sine = sineramp()

def sine_comb(name, cmap=None, **kwargs):
"""Show sine_comb using matplotlib or bokeh via holoviews"""
title = name if cmap else get_aliases(name)

sine = sineramp()
plot = hv.Image(sine, group=title)

backends = hv.Store.loaded_backends()
Expand All @@ -67,9 +85,9 @@ def sine_combs(*args, group=None, not_group=None, only_aliased=False, cols=1, **
args = args or all_original_names(group=group, not_group=not_group,
only_aliased=only_aliased)
plot = hv.Layout([
sine_comb(arg, **kwargs) if isinstance(arg, str) else
sine_comb(*arg, **kwargs) for
arg in args]).cols(cols)
sine_comb(arg, **kwargs) if isinstance(arg, str) else
sine_comb(*arg, **kwargs) for
arg in args]).cols(cols)

backends = hv.Store.loaded_backends()
if 'matplotlib' in backends:
Expand Down
Binary file modified examples/assets/images/named.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/assets/write_named.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

diverging_col = pn.Column('#Diverging', sine_combs(*diverging_n, width=400, height=150).opts(toolbar=None))
linear_col = pn.Column('#Linear', sine_combs(*linear_n, width=400, height=150).opts(toolbar=None))
cat_col = pn.Column('#Categorical', swatches(*cat_n, width=400, height=150).opts(toolbar=None))
cat_col = pn.Column('#Categorical', swatches(*cat_n, width=400, height=150, cols=1).opts(toolbar=None))
misc_col = pn.Column('#Misc', sine_combs(*misc_n, width=400, height=150).opts(toolbar=None))

all_named = pn.Row(
Expand Down
12 changes: 5 additions & 7 deletions examples/user_guide/Continuous.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@
"metadata": {},
"outputs": [],
"source": [
"plot_kwargs = dict(cols=3, aspect=4, fig_size=150)\n",
"\n",
"swatches(only_aliased=True, not_group='glasbey', **plot_kwargs)"
"swatches(only_aliased=True, not_group='glasbey')"
]
},
{
Expand All @@ -66,7 +64,7 @@
"metadata": {},
"outputs": [],
"source": [
"swatches(group=\"linear\", **plot_kwargs)"
"swatches(group=\"linear\")"
]
},
{
Expand All @@ -82,7 +80,7 @@
"metadata": {},
"outputs": [],
"source": [
"swatches(group='diverging', **plot_kwargs)"
"swatches(group='diverging')"
]
},
{
Expand All @@ -98,7 +96,7 @@
"metadata": {},
"outputs": [],
"source": [
"swatches(group='nopic', **plot_kwargs)"
"swatches(group='nopic')"
]
},
{
Expand All @@ -119,7 +117,7 @@
"outputs": [],
"source": [
"misc = [name for name in cc.all_original_names() if \"cyclic\" in name or \"isoluminant\" in name or \"rainbow\" in name]\n",
"swatches(*misc, **plot_kwargs)"
"swatches(*misc)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/user_guide/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"source": [
"from colorcet.plotting import swatch, swatches\n",
"import holoviews as hv\n",
"hv.extension('bokeh')"
"hv.extension('matplotlib')"
]
},
{
Expand Down

0 comments on commit 2258f5e

Please sign in to comment.