Skip to content

Commit

Permalink
Support cticks (#1368)
Browse files Browse the repository at this point in the history
Co-authored-by: maximlt <[email protected]>
  • Loading branch information
ahuang11 and maximlt authored Jul 19, 2024
1 parent 26eb1e4 commit 4d295b7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
16 changes: 16 additions & 0 deletions doc/reference/xarray/image.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@
"data.hvplot.image(x='lon', y='lat', z='air', title=time, clabel='T [C]')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also override the colorbar ticks and labels with `cticks`, specified as an integer, list of ticks positions, or list of tuples of the tick positions and labels."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data.hvplot.image(x='lon', y='lat', z='air', title=time, clabel='T [C]', cticks=[(-40, \"Below Freezing\"), (0, \"Freezing\"), (40, \"Above Freezing\")])"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
4 changes: 2 additions & 2 deletions doc/user_guide/Customization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@
" Axis labels for the x-axis, y-axis, and colorbar\n",
" xlim/ylim (default=None): tuple or list\n",
" Plot limits of the x- and y-axis\n",
" xticks/yticks (default=None): int or list\n",
" Ticks along x- and y-axis specified as an integer, list of\n",
" xticks/yticks/cticks (default=None): int or list\n",
" Ticks along x-axis, y-axis, and colorbar specified as an integer, list of\n",
" ticks positions, or list of tuples of the tick positions and labels\n",
" width (default=700)/height (default=300): int\n",
" The width and height of the plot in pixels\n",
Expand Down
12 changes: 10 additions & 2 deletions hvplot/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ class HoloViewsConverter:
Axis labels for the x-axis, y-axis, and colorbar
xlim/ylim (default=None): tuple or list
Plot limits of the x- and y-axis
xticks/yticks (default=None): int or list
Ticks along x- and y-axis specified as an integer, list of
xticks/yticks/cticks (default=None): int or list
Ticks along x-axis, y-axis, and colorbar specified as an integer, list of
ticks positions, or list of tuples of the tick positions and labels
width (default=700)/height (default=300): int
The width and height of the plot in pixels
Expand Down Expand Up @@ -402,6 +402,7 @@ class HoloViewsConverter:
'colormap',
'fontsize',
'c',
'cticks',
'cmap',
'color_key',
'cnorm',
Expand Down Expand Up @@ -550,6 +551,7 @@ def __init__(
invert=False,
stacked=False,
colorbar=None,
cticks=None,
datashade=False,
rasterize=False,
downsample=None,
Expand Down Expand Up @@ -784,6 +786,12 @@ def __init__(
plot_opts['colorbar'] = True
elif self.rasterize:
plot_opts['colorbar'] = plot_opts.get('colorbar', True)
if plot_opts.get('colorbar') is True and cticks is not None:
if self._backend == 'plotly':
raise ValueError('cticks option is not supported for plotly backend')
key = 'cticks' if self._backend == 'bokeh' else 'cbar_ticks'
self._style_opts[key] = cticks

if 'logz' in kwds and 'logz' in self._kind_options.get(self.kind, {}):
plot_opts['logz'] = kwds.pop('logz')
if invert:
Expand Down
8 changes: 8 additions & 0 deletions hvplot/tests/testoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,11 @@ def test_dataset_scatter_with_title(self, ds2, backend):
plot = ds_sel.hvplot.scatter(x='foo', y='bar') # Image plot
opts = Store.lookup_options(backend, plot, 'plot')
assert opts.kwargs['title'] == 'time = 0, y = 0, x = 0, band = 0'


@pytest.mark.usefixtures('load_xarray_accessor')
class TestXarrayCticks:
def test_cticks(self, da2):
plot = da2.isel(other=0).hvplot(cticks=[5, 10])
handles = hv.renderer('bokeh').get_plot(plot).handles
assert handles['colorbar'].ticker.ticks == [5, 10]

0 comments on commit 4d295b7

Please sign in to comment.