Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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 scalebar support #6002

Merged
merged 25 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Update Scalebar document
  • Loading branch information
hoxbro committed May 29, 2024
commit c3191d0ea892d4c5ea0f12d7db89a9e5fec04853
98 changes: 97 additions & 1 deletion examples/reference/features/bokeh/Scalebar.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,103 @@
"id": "df229fb9-0a56-47b4-a8d2-72cab8782624",
"metadata": {},
"source": [
"Adding scalebar to your figure"
"<div class=\"contentcontainer med left\" style=\"margin-left: -50px;\">\n",
"<dl class=\"dl-horizontal\">\n",
" <dt>Title</dt> <dd> Scalebar</dd>\n",
" <dt>Dependencies</dt> <dd>Bokeh</dd>\n",
" <dt>Backends</dt> \n",
" <dd><a href='./Scalebar.ipynb'>Bokeh</a></dd>\n",
"</dl>\n",
"</div>\n",
"\n",
"The `scalebar` feature adds an overlaid scalebar to the element. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1cee420f-3ad1-4b0c-ae66-b26d726d7a0a",
"metadata": {},
"outputs": [],
"source": [
"import holoviews as hv\n",
"import numpy as np\n",
"\n",
"hv.extension(\"bokeh\")\n",
"\n",
"hv.RGB.load_image(\"../assets/pollen.png\").opts(scalebar=True)"
]
},
{
"cell_type": "markdown",
"id": "34200a10-f6d8-4ed5-a93b-9c62f0cbb7c2",
"metadata": {},
"source": [
"### Unit\n",
"The unit of the scalebar tries to infer it for the first `kdims`, and will fallback to `meter` if nothing is set. To overwrite this you can use `scalebar_unit`. This can ither be a string or a tuple with length of two - one for the dimension and one for the base-dimension. An example is shown below, where we have the dimension be `mm` and the base-dimension `m`. \n",
hoxbro marked this conversation as resolved.
Show resolved Hide resolved
"\n",
"The `scalebar_unit` parameter specifies the unit of the scalebar, which can be a string or a tuple. The determination order is: this value if set, the element's kdim unit (if it exists), or meter. If a tuple, the first value is the unit, and the second is the base unit."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ef9435e-691d-43c0-8c68-9d24f73c7a58",
"metadata": {},
"outputs": [],
"source": [
"hv.RGB.load_image(\"../assets/pollen.png\").opts(scalebar=True, scalebar_unit=(\"mm\", \"m\"))"
]
},
{
"cell_type": "markdown",
"id": "39af797f-4ea2-449b-96c7-eb7d0da8394e",
"metadata": {},
"source": [
"### Customization\n",
"\n",
"You can customize the scalebar with the following parameters:\n",
"\n",
"- The `scalebar_location` parameter defines the positioning anchor for the scalebar, with options like \"bottom_right\", \"top_left\", \"center\", etc.\n",
"- The `scalebar_label` parameter allows customization of the label template, using variables such as `@{value}` and `@{unit}`.\n",
"- The `scalebar_opts` parameter enables specific styling options for the scalebar, as detailed in the [Bokeh's documentation](https://docs.bokeh.org/en/latest/docs/reference/models/annotations.html#bokeh.models.ScaleBar).\n",
"\n",
"All these parameters are only utilized if `scalebar` is set to `True` in `.opts()`. An example of "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "998beb2d-a6a8-43fc-bab0-bf08e3129f91",
"metadata": {},
"outputs": [],
"source": [
"curve1 = hv.Curve(np.random.rand(1000), label=\"Curve 1\")\n",
"curve1.opts(scalebar=True, scalebar_location=\"bottom_left\", scalebar_label=\"First | @{value} @{unit}\")\n",
"curve1"
]
},
{
"cell_type": "markdown",
"id": "871c7b55-353e-4ec6-8b79-bf1299cd2a45",
"metadata": {},
"source": [
"### Toolbar \n",
"\n",
"By default the scalebar tool is added to the toolbar, pressing this will either hide or show the scalebars. The icon can be hidden by setting the `scalebar_icon` to `False`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b74dbebc-5e50-4b11-9170-94beeedb47e7",
"metadata": {},
"outputs": [],
"source": [
"curve2 = hv.Curve(np.random.rand(1000) + 1, label=\"Curve 2\")\n",
"curve2.opts(scalebar=True, scalebar_location=\"top_left\", scalebar_label=\"Second | @{value} @{unit}\", scalebar_unit=(\"cm\", \"m\"))\n",
"\n",
"(curve1 * curve2).opts(width=500, height=500, show_legend=False)"
]
}
],
Expand Down
4 changes: 1 addition & 3 deletions holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ class ElementPlot(BokehPlot, GenericElementPlot):
This can use special variables:
* ``@{value}`` The current value. Optionally can provide a number
formatter with e.g. ``@{value}{%.2f}``.
* ``@{unit}`` The unit of measure, by default in the short form.
Optionally can provide a format ``@{unit}{short}`` or
``@{unit}{long}``.
* ``@{unit}`` The unit of measure.

The scalebar_label is only used if scalebar is True.""")

Expand Down
Loading