1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
|
"""Contains the pyvista.Text class."""
from __future__ import annotations
import pathlib
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Literal
import pyvista
from pyvista._deprecate_positional_args import _deprecate_positional_args
from pyvista.core import _validation
from pyvista.core._typing_core import BoundsTuple
from pyvista.core.utilities.misc import _check_range
from pyvista.core.utilities.misc import _NameMixin
from pyvista.core.utilities.misc import _NoNewAttrMixin
from . import _vtk
from .colors import Color
from .prop3d import _Prop3DMixin
from .themes import Theme
from .tools import FONTS
if TYPE_CHECKING:
from collections.abc import Sequence
from pyvista.core._typing_core import VectorLike
from ._typing import ColorLike
HorizontalOptions = Literal['left', 'center', 'right']
VerticalOptions = Literal['bottom', 'center', 'top']
class CornerAnnotation(
_NoNewAttrMixin, _vtk.DisableVtkSnakeCase, _NameMixin, _vtk.vtkCornerAnnotation
):
"""Text annotation in four corners.
This is an annotation object that manages four text actors / mappers to provide
annotation in the four corners of a viewport.
Parameters
----------
position : str | bool
Position of the text.
text : str
Text input.
prop : pyvista.TextProperty, optional
Text property.
linear_font_scale_factor : float, optional
Linear font scale factor.
name : str, optional
The name of this actor used when tracking on a plotter.
.. versionadded:: 0.45
Examples
--------
Create text annotation in four corners.
>>> from pyvista import CornerAnnotation
>>> text = CornerAnnotation(0, 'text')
>>> prop = text.prop
"""
@_deprecate_positional_args(allowed=['position', 'text'])
def __init__( # noqa: PLR0917
self, position, text, prop=None, linear_font_scale_factor=None, name=None
):
"""Initialize a new text annotation descriptor."""
super().__init__()
self.set_text(position, text)
if prop is None:
self.prop = TextProperty()
if linear_font_scale_factor is not None:
self.linear_font_scale_factor = linear_font_scale_factor
self._name = name
def get_text(self, position):
"""Get the text to be displayed for each corner.
Parameters
----------
position : str | bool
Position of the text.
Returns
-------
str
Text to be displayed for each corner.
"""
return self.GetText(position)
def set_text(self, position, text):
"""Set the text to be displayed for each corner.
Parameters
----------
position : str | bool
Position of the text.
text : str
Text to be displayed for each corner.
"""
corner_mappings = {
'lower_left': self.LowerLeft,
'lower_right': self.LowerRight,
'upper_left': self.UpperLeft,
'upper_right': self.UpperRight,
'lower_edge': self.LowerEdge,
'upper_edge': self.UpperEdge,
'left_edge': self.LeftEdge,
'right_edge': self.RightEdge,
}
corner_mappings['ll'] = corner_mappings['lower_left']
corner_mappings['lr'] = corner_mappings['lower_right']
corner_mappings['ul'] = corner_mappings['upper_left']
corner_mappings['ur'] = corner_mappings['upper_right']
corner_mappings['top'] = corner_mappings['upper_edge']
corner_mappings['bottom'] = corner_mappings['lower_edge']
corner_mappings['right'] = corner_mappings['right_edge']
corner_mappings['r'] = corner_mappings['right_edge']
corner_mappings['left'] = corner_mappings['left_edge']
corner_mappings['l'] = corner_mappings['left_edge']
if isinstance(position, str):
position = corner_mappings[position]
elif position is True:
position = corner_mappings['upper_left']
self.SetText(position, text)
@property
def prop(self) -> TextProperty:
"""Property of this actor.
Returns
-------
pyvista.TextProperty
Property of this actor.
"""
return self.GetTextProperty()
@prop.setter
def prop(self, prop: TextProperty):
self.SetTextProperty(prop)
@property
def linear_font_scale_factor(self) -> float:
"""Font scaling factors.
Returns
-------
float
Font scaling factors.
"""
return self.GetLinearFontScaleFactor()
@linear_font_scale_factor.setter
def linear_font_scale_factor(self, factor: float):
self.SetLinearFontScaleFactor(factor)
class Text(_NoNewAttrMixin, _vtk.DisableVtkSnakeCase, _NameMixin, _vtk.vtkTextActor):
r"""Define text by default theme.
Parameters
----------
text : str, optional
Text string to be displayed.
"\n" is recognized as a carriage return/linefeed (line separator).
The characters must be in the UTF-8 encoding.
position : Sequence[float], optional
The position coordinate.
prop : pyvista.TextProperty, optional
The property of this actor.
name : str, optional
The name of this actor used when tracking on a plotter.
.. versionadded:: 0.45
Examples
--------
Create a text with text's property.
>>> from pyvista import Text
>>> text = Text()
>>> prop = text.prop
"""
@_deprecate_positional_args(allowed=['text'])
def __init__( # noqa: PLR0917
self, text=None, position=None, prop=None, name=None
):
"""Initialize a new text descriptor."""
super().__init__()
if text is not None:
self.input = text
if position is not None:
self.position = position
if prop is None:
self.prop = TextProperty()
self._name = name
@property
def input(self):
r"""Text string to be displayed.
Returns
-------
str
Text string to be displayed.
"\n" is recognized as a carriage return/linefeed (line separator).
The characters must be in the UTF-8 encoding.
"""
return self.GetInput()
@input.setter
def input(self, text: str):
self.SetInput(text)
@property
def prop(self):
"""Property of this actor.
Returns
-------
pyvista.TextProperty
Property of this actor.
"""
return self.GetTextProperty()
@prop.setter
def prop(self, prop: TextProperty):
self.SetTextProperty(prop)
@property
def position(self):
"""Position coordinate.
Returns
-------
Sequence[float]
Position coordinate.
"""
return self.GetPosition()
@position.setter
def position(self, position: Sequence[float]):
self.SetPosition(position[0], position[1])
class Label(_Prop3DMixin, Text):
"""2D label actor with a 3D position coordinate.
Unlike :class:`~pyvista.Text`, which uses 2D viewport coordinates to position text
in a plot, this class instead uses a 3D position coordinate. This class may be
positioned, oriented, and transformed in a manner similar to a 3D
:class:`~pyvista.Actor`.
In addition, this class supports an additional :attr:`relative_position` attribute.
In general, it is recommended to simply use :attr:`~pyvista.Prop3D.position` when positioning a
:class:`Label` by itself. However, if the position of the label depends on the
positioning of another actor, both :attr:`~pyvista.Prop3D.position` and
:attr:`relative_position` may be used together.
In these cases, the :attr:`~pyvista.Prop3D.position` of the label and actor
should be kept in-sync. See the examples below.
Parameters
----------
text : str, optional
Text string to be displayed.
position : VectorLike[float]
Position of the text in XYZ coordinates.
relative_position : VectorLike[float]
Position of the text in XYZ coordinates relative to its :attr:`~pyvista.Prop3D.position`.
size : int
Size of the text label.
prop : pyvista.TextProperty, optional
The property of this actor.
name : str, optional
The name of this actor used when tracking on a plotter.
.. versionadded:: 0.45
See Also
--------
pyvista.Plotter.add_point_labels
Examples
--------
Create a label for a point of interest. Here we add a label to the tip of a cone.
>>> import pyvista as pv
>>> cone_dataset = pv.Cone()
>>> tip = (0.5, 0, 0)
>>> label = pv.Label('tip', position=tip)
Plot the mesh and label.
>>> pl = pv.Plotter()
>>> cone_actor = pl.add_mesh(cone_dataset)
>>> _ = pl.add_actor(label)
>>> pl.show()
The previous example set the label's position as the cone's tip explicitly.
However, this means that the two actors now have different positions.
>>> cone_actor.position
(0.0, 0.0, 0.0)
>>> label.position
(0.5, 0.0, 0.0)
And if we change the 3D orientation of the cone and label, the label is no longer
positioned at the tip.
>>> cone_actor.orientation = 0, 0, 90
>>> label.orientation = 0, 0, 90
>>>
>>> pl = pv.Plotter()
>>> _ = pl.add_actor(cone_actor)
>>> _ = pl.add_actor(label)
>>> pl.show()
This is because rotations by :class:`pyvista.Prop3D` are applied **before** the
actor is moved to its final position, and therefore the label's position is not
considered in the rotation. Hence, the final position of the label remains at
``(0.5, 0.0, 0.0)`` as it did earlier, despite changing its orientation.
If we want the position of the label to have the same positioning *relative* to the
cone, we can instead set its :attr:`relative_position`.
First, reset the label's position to match the cone's position.
>>> label.position = cone_actor.position
>>> label.position
(0.0, 0.0, 0.0)
Now set its :attr:`relative_position` to the tip of the cone.
>>> label.relative_position = tip
>>> label.relative_position
(0.5, 0.0, 0.0)
Plot the results. The label is now correctly positioned at the tip of the cone.
This is because the :attr:`relative_position` is considered as part of the
rotation.
>>> pl = pv.Plotter()
>>> _ = pl.add_actor(cone_actor)
>>> _ = pl.add_actor(label)
>>> pl.show()
As long as the label and cone's :class:`pyvista.Prop3D` attributes are modified
together and synchronized, the label will remain at the tip of the cone.
Modify the position of the label and tip.
>>> cone_actor.position = (1.0, 2.0, 3.0)
>>> label.position = (1.0, 2.0, 3.0)
>>> pl = pv.Plotter()
>>> _ = pl.add_actor(cone_actor)
>>> _ = pl.add_actor(label)
>>> _ = pl.add_axes_at_origin()
>>> pl.show()
"""
def __init__(
self,
text: str | None = None,
position: VectorLike[float] = (0.0, 0.0, 0.0),
relative_position: VectorLike[float] = (0.0, 0.0, 0.0),
*,
size: int = 50,
prop: pyvista.Property | None = None,
name: str = 'Label',
):
Text.__init__(self, text=text, prop=prop)
self.GetPositionCoordinate().SetCoordinateSystemToWorld()
self.SetTextScaleModeToNone() # Use font size to control size of text
self._name = name
_Prop3DMixin.__init__(self)
self.relative_position = relative_position
self.position = position
self.size = size
@property
def _label_position(self) -> tuple[float, float, float]: # numpydoc ignore=RT01
"""Position of the label in xyz space.
This is the "true" position of the label. Internally this is loosely
equal to :attr:`~pyvista.Prop3D.position` + :attr:`relative_position`.
"""
return self.GetPositionCoordinate().GetValue()
@_label_position.setter
def _label_position(self, position: VectorLike[float]):
valid_position = _validation.validate_array3(position)
self.GetPositionCoordinate().SetValue(valid_position)
@property
def size(self) -> int: # numpydoc ignore=RT01
"""Size of the text label.
Notes
-----
The text property's font size used to control the size of the label.
"""
return self.prop.font_size
@size.setter
def size(self, size: int):
self.prop.font_size = size
@property
def relative_position(self) -> tuple[float, float, float]: # numpydoc ignore=RT01
"""Position of the label relative to its :attr:`~pyvista.Prop3D.position`."""
return tuple(self._relative_position.tolist())
@relative_position.setter
def relative_position(self, position: VectorLike[float]):
self._relative_position = _validation.validate_array3(position, dtype_out=float)
self._post_set_update()
def _post_set_update(self):
# Update the label's underlying text position
matrix4x4 = self._transformation_matrix
vector4 = (*self.relative_position, 1)
new_position = (matrix4x4 @ vector4)[:3]
self._label_position = new_position
def _get_bounds(self) -> BoundsTuple:
# Define its 3D position as its bounds
x, y, z = self._label_position
return BoundsTuple(x, x, y, y, z, z)
class TextProperty(_NoNewAttrMixin, _vtk.DisableVtkSnakeCase, _vtk.vtkTextProperty):
"""Define text's property.
Parameters
----------
theme : pyvista.plotting.themes.Theme, optional
Plot-specific theme.
color : pyvista.ColorLike, optional
Either a string, RGB list, or hex color string. For example:
``color='white'``, ``color='w'``, ``color=[1.0, 1.0, 1.0]``, or
``color='#FFFFFF'``. Color will be overridden if scalars are
specified.
font_family : str | None, optional
Font family or None.
orientation : float, optional
Text's orientation (in degrees).
font_size : int, optional
Font size.
font_file : str, optional
Font file path.
shadow : bool, optional
If enable the shadow.
justification_horizontal : str, optional
Text's horizontal justification.
Should be either "left", "center" or "right".
justification_vertical : str, optional
Text's vertical justification.
Should be either "bottom", "center" or "top".
italic : bool, default: False
Italicises title and bar labels.
bold : bool, default: True
Bolds title and bar labels.
background_color : pyvista.Color, optional
Background color of text.
background_opacity : pyvista.Color, optional
Background opacity of text.
Examples
--------
Create a text's property.
>>> from pyvista import TextProperty
>>> prop = TextProperty()
>>> prop.opacity = 0.5
>>> prop.background_color = 'b'
>>> prop.background_opacity = 0.5
>>> prop.show_frame = True
>>> prop.frame_color = 'b'
>>> prop.frame_width = 10
>>> prop.frame_color
Color(name='blue', hex='#0000ffff', opacity=255)
"""
_theme = Theme()
_color_set = None
_background_color_set = None
_font_family = None
@_deprecate_positional_args(allowed=['theme'])
def __init__( # noqa: PLR0917
self,
theme=None,
color=None,
font_family=None,
orientation=None,
font_size=None,
font_file=None,
shadow: bool = False, # noqa: FBT001, FBT002
justification_horizontal=None,
justification_vertical=None,
italic: bool = False, # noqa: FBT001, FBT002
bold: bool = False, # noqa: FBT001, FBT002
background_color=None,
background_opacity=None,
):
"""Initialize text's property."""
super().__init__()
if theme is None:
# copy global theme to ensure local property theme is fixed
# after creation.
self._theme.load_theme(pyvista.global_theme)
else:
self._theme.load_theme(theme)
self.color = color
self.font_family = font_family
if orientation is not None:
self.orientation = orientation
if font_size is not None:
self.font_size = font_size
if font_file is not None:
self.set_font_file(font_file)
if shadow:
self.enable_shadow()
if justification_horizontal is not None:
self.justification_horizontal = justification_horizontal
if justification_vertical is not None:
self.justification_vertical = justification_vertical
self.italic = italic
self.bold = bold
if background_color is not None:
self.background_color = background_color
if background_opacity is not None:
self.background_opacity = background_opacity
@property
def color(self) -> Color:
"""Color of text's property.
Returns
-------
pyvista.Color
Color of text's property.
"""
return Color(self.GetColor())
@color.setter
def color(self, color: ColorLike):
self._color_set = color is not None
rgb_color = Color(color, default_color=self._theme.font.color)
self.SetColor(rgb_color.float_rgb)
@property
def opacity(self) -> float:
"""Opacity of text's property.
Returns
-------
float
Opacity of the text. A single float value that will be applied globally
opacity of the text and uniformly applied everywhere. Between 0 and 1.
"""
return self.GetOpacity()
@opacity.setter
def opacity(self, opacity: float):
_check_range(opacity, (0, 1), 'opacity')
self.SetOpacity(opacity)
@property
def background_color(self) -> Color:
"""Background color of text's property.
Returns
-------
pyvista.Color
Background color of text's property.
"""
return Color(self.GetBackgroundColor())
@background_color.setter
def background_color(self, color: ColorLike):
self._background_color_set = color is not None
rgb_color = Color(color)
self.SetBackgroundColor(rgb_color.float_rgb)
@property
def background_opacity(self) -> float:
"""Background opacity of text's property.
Returns
-------
float
Background opacity of the text. A single float value that will be applied globally.
Background opacity of the text and uniformly applied everywhere. Between 0 and 1.
"""
return self.GetBackgroundOpacity()
@background_opacity.setter
def background_opacity(self, opacity: float):
_check_range(opacity, (0, 1), 'background_opacity')
self.SetBackgroundOpacity(opacity)
@property
def show_frame(self) -> bool:
"""Visibility of frame.
Returns
-------
bool:
If shows the frame.
"""
return bool(self.GetFrame())
@show_frame.setter
def show_frame(self, frame: bool):
self.SetFrame(frame)
@property
def frame_color(self) -> Color:
"""Frame color of text property.
Returns
-------
pyvista.Color
Frame color of text property.
"""
return Color(self.GetFrameColor())
@frame_color.setter
def frame_color(self, color):
self.SetFrameColor(Color(color).float_rgb)
@property
def frame_width(self) -> int:
"""Width of the frame.
Returns
-------
int
Width of the frame. The width is expressed in pixels.
The default is 1 pixel.
"""
return self.GetFrameWidth()
@frame_width.setter
def frame_width(self, width: int):
self.SetFrameWidth(width)
@property
def font_family(self) -> str | None:
"""Font family.
Returns
-------
str | None
Font family or None.
"""
return self._font_family
@font_family.setter
def font_family(self, font_family: str | None):
if font_family is None:
font_family = self._theme.font.family
self._font_family = font_family
self.SetFontFamily(FONTS[self._font_family].value)
@property
def font_size(self) -> int:
"""Font size.
Returns
-------
int
Font size.
"""
return self.GetFontSize()
@font_size.setter
def font_size(self, font_size: int):
self.SetFontSize(font_size)
def enable_shadow(self) -> None:
"""Enable the shadow."""
self.SetShadow(True)
@property
def orientation(self) -> float:
"""Text's orientation (in degrees).
Returns
-------
float
Text's orientation (in degrees).
"""
return self.GetOrientation()
@orientation.setter
def orientation(self, orientation: float):
self.SetOrientation(orientation)
def set_font_file(self, font_file: str):
"""Set the font file.
Parameters
----------
font_file : str
Font file path.
"""
path = pathlib.Path(font_file)
path = path.resolve()
if not Path(path).is_file():
msg = f'Unable to locate {path}'
raise FileNotFoundError(msg)
self.SetFontFamily(_vtk.VTK_FONT_FILE)
self.SetFontFile(str(path))
@property
def justification_horizontal(self) -> str:
"""Text's justification horizontal.
Returns
-------
str
Text's horizontal justification.
Should be either "left", "center" or "right".
"""
justification = self.GetJustificationAsString().lower()
if justification == 'centered':
justification = 'center'
return justification
@justification_horizontal.setter
def justification_horizontal(self, justification: str):
if justification.lower() == 'left':
self.SetJustificationToLeft()
elif justification.lower() == 'center':
self.SetJustificationToCentered()
elif justification.lower() == 'right':
self.SetJustificationToRight()
else:
msg = (
f'Invalid {justification} for justification_horizontal. '
'Should be either "left", "center" or "right".'
)
raise ValueError(msg)
@property
def justification_vertical(self) -> str:
"""Text's vertical justification.
Returns
-------
str
Text's vertical justification.
Should be either "bottom", "center" or "top".
"""
justification = self.GetVerticalJustificationAsString().lower()
if justification == 'centered':
justification = 'center'
return justification
@justification_vertical.setter
def justification_vertical(self, justification: str):
if justification.lower() == 'bottom':
self.SetVerticalJustificationToBottom()
elif justification.lower() == 'center':
self.SetVerticalJustificationToCentered()
elif justification.lower() == 'top':
self.SetVerticalJustificationToTop()
else:
msg = (
f'Invalid {justification} for justification_vertical. '
'Should be either "bottom", "center" or "top".'
)
raise ValueError(msg)
@property
def italic(self) -> bool:
"""Italic of text's property.
Returns
-------
bool
If text is italic.
"""
return bool(self.GetItalic())
@italic.setter
def italic(self, italic: bool):
self.SetItalic(italic)
@property
def bold(self) -> bool:
"""Bold of text's property.
Returns
-------
bool
If text is bold.
"""
return bool(self.GetBold())
@bold.setter
def bold(self, bold: bool):
self.SetBold(bold)
def shallow_copy(self, to_copy: TextProperty) -> None:
"""Create a shallow copy of the text's property.
Parameters
----------
to_copy : pyvista.TextProperty
Text's property to copy from.
"""
self.ShallowCopy(to_copy)
|