layer_tooltips#
- class layer_tooltips(variables: List[str] | None = None)#
Configure tooltips.
Notes
Set tooltips=’none’ to hide tooltips from this layer.
Examples
1import numpy as np 2from lets_plot import * 3LetsPlot.setup_html() 4n = 100 5np.random.seed(42) 6data = { 7 'id': np.arange(n), 8 'x': np.random.normal(size=n), 9 'y': np.random.normal(size=n), 10 'c': np.random.choice(['a', 'b'], size=n), 11 'w': np.random.randint(1, 11, size=n) 12} 13ggplot(data, aes('x', 'y')) + \ 14 geom_point(aes(color='c', size='w'), \ 15 tooltips=layer_tooltips().line('@c "@id"') 16 .line('---') 17 .format('@y', '.2f') 18 .line('(x, y)|(^x, @y)') 19 .line('@|@w')) + \ 20 scale_size(range=[2, 4])
1import numpy as np 2from lets_plot import * 3LetsPlot.setup_html() 4n = 100 5np.random.seed(42) 6data = { 7 'x': np.random.normal(size=n), 8 'y': np.random.normal(size=n), 9 'c': np.random.randint(10, size=n) 10} 11ggplot(data, aes('x', 'y')) + \ 12 geom_point(aes(color='c'), tooltips='none')
- __init__(variables: List[str] | None = None)#
Initialize self.
- Parameters:
- variableslist of str
Variable names to place in the general tooltip with default formatting.
- as_dict()#
Return the dictionary of all properties of the object.
- Returns:
- dict
Dictionary of properties.
Examples
1from lets_plot import * 2LetsPlot.setup_html() 3layer_tooltips().format('@x', '.2f')\ 4 .line('@x @y')\ 5 .line('^fill')\ 6 .as_dict()
{'formats': [{'field': '@x', 'format': '.2f'}], 'lines': ['@x @y', '^fill']}
- format(field=None, format=None)#
Define the format for displaying the value. This format will be applied to the mapped value in the default tooltip or to the corresponding value specified in the ‘line’ template.
- Parameters:
- fieldstr
Name of an aesthetic or variable that would be formatted. The field name starts with a ‘^’ prefix for aesthetics, the variable name starts with a ‘@’ prefix or without any prefix.
- formatstr
Formatting specification. The format contains a number format (‘1.f’), a string template (‘{.1f}’) or a date/time format (‘%d.%m.%y’). The numeric format for non-numeric value will be ignored. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.
- Returns:
- layer_tooltips
Layer tooltips specification.
Notes
It’s possible to set the format for all positional aesthetics:
field=’^X’ - for all positional x,
field=’^Y’ - for all positional y.
The string template in format will allow to change lines for the default tooltip without line specifying. Also the template will change the line for side tooltips. Aes and var formats are not interchangeable, i.e. var format will not be applied to aes, mapped to this variable.
For more info see Formatting.
Examples
1import numpy as np 2from lets_plot import * 3LetsPlot.setup_html() 4n = 100 5np.random.seed(42) 6data = { 7 'a': np.random.normal(size=n), 8 'b': np.random.normal(size=n), 9 'c': np.random.choice(['X', 'Y'], size=n), 10 'd': np.random.uniform(size=n), 11 'e': np.random.randint(100, size=n) 12} 13ggplot(data, aes('a', 'b')) + \ 14 geom_point(aes(shape='c', size='e', color='d'), show_legend=False, \ 15 tooltips=layer_tooltips().format(field='a', format='.1f')\ 16 .format('^y', '.1f')\ 17 .line('(@a, ^y)')\ 18 .format('c', '{{{}}}')\ 19 .line('@|@c')\ 20 .format('^color', '≈ {.2f}')\ 21 .line('@|^color')\ 22 .format('e', '{}%')\ 23 .line('e|@e')) + \ 24 scale_size(range=[2, 4])
1import numpy as np 2from lets_plot import * 3LetsPlot.setup_html() 4n = 50 5np.random.seed(42) 6data = { 7 'v': np.random.normal(size=n), 8 'c': np.random.choice(['a', 'b', 'c'], size=n), 9} 10ggplot(data, aes('c', 'v')) + \ 11 geom_boxplot(tooltips=layer_tooltips().format('^Y', '.4f')\ 12 .format('^ymin', 'min y: {.2f}')\ 13 .format('^ymax', 'max y: {.2f}'))
- line(value)#
Line to show in the tooltip. Add a line template to the tooltip with a label.
- Parameters:
- valuestr
Enriched string which becomes one line of the tooltip.
- Returns:
- layer_tooltips
Layer tooltips specification.
Notes
Variables and aesthetics can be accessed via special syntax:
^color for aes,
@x for variable,
@{x + 1} for variable with spaces in the name,
@{x^2 + 1} for variable with spaces and ‘^’ symbol in the name,
@x^2 for variable with ‘^’ symbol in its name.
A ‘^’ symbol can be escaped with a backslash, a brace character in the literal text - by doubling:
‘x\^2’ -> “x^2”
‘{{x}}’ -> “{x}”
The specified ‘line’ for side tooltip will move it to the general multi-line tooltip. The default tooltip has a label before the value, usually containing the name of the mapped variable. It has its own behaviour, like blank label for axis aesthetics. This default label can be set in template using a pair of symbols ‘@|’. The label can be overridden by specifying a string value before ‘|’ symbol. Within the tooltip line the label is left-aligned, the string formed by template is right-aligned. If a label is not specified, the string will be centered in the tooltip.
Examples
1import numpy as np 2from lets_plot import * 3LetsPlot.setup_html() 4n = 100 5np.random.seed(42) 6x = np.linspace(-3, 3, n) 7y = 9 - x ** 2 + np.random.normal(scale=.3, size=n) 8data = {'x': x, '9 - x^2': y} 9ggplot(data) + \ 10 geom_point(aes('x', '9 - x^2'), \ 11 tooltips=layer_tooltips().format('x', '.3f')\ 12 .line('x = @x')\ 13 .format('9 - x^2', '.3f')\ 14 .line('9 - x\^2 = @{9 - x^2}'))
1import numpy as np 2from lets_plot import * 3LetsPlot.setup_html() 4n = 100 5np.random.seed(42) 6data = { 7 'x': np.random.normal(size=n), 8 'y': np.random.normal(size=n), 9 'c': np.random.choice(['X', 'Y'], size=n), 10 'p': np.random.uniform(size=n), 11 'w': np.random.randint(100, size=n) 12} 13ggplot(data, aes('x', 'y')) + \ 14 geom_point(aes(shape='c', size='w', color='p'), show_legend=False, \ 15 tooltips=layer_tooltips().format('x', '.2f')\ 16 .format('y', '.2f')\ 17 .line('(^x, ^y)')\ 18 .line('|^shape')\ 19 .line('@|^color')\ 20 .line('w|^size')) + \ 21 scale_size(range=[2, 4])
- anchor(value)#
Specify a fixed position for a general tooltip.
- Parameters:
- value{‘top_left’, ‘top_center’, ‘top_right’, ‘middle_left’, ‘middle_center’, ‘middle_right’, ‘bottom_left’, ‘bottom_center’, ‘bottom_right’}
Type of the tooltip anchoring.
- Returns:
- layer_tooltips
Layer tooltips specification.
Examples
1import numpy as np 2from lets_plot import * 3LetsPlot.setup_html() 4n = 100 5np.random.seed(42) 6x = np.random.normal(size=n) 7y = np.random.normal(size=n) 8ggplot({'x': x, 'y': y}, aes('x', 'y')) + \ 9 geom_point(tooltips=layer_tooltips().line('(^x, ^y)')\ 10 .anchor('top_center'))
- min_width(value)#
Minimum width of the general tooltip.
- Parameters:
- valuefloat
Minimum width value in px.
- Returns:
- layer_tooltips
Layer tooltips specification.
Examples
1import numpy as np 2from lets_plot import * 3LetsPlot.setup_html() 4n = 100 5np.random.seed(42) 6x = np.random.normal(size=n) 7y = np.random.normal(size=n) 8ggplot({'x': x, 'y': y}, aes('x', 'y')) + \ 9 geom_point(tooltips=layer_tooltips().line('(^x, ^y)')\ 10 .min_width(200))
- color(value)#
Function color(value) is deprecated.
- title(value)#
Line with title to show in the tooltip. Add a title template to the tooltip.
- Parameters:
- valuestr
Enriched string which becomes the title of the tooltip.
- Returns:
- layer_tooltips
Layer tooltips specification.
Notes
The specification rules are the same as for the lines() function: variables and aesthetics can be used in the template. The resulting string will be at the beginning of the general tooltip, centered and highlighted in bold. A long title can be split into multiple lines using \n as a text separator.
Examples
1import numpy as np 2from lets_plot import * 3LetsPlot.setup_html() 4n = 100 5np.random.seed(42) 6data = { 7 'id': np.arange(n), 8 'x': np.random.normal(size=n), 9 'y': np.random.normal(size=n), 10 'c': np.random.choice(['a', 'b'], size=n), 11 'w': np.random.randint(1, 11, size=n) 12} 13ggplot(data, aes('x', 'y')) + \ 14 geom_point(aes(color='c', size='w'), show_legend=False, \ 15 tooltips=layer_tooltips().title('@id') 16 .line('color|@c') 17 .line('size|@w'))
- disable_splitting()#
Hide side tooltips.
- Returns:
- layer_tooltips
Layer tooltips specification.
Notes
By default, the disable_splitting() function moves all side tooltips to the general tooltip. If the content of a general tooltip is specified with the line() functions, the general tooltip will get the given lines, and the side tooltips will be hidden.
Examples
1import numpy as np 2from lets_plot import * 3LetsPlot.setup_html() 4n = 50 5np.random.seed(42) 6data = { 7 'v': np.random.normal(size=n), 8 'c': np.random.choice(['a', 'b', 'c'], size=n), 9} 10ggplot(data, aes('c', 'v')) + \ 11 geom_boxplot(tooltips=layer_tooltips().disable_splitting())