layer_labels#

class layer_labels(variables: List[str] | None = None)#

Configure annotations for geometry layers.

Annotations are currently supported for bar, pie, and crossbar geometry layers. This class provides methods to customize the appearance and content of text labels displayed on these geometries.

Notes

By default, annotation text color is automatically selected for optimal contrast: white text appears on darker filled geometries, and black text appears on lighter filled geometries.

The text color can be manually specified using: theme(label_text=element_text(color=...))

Alternatively, the inherit_color() method can be used to override both automatic and manual color settings, making the annotation text use the geometry’s color aesthetic instead.

Examples

1from lets_plot import *
2LetsPlot.setup_html()
3data = {'name': ['a', 'b', 'c', 'd', 'b'], 'value': [40, 90, 10, 50, 20 ]}
4ggplot(data) + geom_pie(aes(slice='value', fill='name'), size=15, hole=0.4, \
5                        stat='identity', tooltips='none', \
6                        labels=layer_labels().line('@value'))
__init__(variables: List[str] | None = None)#

Initialize self.

Parameters:
variableslist of str

Variable names to place in the annotation with default formatting.

as_dict()#

Return a dictionary of all properties of the object.

Returns:
dict

Dictionary of properties.

Examples

1from lets_plot import *
2LetsPlot.setup_html()
3layer_labels().format('@{..prop..}', '.0%') \
4             .line('@name') \
5             .line('(@{..prop..})') \
6             .as_dict()
{'formats': [{'field': '@{..prop..}', 'format': '.0%'}],
 'lines': ['@name', '(@{..prop..})']}
format(field=None, format=None)#

Define the format for displaying the value. This format will be applied 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_labels

Annotations specification.

Notes

For more info see Formatting.

Examples

1from lets_plot import *
2LetsPlot.setup_html()
3data = {'name': ['a', 'b', 'c', 'd', 'b'], 'value': [40, 90, 10, 50, 20 ]}
4ggplot(data) + geom_pie(aes(fill=as_discrete('name', order_by='..count..'), weight='value'), \
5                        size=15, tooltips='none', \
6                        labels=layer_labels(['..proppct..']) \
7                                      .format('..proppct..', '{.1f}%'))

 1from lets_plot import *
 2LetsPlot.setup_html()
 3data = {'name': ['a', 'b', 'c', 'd', 'b'], 'value': [40, 90, 10, 50, 20 ]}
 4ggplot(data) + geom_pie(aes(fill=as_discrete('name', order_by='..count..', order=1), weight='value'), \
 5                        size=15, tooltips='none', \
 6                        labels=layer_labels() \
 7                                        .format('^fill', '{{{}}}') \
 8                                        .line('^fill') \
 9                                        .format('..count..', 'd') \
10                                        .line('@{..count..}') \
11                                        .format('..prop..', '.1%') \
12                                        .line('@{..prop..}') \
13                                        .format('..sum..', 'of {d}') \
14                                        .line('@{..sum..}'))
line(value)#

Add a line of text to the multiline label annotation.

This method configures one line of text that will be displayed in a multiline label. Multiple calls to this method can be chained to build up a complete multiline annotation.

Parameters:
valuestr

The text content for this line of the annotation. Can include variable and aesthetic references.

Returns:
layer_labels

Annotations specification.

Notes

Variables and aesthetics can be accessed via special syntax:

  • ^color for aesthetics,

  • @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.

Special characters can be escaped:

  • ‘x\^2’ -> “x^2” (escape ^ with backslash)

  • ‘{{x}}’ -> “{x}” (escape braces by doubling)

Examples

 1from lets_plot import *
 2LetsPlot.setup_html()
 3data = {'name': ['a', 'b', 'c', 'd', 'b'], 'value': [40, 90, 10, 50, 20 ]}
 4ggplot(data) + geom_pie(aes(fill='name', weight='value'), size=15, \
 5                        tooltips='none', \
 6                        labels=layer_labels()\
 7                              .format('..prop..', '.1%')\
 8                              .line('"^fill"')\
 9                              .line('@{..count..}')\
10                              .line('@{..prop..}')\
11                              .line('(@{..sum..})'))
size(value)#

Set the text size for the annotation.

Parameters:
valuefloat

The text size value for the annotation.

Returns:
layer_labels

Annotations specification.

Examples

1from lets_plot import *
2LetsPlot.setup_html()
3data = {'name': ['a', 'b', 'c', 'd', 'b'], 'value': [40, 90, 10, 50, 20 ]}
4ggplot(data) + geom_pie(aes(slice='value', fill='name'), size=15, hole=0.4, \
5                        stat='identity', tooltips='none', \
6                        labels=layer_labels().line('@value')
7                                                 .size(25))
inherit_color()#

Use the layer’s color for the annotation text.

When enabled, the annotation text will inherit the color from the layer it’s associated with, rather than using a default or explicitly set color.

Returns:
layer_labels

Annotations specification.

Examples

1from lets_plot import *
2LetsPlot.setup_html()
3data = {'name': ['a', 'b', 'c', 'd', 'b'], 'value': [40, 90, 10, 50, 20 ]}
4ggplot(data) + geom_pie(aes(slice='value', color='name'), alpha=0, size=15, hole=0.4, \
5                        stroke=5, spacer_color='pen', \
6                        stat='identity', tooltips='none', \
7                        labels=layer_labels().line('@value')
8                                             .inherit_color())