Skip to content

Commit d7e7d82

Browse files
Show example for :meth:~.Vector.coordinate_label and add more customization for :class:~.Matrix. (#2138)
* fix docs * fix doc * apply fixes * improve typing * weird change * improve typing + docs * forgot three params * fix TYPE_CHECKING return val * label -> color * apply suggestions from code review * attempt at fixing duplicated parameters * attempt to fix duplicated paramters pt.2 * attempt to fix duplicated paramters pt.3 final Co-authored-by: icedcoffeeee <[email protected]>
1 parent 7fc681d commit d7e7d82

File tree

2 files changed

+65
-50
lines changed

2 files changed

+65
-50
lines changed

manim/mobject/geometry.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,8 @@ def coordinate_label(
15611561
self,
15621562
integer_labels: bool = True,
15631563
n_dim: int = 2,
1564-
color: str = WHITE,
1564+
color: Optional[Color] = None,
1565+
**kwargs,
15651566
):
15661567
"""Creates a label based on the coordinates of the vector.
15671568
@@ -1572,24 +1573,32 @@ def coordinate_label(
15721573
n_dim
15731574
The number of dimensions of the vector.
15741575
color
1575-
The color of the label.
1576+
Sets the color of label, optional.
1577+
kwargs
1578+
Additional arguments to be passed to :class:`~.Matrix`.
15761579
15771580
Examples
15781581
--------
15791582
1580-
.. manim VectorCoordinateLabel
1583+
.. manim:: VectorCoordinateLabel
15811584
:save_last_frame:
15821585
15831586
class VectorCoordinateLabel(Scene):
15841587
def construct(self):
15851588
plane = NumberPlane()
15861589
1587-
vect_1 = Vector([1, 2])
1588-
vect_2 = Vector([-3, -2])
1589-
label_1 = vect1.coordinate_label()
1590-
label_2 = vect2.coordinate_label(color=YELLOW)
1590+
vec_1 = Vector([1, 2])
1591+
vec_2 = Vector([-3, -2])
1592+
label_1 = vec_1.coordinate_label()
1593+
label_2 = vec_2.coordinate_label(color=YELLOW)
1594+
1595+
self.add(plane, vec_1, vec_2, label_1, label_2)
1596+
1597+
Returns
1598+
-------
1599+
:class:`~.Matrix`
15911600
1592-
self.add(plane, vect_1, vect_2, label_1, label_2)
1601+
The label.
15931602
"""
15941603
# avoiding circular imports
15951604
from .matrix import Matrix
@@ -1599,8 +1608,7 @@ def construct(self):
15991608
vect = np.round(vect).astype(int)
16001609
vect = vect[:n_dim]
16011610
vect = vect.reshape((n_dim, 1))
1602-
1603-
label = Matrix(vect)
1611+
label = Matrix(vect, **kwargs)
16041612
label.scale(LARGE_BUFF - 0.2)
16051613

16061614
shift_dir = np.array(self.get_end())
@@ -1609,7 +1617,8 @@ def construct(self):
16091617
else: # Pointing left
16101618
shift_dir -= label.get_right() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * RIGHT
16111619
label.shift(shift_dir)
1612-
label.set_color(color)
1620+
if color is not None:
1621+
label.set_color(color)
16131622
return label
16141623

16151624

manim/mobject/matrix.py

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def construct(self):
3838

3939

4040
import itertools as it
41+
from typing import Iterable, Sequence, Type
4142

4243
import numpy as np
4344

@@ -126,48 +127,53 @@ def construct(self):
126127

127128
def __init__(
128129
self,
129-
matrix,
130-
v_buff=0.8,
131-
h_buff=1.3,
132-
bracket_h_buff=MED_SMALL_BUFF,
133-
bracket_v_buff=MED_SMALL_BUFF,
134-
add_background_rectangles_to_entries=False,
135-
include_background_rectangle=False,
136-
element_to_mobject=MathTex,
137-
element_to_mobject_config={},
138-
element_alignment_corner=DR,
139-
left_bracket="[",
140-
right_bracket="]",
130+
matrix: Iterable,
131+
v_buff: float = 0.8,
132+
h_buff: float = 1.3,
133+
bracket_h_buff: float = MED_SMALL_BUFF,
134+
bracket_v_buff: float = MED_SMALL_BUFF,
135+
add_background_rectangles_to_entries: bool = False,
136+
include_background_rectangle: bool = False,
137+
element_to_mobject: Type[MathTex] = MathTex,
138+
element_to_mobject_config: dict = {},
139+
element_alignment_corner: Sequence[float] = DR,
140+
left_bracket: str = "[",
141+
right_bracket: str = "]",
142+
bracket_config: dict = {},
141143
**kwargs,
142144
):
143145
"""
144146
145147
Parameters
146148
----------
147-
matrix : :class:`typing.Iterable`
148-
A numpy 2d array or list of lists
149-
v_buff : :class:`float`, optional
150-
vertical buffer, by default 0.8
151-
h_buff : :class:`float`, optional
152-
horizontal buffer, by default 1.3
153-
bracket_h_buff : :class:`float`, optional
154-
bracket horizontal buffer, by default MED_SMALL_BUFF
155-
bracket_v_buff : :class:`float`, optional
156-
bracket vertical buffer, by default MED_SMALL_BUFF
157-
add_background_rectangles_to_entries : :class:`bool`, optional
158-
`True` if should add backgraound rectangles to entries, by default False
159-
include_background_rectangle : :class:`bool`, optional
160-
`True` if should include background rectangle, by default False
161-
element_to_mobject : :class:`~.Mobject`, optional
162-
element to mobject, by default MathTex
163-
element_to_mobject_config : Dict[:class:`str`, :class:`~.Mobject`], optional
164-
element to mobject config, by default {}
165-
element_alignment_corner : :class:`np.ndarray`, optional
166-
the element alignment corner, by default DR
167-
left_bracket : :class:`str`, optional
168-
the left bracket type, by default "["
169-
right_bracket : :class:`str`, optional
170-
the right bracket type, by default "]"
149+
matrix
150+
A numpy 2d array or list of lists.
151+
v_buff
152+
Vertical distance between elements, by default 0.8.
153+
h_buff
154+
Horizontal distance between elements, by default 1.3.
155+
bracket_h_buff
156+
Distance of the brackets from the matrix, by default ``MED_SMALL_BUFF``.
157+
bracket_v_buff
158+
Height of the brackets, by default ``MED_SMALL_BUFF``.
159+
add_background_rectangles_to_entries
160+
``True`` if should add backgraound rectangles to entries, by default ``False``.
161+
include_background_rectangle
162+
``True`` if should include background rectangle, by default ``False``.
163+
element_to_mobject
164+
The mobject class used to construct the elements, by default :class:`~.MathTex`.
165+
element_to_mobject_config
166+
Additional arguments to be passed to the constructor in ``element_to_mobject``,
167+
by default ``{}``.
168+
element_alignment_corner
169+
The corner to which elements are aligned, by default ``DR``.
170+
left_bracket
171+
The left bracket type, by default ``"["``.
172+
right_bracket
173+
The right bracket type, by default ``"]"``.
174+
bracket_config
175+
Additional arguments to be passed to :class:`~.MathTex` when constructing
176+
the brackets.
171177
172178
"""
173179

@@ -187,7 +193,7 @@ def __init__(
187193
self.organize_mob_matrix(mob_matrix)
188194
self.elements = VGroup(*it.chain(*mob_matrix))
189195
self.add(self.elements)
190-
self.add_brackets(self.left_bracket, self.right_bracket)
196+
self.add_brackets(self.left_bracket, self.right_bracket, **bracket_config)
191197
self.center()
192198
self.mob_matrix = mob_matrix
193199
if self.add_background_rectangles_to_entries:
@@ -217,7 +223,7 @@ def organize_mob_matrix(self, matrix):
217223
)
218224
return self
219225

220-
def add_brackets(self, left="[", right="]"):
226+
def add_brackets(self, left="[", right="]", **kwargs):
221227
"""Used internally. Adds the brackets to the Matrix mobject.
222228
223229
See Latex document for various bracket types.
@@ -235,7 +241,7 @@ def add_brackets(self, left="[", right="]"):
235241
The current matrix object (self).
236242
"""
237243

238-
bracket_pair = MathTex(left, right)
244+
bracket_pair = MathTex(left, right, **kwargs)
239245
bracket_pair.scale(2)
240246
bracket_pair.stretch_to_fit_height(self.height + 2 * self.bracket_v_buff)
241247
l_bracket, r_bracket = bracket_pair.split()

0 commit comments

Comments
 (0)