Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Added :meth:`~.CoordinateSystem.get_line_graph` for plotting a line graph (#1385)

Abhijith Muthyala569111011ecb
diff --git a/manim/mobject/coordinate_systems.py b/manim/mobject/coordinate_systems.py
index a99c31748..12025c640 100644
--- a/manim/mobject/coordinate_systems.py
+++ b/manim/mobject/coordinate_systems.py
@@ -4,17 +4,18 @@ __all__ = ["CoordinateSystem", "Axes", "ThreeDAxes", "NumberPlane", "ComplexPlan
import numbers
+from typing import Iterable, Optional
import numpy as np
from .. import config
from ..constants import *
from ..mobject.functions import ParametricFunction
-from ..mobject.geometry import Arrow, Line
+from ..mobject.geometry import Arrow, Dot, Line
from ..mobject.number_line import NumberLine
from ..mobject.svg.tex_mobject import MathTex
-from ..mobject.types.vectorized_mobject import VGroup
-from ..utils.color import BLUE, BLUE_D, LIGHT_GREY, WHITE
+from ..mobject.types.vectorized_mobject import VDict, VGroup, VMobject
+from ..utils.color import BLUE, BLUE_D, LIGHT_GREY, WHITE, YELLOW, Colors
from ..utils.config_ops import merge_dicts_recursively, update_dict_recursively
from ..utils.simple_functions import binary_search
from ..utils.space_ops import angle_of_vector
@@ -212,6 +213,87 @@ class Axes(VGroup, CoordinateSystem):
self.add(self.get_coordinate_labels(x_vals, y_vals))
return self
+ def get_line_graph(
+ self,
+ x_values: Iterable[float],
+ y_values: Iterable[float],
+ z_values: Optional[Iterable[float]] = None,
+ line_color: Colors = YELLOW,
+ add_vertex_dots: bool = True,
+ vertex_dot_radius: float = DEFAULT_DOT_RADIUS,
+ vertex_dot_style: Optional[dict] = None,
+ **kwargs
+ ) -> VDict:
+ """Draws a line graph.
+
+ The graph connects the vertices formed from zipping
+ ``x_values``, ``y_values`` and ``z_values``. Also adds :class:`Dots <.Dot>` at the
+ vertices if ``add_vertex_dots`` is set to ``True``.
+
+ Parameters
+ ----------
+ x_values
+ Iterable of values along the x-axis.
+ y_values
+ Iterable of values along the y-axis.
+ z_values
+ Iterable of values (zeros if z_values is None) along the z-axis.
+ line_color
+ Color for the line graph.
+ add_vertex_dots
+ Whether or not to add :class:`~.Dot` at each vertex.
+ vertex_dot_radius
+ Radius for the :class:`~.Dot` at each vertex.
+ vertex_dot_style
+ Style arguments to be passed into :class:`~.Dot` at each vertex.
+ kwargs
+ Additional arguments to be passed into :class:`~.VMobject`.
+
+ Examples
+ --------
+
+ .. manim:: LineGraphExample
+ :save_last_frame:
+
+ class LineGraphExample(Scene):
+ def construct(self):
+ plane = NumberPlane(x_min=0, x_max=7, y_min=0, y_max=5, axis_config={"width": 7, "include_numbers": True})
+ plane.center()
+ line_graph = plane.get_line_graph(
+ x_values = [0, 1.5, 2, 2.8, 4, 6.25],
+ y_values = [1, 3, 2.25, 4, 2.5, 1.75],
+ line_color=GOLD_E,
+ vertex_dot_style=dict(stroke_width=3, fill_color=PURPLE),
+ stroke_width = 4,
+ )
+ self.add(plane, line_graph)
+ """
+ x_values, y_values = map(np.array, (x_values, y_values))
+ if z_values is None:
+ z_values = np.zeros(x_values.shape)
+
+ line_graph = VDict()
+ graph = VMobject(color=line_color, **kwargs)
+ vertices = [
+ self.coords_to_point(x, y, z)
+ for x, y, z in zip(x_values, y_values, z_values)
+ ]
+ graph.set_points_as_corners(vertices)
+ graph.z_index = -1
+ line_graph["line_graph"] = graph
+
+ if add_vertex_dots:
+ vertex_dot_style = vertex_dot_style or {}
+ vertex_dots = VGroup(
+ *[
+ Dot(point=vertex, radius=vertex_dot_radius, **vertex_dot_style)
+ for vertex in vertices
+ ]
+ )
+ line_graph["vertex_dots"] = vertex_dots
+
+ return line_graph
+
class ThreeDAxes(Axes):
def __init__(