Skip to content

Commit 41ed660

Browse files
Docstrings: Change docstrings for the inherited members of the smooth_labels().
1 parent dc2399e commit 41ed660

1 file changed

Lines changed: 174 additions & 2 deletions

File tree

python-package/lets_plot/plot/annotation.py

Lines changed: 174 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,12 @@ class smooth_labels(layer_labels):
344344
345345
.. jupyter-execute::
346346
:linenos:
347-
:emphasize-lines: 8
347+
:emphasize-lines: 9
348348
349349
import numpy as np
350350
from lets_plot import *
351351
LetsPlot.setup_html()
352+
np.random.seed(42)
352353
n = 100
353354
x = np.linspace(-2, 2, n)
354355
y = x**2 + np.random.normal(size=n)
@@ -401,11 +402,12 @@ def eq(self, lhs=None, rhs=None, format=None, threshold=None) -> "smooth_labels"
401402
--------
402403
.. jupyter-execute::
403404
:linenos:
404-
:emphasize-lines: 9
405+
:emphasize-lines: 10
405406
406407
import numpy as np
407408
from lets_plot import *
408409
LetsPlot.setup_html()
410+
np.random.seed(42)
409411
x = np.linspace(-2, 2, 50)
410412
y = x**2 + np.random.normal(size=len(x))
411413
ggplot({'x': x, 'y': y}, aes('x', 'y')) + geom_point() + \\
@@ -544,3 +546,173 @@ def as_dict(self):
544546
d["options"] = _filter_none(opts)
545547

546548
return _filter_none(d)
549+
550+
def format(self, field=None, format=None):
551+
"""
552+
Define the format for displaying the value.
553+
This format will be applied to the corresponding value specified in the 'line' template.
554+
555+
Parameters
556+
----------
557+
field : str
558+
Name of an aesthetic or variable that would be formatted.
559+
The field name starts with a '^' prefix for aesthetics,
560+
the variable name starts with a '@' prefix or without any prefix.
561+
format : str
562+
Formatting specification. The format contains a number format ('1.f'),
563+
a string template ('{.1f}') or a date/time format ('%d.%m.%y').
564+
The numeric format for non-numeric value will be ignored.
565+
If you need to include a brace character in the literal text,
566+
it can be escaped by doubling: ``{{`` and ``}}``.
567+
568+
Returns
569+
-------
570+
``smooth_labels``
571+
Annotations specification.
572+
573+
Notes
574+
-----
575+
For more info see `Formatting <https://lets-plot.org/python/pages/formats.html>`__.
576+
577+
Examples
578+
--------
579+
.. jupyter-execute::
580+
:linenos:
581+
:emphasize-lines: 11
582+
583+
import numpy as np
584+
from lets_plot import *
585+
LetsPlot.setup_html()
586+
np.random.seed(42)
587+
x = np.linspace(-2, 2, 50)
588+
y = x**2 + np.random.normal(size=len(x))
589+
ggplot({'x': x, 'y': y}, aes('x', 'y')) + \\
590+
geom_point() + \\
591+
geom_smooth(deg=2, labels=smooth_labels()\\
592+
.line(r'\(R\^2=\)@..r2..')\\
593+
.format('..r2..', '.3f')\\
594+
.label_x('center'))
595+
596+
"""
597+
# This method is overridden explicitly only to provide a custom docstring.
598+
return super().format(field, format)
599+
600+
def line(self, value):
601+
"""
602+
Add a line of text to the multiline label annotation.
603+
604+
This method configures one line of text that will be displayed in a
605+
multiline label. Multiple calls to this method can be chained to build
606+
up a complete multiline annotation.
607+
608+
Parameters
609+
----------
610+
value : str
611+
The text content for this line of the annotation. Can include
612+
variable and aesthetic references.
613+
614+
Returns
615+
-------
616+
``smooth_labels``
617+
Annotations specification.
618+
619+
Notes
620+
-----
621+
Variables and aesthetics can be accessed via special syntax:
622+
623+
- ^color for aesthetics,
624+
- @x for variable,
625+
- @{x + 1} for variable with spaces in the name,
626+
- @{x^2 + 1} for variable with spaces and '^' symbol in the name,
627+
- @x^2 for variable with '^' symbol in its name.
628+
629+
Special characters can be escaped:
630+
631+
- 'x\\\\^2' -> "x^2" (escape ^ with backslash)
632+
- '{{x}}' -> "{x}" (escape braces by doubling)
633+
634+
Examples
635+
--------
636+
.. jupyter-execute::
637+
:linenos:
638+
:emphasize-lines: 5-6
639+
640+
from lets_plot import *
641+
LetsPlot.setup_html()
642+
ggplot({'x': [0, 1, 2], 'y': [0, 1, 4]}, aes('x', 'y')) + geom_point() + \\
643+
geom_smooth(deg=2, labels=smooth_labels()\\
644+
.line(r'\(R\^2=\)@..r2..')\\
645+
.line('~eq'))
646+
647+
"""
648+
# This method is overridden explicitly only to provide a custom docstring.
649+
return super().line(value)
650+
651+
def size(self, value):
652+
"""
653+
Set the text size for the annotation.
654+
655+
Parameters
656+
----------
657+
value : float
658+
The text size value for the annotation.
659+
660+
Returns
661+
-------
662+
``smooth_labels``
663+
Annotations specification.
664+
665+
Examples
666+
--------
667+
668+
.. jupyter-execute::
669+
:linenos:
670+
:emphasize-lines: 4
671+
672+
from lets_plot import *
673+
LetsPlot.setup_html()
674+
ggplot({'x': [0, 1, 2], 'y': [0, 1, 4]}, aes('x', 'y')) + geom_point() + \\
675+
geom_smooth(deg=2, labels=smooth_labels().line('~eq').size(25))
676+
677+
"""
678+
# This method is overridden explicitly only to provide a custom docstring.
679+
return super().size(value)
680+
681+
def inherit_color(self):
682+
"""
683+
Use the layer's color for the annotation text.
684+
685+
When enabled, the annotation text will inherit the color from the
686+
layer it's associated with, rather than using a default or
687+
explicitly set color.
688+
689+
Returns
690+
-------
691+
``smooth_labels``
692+
Annotations specification.
693+
694+
Examples
695+
--------
696+
697+
.. jupyter-execute::
698+
:linenos:
699+
:emphasize-lines: 14
700+
701+
import numpy as np
702+
from lets_plot import *
703+
LetsPlot.setup_html()
704+
np.random.seed(42)
705+
x = np.linspace(-2, 2, 50)
706+
y = x**2 + np.random.normal(size=len(x))
707+
g = np.random.choice(['A', 'B'], size=len(x))
708+
data = {'x': x, 'y': y, 'g': g}
709+
ggplot(data, aes('x', 'y', color='g')) + \\
710+
geom_point() + \\
711+
geom_smooth(deg=2, labels=smooth_labels()
712+
.line('~eq')
713+
.eq(format='.2f')
714+
.inherit_color())
715+
716+
"""
717+
# This method is overridden explicitly only to provide a custom docstring.
718+
return super().inherit_color()

0 commit comments

Comments
 (0)