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
|
"""Module containing the wrapping of CubeAxesActor."""
from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
import pyvista
from pyvista.core.utilities.arrays import convert_string_array
from . import _vtk
if TYPE_CHECKING: # pragma: no cover
from pyvista.core._typing_core import BoundsLike
def make_axis_labels(vmin, vmax, n, fmt):
"""
Create axis labels as a vtkStringArray.
Parameters
----------
vmin : float
The minimum value for the axis labels.
vmax : float
The maximum value for the axis labels.
n : int
The number of labels to create.
fmt : str
A format string for the labels. If the string starts with '%', the label will be formatted using the old-style string formatting method.
Otherwise, the label will be formatted using the new-style string formatting method.
Returns
-------
vtkStringArray
The created labels as a vtkStringArray object.
"""
labels = _vtk.vtkStringArray()
for v in np.linspace(vmin, vmax, n):
label = (fmt % v if fmt.startswith('%') else fmt.format(v)) if fmt else f'{v}'
labels.InsertNextValue(label)
return labels
class CubeAxesActor(_vtk.vtkCubeAxesActor):
"""Wrap vtkCubeAxesActor.
This class is created to wrap vtkCubeAxesActor, which is used to draw axes
and labels for the input data bounds. This wrapping aims to provide a
user-friendly interface to use `vtkCubeAxesActor
<https://vtk.org/doc/nightly/html/classvtkCubeAxesActor.html>`_.
Parameters
----------
camera : pyvista.Camera
Camera to link to the axes actor.
minor_ticks : bool, default: False
If ``True``, also plot minor ticks on all axes.
tick_location : str, optional
Set how the ticks are drawn on the axes grid. Options include:
``'inside', 'outside', 'both'``.
x_title : str, default: "X Axis"
Title of the x-axis.
y_title : str, default: "Y Axis"
Title of the y-axis.
z_title : str, default: "Z Axis"
Title of the z-axis.
x_axis_visibility : bool, default: True
Visibility of the x-axis.
y_axis_visibility : bool, default: True
Visibility of the y-axis.
z_axis_visibility : bool, default: True
Visibility of the z-axis.
x_label_format : str, optional
A format string defining how tick labels are generated from tick
positions for the x-axis. Defaults to the theme format if set,
otherwise ``'%.1f'``.
y_label_format : str, optional
A format string defining how tick labels are generated from tick
positions for the y-axis. Defaults to the theme format if set,
otherwise ``'%.1f'``.
z_label_format : str, optional
A format string defining how tick labels are generated from tick
positions for the z-axis. Defaults to the theme format if set,
otherwise ``'%.1f'``.
x_label_visibility : bool, default: True
The visibility of the x-axis labels.
y_label_visibility : bool, default: True
The visibility of the y-axis labels.
z_label_visibility : bool, default: True
The visibility of the z-axis labels.
n_xlabels : int, default: 5
Number of labels along the x-axis.
n_ylabels : int, default: 5
Number of labels along the y-axis.
n_zlabels : int, default: 5
Number of labels along the z-axis.
Examples
--------
Create a 3D plotter and add a CubeAxesActor to it.
>>> import pyvista as pv
>>> mesh = pv.Cube()
>>> pl = pv.Plotter()
>>> actor = pl.add_mesh(mesh)
>>> cube_axes_actor = pv.CubeAxesActor(pl.camera)
>>> cube_axes_actor.bounds = mesh.bounds
>>> actor, property = pl.add_actor(cube_axes_actor)
>>> pl.show()
"""
def __init__(
self,
camera,
minor_ticks=False,
tick_location=None,
x_title='X Axis',
y_title='Y Axis',
z_title='Z Axis',
x_axis_visibility=True,
y_axis_visibility=True,
z_axis_visibility=True,
x_label_format=None,
y_label_format=None,
z_label_format=None,
x_label_visibility=True,
y_label_visibility=True,
z_label_visibility=True,
n_xlabels=5,
n_ylabels=5,
n_zlabels=5,
):
"""Initialize CubeAxesActor."""
super().__init__()
self.camera = camera
# empty string used for clearing axis labels
self._empty_str = _vtk.vtkStringArray()
self._empty_str.InsertNextValue('')
# stop labels from being generated several times during init
self.x_axis_visibility = False
self.y_axis_visibility = False
self.z_axis_visibility = False
if not minor_ticks:
self.x_axis_minor_tick_visibility = minor_ticks
self.y_axis_minor_tick_visibility = minor_ticks
self.z_axis_minor_tick_visibility = minor_ticks
if tick_location:
self.tick_location = tick_location
self.x_title = x_title
self.y_title = y_title
self.z_title = z_title
self._x_label_visibility = x_label_visibility
self._y_label_visibility = y_label_visibility
self._z_label_visibility = z_label_visibility
if x_label_format is None:
x_label_format = pyvista.global_theme.font.fmt
if x_label_format is None:
x_label_format = '%.1f'
if y_label_format is None:
y_label_format = pyvista.global_theme.font.fmt
if y_label_format is None:
y_label_format = '%.1f'
if z_label_format is None:
z_label_format = pyvista.global_theme.font.fmt
if z_label_format is None:
z_label_format = '%.1f'
self.x_label_format = x_label_format
self.y_label_format = y_label_format
self.z_label_format = z_label_format
self.n_xlabels = n_xlabels
self.n_ylabels = n_ylabels
self.n_zlabels = n_zlabels
self.x_axis_visibility = x_axis_visibility
self.y_axis_visibility = y_axis_visibility
self.z_axis_visibility = z_axis_visibility
@property
def tick_location(self) -> str: # numpydoc ignore=RT01
"""Return or set how the ticks are drawn on the axes grid.
Options include: ``'inside', 'outside', 'both'``.
"""
tloc = self.GetTickLocation()
if tloc == 0:
return 'inside'
if tloc == 1:
return 'outside'
return 'both'
@tick_location.setter
def tick_location(self, value: str): # numpydoc ignore=GL08
if not isinstance(value, str):
raise TypeError(f'`tick_location` must be a string, not {type(value)}')
value = value.lower()
if value in ('inside'):
self.SetTickLocationToInside()
elif value in ('outside'):
self.SetTickLocationToOutside()
elif value in ('both'):
self.SetTickLocationToBoth()
else:
raise ValueError(
f'Value of tick_location ("{value}") should be either "inside", "outside", '
'or "both".',
)
@property
def bounds(self) -> BoundsLike: # numpydoc ignore=RT01
"""Return or set the bounding box."""
return self.GetBounds()
@bounds.setter
def bounds(self, bounds: BoundsLike): # numpydoc ignore=GL08
self.SetBounds(bounds)
self._update_labels()
self.x_axis_range = float(bounds[0]), float(bounds[1])
self.y_axis_range = float(bounds[2]), float(bounds[3])
self.z_axis_range = float(bounds[4]), float(bounds[5])
@property
def x_axis_range(self) -> tuple[float, float]: # numpydoc ignore=RT01
"""Return or set the x-axis range."""
return self.GetXAxisRange()
@x_axis_range.setter
def x_axis_range(self, value: tuple[float, float]): # numpydoc ignore=GL08
self.SetXAxisRange(value)
self._update_x_labels()
@property
def y_axis_range(self) -> tuple[float, float]: # numpydoc ignore=RT01
"""Return or set the y-axis range."""
return self.GetYAxisRange()
@y_axis_range.setter
def y_axis_range(self, value: tuple[float, float]): # numpydoc ignore=GL08
self.SetYAxisRange(value)
self._update_y_labels()
@property
def z_axis_range(self) -> tuple[float, float]: # numpydoc ignore=RT01
"""Return or set the z-axis range."""
return self.GetZAxisRange()
@z_axis_range.setter
def z_axis_range(self, value: tuple[float, float]): # numpydoc ignore=GL08
self.SetZAxisRange(value)
self._update_z_labels()
@property
def label_offset(self) -> float: # numpydoc ignore=RT01
"""Return or set the distance between labels and the axis."""
return self.GetLabelOffset()
@label_offset.setter
def label_offset(self, offset: float): # numpydoc ignore=GL08
self.SetLabelOffset(offset)
@property
def title_offset(self) -> float: # numpydoc ignore=RT01
"""Return or set the distance between title and labels."""
return self.GetTitleOffset()
@title_offset.setter
def title_offset(self, offset: float): # numpydoc ignore=GL08
self.SetTitleOffset(offset)
@property
def camera(self) -> pyvista.Camera: # numpydoc ignore=RT01
"""Return or set the camera that performs scaling and translation."""
return self.GetCamera()
@camera.setter
def camera(self, camera: pyvista.Camera): # numpydoc ignore=GL08
self.SetCamera(camera)
@property
def x_axis_minor_tick_visibility(self) -> bool: # numpydoc ignore=RT01
"""Return or set visibility of the x-axis minior tick."""
return bool(self.GetXAxisMinorTickVisibility())
@x_axis_minor_tick_visibility.setter
def x_axis_minor_tick_visibility(self, value: bool): # numpydoc ignore=GL08
self.SetXAxisMinorTickVisibility(value)
@property
def y_axis_minor_tick_visibility(self) -> bool: # numpydoc ignore=RT01
"""Return or set visibility of the y-axis minior tick."""
return bool(self.GetYAxisMinorTickVisibility())
@y_axis_minor_tick_visibility.setter
def y_axis_minor_tick_visibility(self, value: bool): # numpydoc ignore=GL08
self.SetYAxisMinorTickVisibility(value)
@property
def z_axis_minor_tick_visibility(self) -> bool: # numpydoc ignore=RT01
"""Return or set visibility of the z-axis minior tick."""
return bool(self.GetZAxisMinorTickVisibility())
@z_axis_minor_tick_visibility.setter
def z_axis_minor_tick_visibility(self, value: bool): # numpydoc ignore=GL08
self.SetZAxisMinorTickVisibility(value)
@property
def x_label_visibility(self) -> bool: # numpydoc ignore=RT01
"""Return or set the visibility of the x-axis labels."""
return self._x_label_visibility
@x_label_visibility.setter
def x_label_visibility(self, value: bool): # numpydoc ignore=GL08
self._x_label_visibility = bool(value)
self._update_x_labels()
@property
def y_label_visibility(self) -> bool: # numpydoc ignore=RT01
"""Return or set the visibility of the y-axis labels."""
return self._y_label_visibility
@y_label_visibility.setter
def y_label_visibility(self, value: bool): # numpydoc ignore=GL08
self._y_label_visibility = bool(value)
self._update_y_labels()
@property
def z_label_visibility(self) -> bool: # numpydoc ignore=RT01
"""Return or set the visibility of the z-axis labels."""
return self._z_label_visibility
@z_label_visibility.setter
def z_label_visibility(self, value: bool): # numpydoc ignore=GL08
self._z_label_visibility = bool(value)
self._update_z_labels()
@property
def x_axis_visibility(self) -> bool: # numpydoc ignore=RT01
"""Return or set the visibility of the x-axis."""
return bool(self.GetXAxisVisibility())
@x_axis_visibility.setter
def x_axis_visibility(self, value: bool): # numpydoc ignore=GL08
self.SetXAxisVisibility(value)
@property
def y_axis_visibility(self) -> bool: # numpydoc ignore=RT01
"""Return or set the visibility of the y-axis."""
return bool(self.GetYAxisVisibility())
@y_axis_visibility.setter
def y_axis_visibility(self, value: bool): # numpydoc ignore=GL08
self.SetYAxisVisibility(value)
@property
def z_axis_visibility(self) -> bool: # numpydoc ignore=RT01
"""Return or set the visibility of the y-axis."""
return bool(self.GetZAxisVisibility())
@z_axis_visibility.setter
def z_axis_visibility(self, value: bool): # numpydoc ignore=GL08
self.SetZAxisVisibility(value)
@property
def x_label_format(self) -> str: # numpydoc ignore=RT01
"""Return or set the label of the x-axis."""
return self.GetXLabelFormat()
@x_label_format.setter
def x_label_format(self, value: str): # numpydoc ignore=GL08
self.SetXLabelFormat(value)
self._update_x_labels()
@property
def y_label_format(self) -> str: # numpydoc ignore=RT01
"""Return or set the label of the y-axis."""
return self.GetYLabelFormat()
@y_label_format.setter
def y_label_format(self, value: str): # numpydoc ignore=GL08
self.SetYLabelFormat(value)
self._update_y_labels()
@property
def z_label_format(self) -> str: # numpydoc ignore=RT01
"""Return or set the label of the z-axis."""
return self.GetZLabelFormat()
@z_label_format.setter
def z_label_format(self, value: str): # numpydoc ignore=GL08
self.SetZLabelFormat(value)
self._update_z_labels()
@property
def x_title(self) -> str: # numpydoc ignore=RT01
"""Return or set the title of the x-axis."""
return self._x_title
@x_title.setter
def x_title(self, value: str): # numpydoc ignore=GL08
self._x_title = value
self._update_x_labels()
@property
def y_title(self) -> str: # numpydoc ignore=RT01
"""Return or set the title of the y-axis."""
return self._y_title
@y_title.setter
def y_title(self, value: str): # numpydoc ignore=GL08
self._y_title = value
self._update_y_labels()
@property
def z_title(self) -> str: # numpydoc ignore=RT01
"""Return or set the title of the z-axis."""
return self._z_title
@z_title.setter
def z_title(self, value: str): # numpydoc ignore=GL08
self._z_title = value
self._update_z_labels()
@property
def use_2d_mode(self) -> bool: # numpydoc ignore=RT01
"""Use the 2d render mode.
This can be enabled for smoother plotting.
"""
return bool(self.GetUse2DMode())
@use_2d_mode.setter
def use_2d_mode(self, value: bool): # numpydoc ignore=GL08
self.SetUse2DMode(value)
@property
def n_xlabels(self): # numpydoc ignore=RT01
"""Number of labels on the x-axis."""
return self._n_xlabels
@n_xlabels.setter
def n_xlabels(self, value: int): # numpydoc ignore=GL08
self._n_xlabels = value
self._update_x_labels()
@property
def n_ylabels(self): # numpydoc ignore=RT01
"""Number of labels on the y-axis."""
return self._n_ylabels
@n_ylabels.setter
def n_ylabels(self, value: int): # numpydoc ignore=GL08
self._n_ylabels = value
self._update_y_labels()
@property
def n_zlabels(self): # numpydoc ignore=RT01
"""Number of labels on the z-axis."""
return self._n_zlabels
@n_zlabels.setter
def n_zlabels(self, value: int): # numpydoc ignore=GL08
self._n_zlabels = value
self._update_z_labels()
def _update_labels(self):
"""Update all labels."""
self._update_x_labels()
self._update_y_labels()
self._update_z_labels()
def _update_x_labels(self):
"""Regenerate x-axis labels."""
if self.x_axis_visibility:
self.SetXTitle(self._x_title)
if self._x_label_visibility:
self.SetAxisLabels(
0,
make_axis_labels(*self.x_axis_range, self.n_xlabels, self.x_label_format),
)
else:
self.SetAxisLabels(0, self._empty_str)
else:
self.SetXTitle('')
self.SetAxisLabels(0, self._empty_str)
def _update_y_labels(self):
"""Regenerate y-axis labels."""
if self.y_axis_visibility:
self.SetYTitle(self._y_title)
if self._y_label_visibility:
self.SetAxisLabels(
1,
make_axis_labels(*self.y_axis_range, self.n_ylabels, self.y_label_format),
)
else:
self.SetAxisLabels(1, self._empty_str)
else:
self.SetYTitle('')
self.SetAxisLabels(1, self._empty_str)
def _update_z_labels(self):
"""Regenerate z-axis labels."""
if self.z_axis_visibility:
self.SetZTitle(self._z_title)
if self._z_label_visibility:
self.SetAxisLabels(
2,
make_axis_labels(*self.z_axis_range, self.n_zlabels, self.z_label_format),
)
else:
self.SetAxisLabels(2, self._empty_str)
else:
self.SetZTitle('')
self.SetAxisLabels(2, self._empty_str)
@property
def x_labels(self) -> list[str]: # numpydoc ignore=RT01
"""Return the x-axis labels."""
return convert_string_array(self.GetAxisLabels(0))
@property
def y_labels(self) -> list[str]: # numpydoc ignore=RT01
"""Return the y-axis labels."""
return convert_string_array(self.GetAxisLabels(1))
@property
def z_labels(self) -> list[str]: # numpydoc ignore=RT01
"""Return the z-axis labels."""
return convert_string_array(self.GetAxisLabels(2))
def update_bounds(self, bounds):
"""Update the bounds of this actor.
Unlike the :attr:`CubeAxesActor.bounds` attribute, updating the bounds
also updates the axis labels.
Parameters
----------
bounds : sequence[float]
Bounds in the form of ``[xmin, xmax, ymin, ymax, zmin, zmax]``.
"""
self.bounds = bounds
|