diff --git a/manim/mobject/geometry.py b/manim/mobject/geometry.py index f4b1be0307..760677a3be 100644 --- a/manim/mobject/geometry.py +++ b/manim/mobject/geometry.py @@ -1561,7 +1561,8 @@ def coordinate_label( self, integer_labels: bool = True, n_dim: int = 2, - color: str = WHITE, + color: Optional[Color] = None, + **kwargs, ): """Creates a label based on the coordinates of the vector. @@ -1572,24 +1573,32 @@ def coordinate_label( n_dim The number of dimensions of the vector. color - The color of the label. + Sets the color of label, optional. + kwargs + Additional arguments to be passed to :class:`~.Matrix`. Examples -------- - .. manim VectorCoordinateLabel + .. manim:: VectorCoordinateLabel :save_last_frame: class VectorCoordinateLabel(Scene): def construct(self): plane = NumberPlane() - vect_1 = Vector([1, 2]) - vect_2 = Vector([-3, -2]) - label_1 = vect1.coordinate_label() - label_2 = vect2.coordinate_label(color=YELLOW) + vec_1 = Vector([1, 2]) + vec_2 = Vector([-3, -2]) + label_1 = vec_1.coordinate_label() + label_2 = vec_2.coordinate_label(color=YELLOW) + + self.add(plane, vec_1, vec_2, label_1, label_2) + + Returns + ------- + :class:`~.Matrix` - self.add(plane, vect_1, vect_2, label_1, label_2) + The label. """ # avoiding circular imports from .matrix import Matrix @@ -1599,8 +1608,7 @@ def construct(self): vect = np.round(vect).astype(int) vect = vect[:n_dim] vect = vect.reshape((n_dim, 1)) - - label = Matrix(vect) + label = Matrix(vect, **kwargs) label.scale(LARGE_BUFF - 0.2) shift_dir = np.array(self.get_end()) @@ -1609,7 +1617,8 @@ def construct(self): else: # Pointing left shift_dir -= label.get_right() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * RIGHT label.shift(shift_dir) - label.set_color(color) + if color is not None: + label.set_color(color) return label diff --git a/manim/mobject/matrix.py b/manim/mobject/matrix.py index 3f6037d6d2..450dbcd99e 100644 --- a/manim/mobject/matrix.py +++ b/manim/mobject/matrix.py @@ -38,6 +38,7 @@ def construct(self): import itertools as it +from typing import Iterable, Sequence, Type import numpy as np @@ -126,48 +127,53 @@ def construct(self): def __init__( self, - matrix, - v_buff=0.8, - h_buff=1.3, - bracket_h_buff=MED_SMALL_BUFF, - bracket_v_buff=MED_SMALL_BUFF, - add_background_rectangles_to_entries=False, - include_background_rectangle=False, - element_to_mobject=MathTex, - element_to_mobject_config={}, - element_alignment_corner=DR, - left_bracket="[", - right_bracket="]", + matrix: Iterable, + v_buff: float = 0.8, + h_buff: float = 1.3, + bracket_h_buff: float = MED_SMALL_BUFF, + bracket_v_buff: float = MED_SMALL_BUFF, + add_background_rectangles_to_entries: bool = False, + include_background_rectangle: bool = False, + element_to_mobject: Type[MathTex] = MathTex, + element_to_mobject_config: dict = {}, + element_alignment_corner: Sequence[float] = DR, + left_bracket: str = "[", + right_bracket: str = "]", + bracket_config: dict = {}, **kwargs, ): """ Parameters ---------- - matrix : :class:`typing.Iterable` - A numpy 2d array or list of lists - v_buff : :class:`float`, optional - vertical buffer, by default 0.8 - h_buff : :class:`float`, optional - horizontal buffer, by default 1.3 - bracket_h_buff : :class:`float`, optional - bracket horizontal buffer, by default MED_SMALL_BUFF - bracket_v_buff : :class:`float`, optional - bracket vertical buffer, by default MED_SMALL_BUFF - add_background_rectangles_to_entries : :class:`bool`, optional - `True` if should add backgraound rectangles to entries, by default False - include_background_rectangle : :class:`bool`, optional - `True` if should include background rectangle, by default False - element_to_mobject : :class:`~.Mobject`, optional - element to mobject, by default MathTex - element_to_mobject_config : Dict[:class:`str`, :class:`~.Mobject`], optional - element to mobject config, by default {} - element_alignment_corner : :class:`np.ndarray`, optional - the element alignment corner, by default DR - left_bracket : :class:`str`, optional - the left bracket type, by default "[" - right_bracket : :class:`str`, optional - the right bracket type, by default "]" + matrix + A numpy 2d array or list of lists. + v_buff + Vertical distance between elements, by default 0.8. + h_buff + Horizontal distance between elements, by default 1.3. + bracket_h_buff + Distance of the brackets from the matrix, by default ``MED_SMALL_BUFF``. + bracket_v_buff + Height of the brackets, by default ``MED_SMALL_BUFF``. + add_background_rectangles_to_entries + ``True`` if should add backgraound rectangles to entries, by default ``False``. + include_background_rectangle + ``True`` if should include background rectangle, by default ``False``. + element_to_mobject + The mobject class used to construct the elements, by default :class:`~.MathTex`. + element_to_mobject_config + Additional arguments to be passed to the constructor in ``element_to_mobject``, + by default ``{}``. + element_alignment_corner + The corner to which elements are aligned, by default ``DR``. + left_bracket + The left bracket type, by default ``"["``. + right_bracket + The right bracket type, by default ``"]"``. + bracket_config + Additional arguments to be passed to :class:`~.MathTex` when constructing + the brackets. """ @@ -187,7 +193,7 @@ def __init__( self.organize_mob_matrix(mob_matrix) self.elements = VGroup(*it.chain(*mob_matrix)) self.add(self.elements) - self.add_brackets(self.left_bracket, self.right_bracket) + self.add_brackets(self.left_bracket, self.right_bracket, **bracket_config) self.center() self.mob_matrix = mob_matrix if self.add_background_rectangles_to_entries: @@ -217,7 +223,7 @@ def organize_mob_matrix(self, matrix): ) return self - def add_brackets(self, left="[", right="]"): + def add_brackets(self, left="[", right="]", **kwargs): """Used internally. Adds the brackets to the Matrix mobject. See Latex document for various bracket types. @@ -235,7 +241,7 @@ def add_brackets(self, left="[", right="]"): The current matrix object (self). """ - bracket_pair = MathTex(left, right) + bracket_pair = MathTex(left, right, **kwargs) bracket_pair.scale(2) bracket_pair.stretch_to_fit_height(self.height + 2 * self.bracket_v_buff) l_bracket, r_bracket = bracket_pair.split()