Skip to content

Commit

Permalink
Support Pixel Ratio (#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipc2 committed Sep 20, 2024
1 parent bd3e590 commit 266565b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion doc/user_guide/Customization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@
" dynspread (default=False):\n",
" Allows plots generated with datashade=True to increase the point\n",
" size to make sparse regions more visible\n",
" pixel_ratio (default=None):\n",
" Pixel ratio applied to the height and width, used when rasterizing or\n",
" datashading. When not set explicitly, the ratio is automatically\n",
" obtained from the browser device pixel ratio. Default is 1 when\n",
" the browser information is not available. Useful when the browser\n",
" information is not available (pixel_ratio=2 can give better results on\n",
" Retina displays) or for using lower resolution for speed.\n",
" rasterize (default=False):\n",
" Whether to apply rasterization using the datashader library\n",
" returning an aggregated Image\n",
Expand All @@ -208,7 +215,8 @@
" the number of individual data points present in the current zoom range\n",
" is above this threshold. The raw plot is displayed otherwise.\n",
" x_sampling/y_sampling (default=None):\n",
" Specifies the smallest allowed sampling interval along the x/y axis."
" Specifies the smallest allowed sampling interval along the x/y axis.\n",
" Used when rasterizing or datashading."
]
},
{
Expand Down
16 changes: 16 additions & 0 deletions hvplot/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ class HoloViewsConverter:
For plots generated with datashade=True or rasterize=True,
automatically increase the point size when the data is sparse
so that individual points become more visible
pixel_ratio (default=None):
Pixel ratio applied to the height and width, used when rasterizing or
datashading. When not set explicitly, the ratio is automatically
obtained from the browser device pixel ratio. Default is 1 when
the browser information is not available. Useful when the browser
information is not available (pixel_ratio=2 can give better results on
Retina displays) or for using lower resolution for speed.
rasterize (default=False):
Whether to apply rasterization using the Datashader library,
returning an aggregated Image (to be colormapped by the
Expand All @@ -290,6 +297,7 @@ class HoloViewsConverter:
is above this threshold. The raw plot is displayed otherwise.
x_sampling/y_sampling (default=None):
Specifies the smallest allowed sampling interval along the x/y axis.
Used when rasterizing or datashading.
Geographic options
------------------
Expand Down Expand Up @@ -426,6 +434,7 @@ class HoloViewsConverter:
_op_options = [
'datashade',
'rasterize',
'pixel_ratio',
'x_sampling',
'y_sampling',
'downsample',
Expand Down Expand Up @@ -587,6 +596,7 @@ def __init__(
dynspread=False,
x_sampling=None,
y_sampling=None,
pixel_ratio=None,
project=False,
tools=[],
attr_labels=None,
Expand Down Expand Up @@ -703,6 +713,7 @@ def __init__(
self.precompute = precompute
self.x_sampling = x_sampling
self.y_sampling = y_sampling
self.pixel_ratio = pixel_ratio

# By type
self.subplots = subplots
Expand Down Expand Up @@ -1774,6 +1785,7 @@ def method_wrapper(ds, x, y):
opts['line_width'] = self._style_opts['line_width']

style = {}

if self.datashade:
operation = datashade
if 'cmap' in opts and 'color_key' not in opts:
Expand All @@ -1783,13 +1795,17 @@ def method_wrapper(ds, x, y):
opts['cnorm'] = self._plot_opts['cnorm']
if 'rescale_discrete_levels' in self._plot_opts:
opts['rescale_discrete_levels'] = self._plot_opts['rescale_discrete_levels']
if self.pixel_ratio:
opts['pixel_ratio'] = self.pixel_ratio
elif self.rasterize:
operation = rasterize
eltype = 'ImageStack' if categorical else 'Image'
if 'cmap' in self._style_opts:
style['cmap'] = self._style_opts['cmap']
if self._dim_ranges.get('c', (None, None)) != (None, None):
style['clim'] = self._dim_ranges['c']
if self.pixel_ratio:
opts['pixel_ratio'] = self.pixel_ratio

processed = self._resample_obj(operation, obj, opts)
if self.dynspread:
Expand Down
12 changes: 12 additions & 0 deletions hvplot/tests/testoperations.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ def test_datashade_cnorm(self):
actual = plot.callback.inputs[0].callback.operation.p['cnorm']
assert actual == expected

def test_rasterize_pixel_ratio(self):
expected = 0.5
plot = self.df.hvplot(x='x', y='y', rasterize=True, pixel_ratio=expected)
actual = plot.callback.inputs[0].callback.operation.p['pixel_ratio']
assert actual == expected

def test_datashade_pixel_ratio(self):
expected = 0.5
plot = self.df.hvplot(x='x', y='y', datashade=True, pixel_ratio=expected)
actual = plot.callback.inputs[0].callback.operation.p['pixel_ratio']
assert actual == expected

def test_rasterize_rescale_discrete_levels(self):
expected = False
plot = self.df.hvplot(
Expand Down

0 comments on commit 266565b

Please sign in to comment.