lets_plot.layer_labels#

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

Configure annotations (for pie and bar charts).

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 the 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 https://lets-plot.org/python/pages/formats.html.

Examples

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

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

Line to show in the annotation.

Parameters:
valuestr

Enriched string which becomes one line of the annotation.

Returns:
layer_labels

Annotations 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}”

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)#

Text size in the annotation.

Parameters:
valuefloat

Text size in 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))
props()#

Return the dictionary of all properties of the object in their initial form.

Returns:
dict

Dictionary of properties.

Examples

1from lets_plot import *
2LetsPlot.setup_html()
3p = ggplot({'x': [0], 'y': [0]}) + geom_point(aes('x', 'y'))
4p.props()
{'data': {'x': [0], 'y': [0]},
 'mapping': <lets_plot.plot.core.FeatureSpec at 0x7ba2809f1db0>,
 'data_meta': {}}