manim.scene.graph_scene.GraphScene

class GraphScene(x_min=- 1, x_max=10, x_axis_width=9, x_leftmost_tick=None, x_labeled_nums=None, x_axis_label='$x$', y_min=- 1, y_max=10, y_axis_height=6, y_bottom_tick=None, y_labeled_nums=None, y_axis_label='$y$', axes_color='#888888', graph_origin=array([- 4., - 2.5, 0.]), exclude_zero_label=True, default_graph_colors=['#58C4DD', '#83C167', '#FFFF00'], default_derivative_color='#83C167', default_input_color='#FFFF00', default_riemann_start_color='#58C4DD', default_riemann_end_color='#83C167', area_opacity=0.8, num_rects=50, include_tip=False, x_axis_visibility=True, y_axis_visibility=True, x_label_position=array([1., 1., 0.]), y_label_position=array([1., 1., 0.]), x_axis_config=None, y_axis_config=None, **kwargs)[source]

Bases: manim.scene.scene.Scene

A special scene intended for plotting functions.

Note

This class is in the process of being deprecated. Functionality is currently being ported to Axes.

Deprecated

The class GraphScene has been deprecated since v0.7.0 and is expected to be removed after v0.9.0. Use Axes instead.

Methods

add_T_label

Create a triangle marker with a vertical line from the x-axis to self.v_graph at the given x coordinate x_val.

angle_of_tangent

Returns the angle to the x axis of the tangent to the plotted curve at a particular x-value.

animate_secant_slope_group_change

This method animates the change of the secant slope group from the old secant slope group, into a new secant slope group.

coords_to_point

Maps a pair of coordinates (with respect to the axes) to a point in the scene.

get_animation_integral_bounds_change

This method requires a lot of prerequisites: self.area must be defined from self.get_area() self.left_v_line and self.right_v_line must be defined from self.get_v_line self.left_T_label_group and self.right_T_label_group must be defined from self.add_T_label

get_area

Returns a VGroup of Riemann rectangles sufficiently small enough to visually approximate the area under the graph passed.

get_derivative_graph

Returns the curve of the derivative of the passed graph.

get_graph

This method gets a curve to plot on the graph.

get_graph_label

This method returns a properly positioned label for the passed graph, styled with the passed parameters.

get_riemann_rectangles

This method returns the VGroup() of the Riemann Rectangles for a particular curve.

get_riemann_rectangles_list

Returns a list of Riemann Rectangles.

get_secant_slope_group

This method returns a VGroup of (two lines representing dx and df, the labels for dx and df, and the Secant to the Graph/curve at a particular x value.

get_vertical_line_to_graph

This method returns a Vertical line from the x-axis to the corresponding point on the graph/curve.

get_vertical_lines_to_graph

Obtains multiple lines from the x axis to the Graph/curve.

input_to_graph_point

This method returns a coordinate on the curve given an x_value and a the graph-curve for which the corresponding y value should be found.

point_to_coords

Maps a point in the scene to a pair of coordinates (with respect to the axes)

setup

This method is used internally by Manim to set up the scene for proper use.

setup_axes

This method sets up the axes of the graph.

slope_of_tangent

Returns the slope of the tangent to the plotted curve at a particular x-value.

transform_between_riemann_rects

Transform between groups of Riemann Rectangles.

Attributes

camera

add_T_label(x_val, side=array([1., 0., 0.]), label=None, color='#FFFFFF', animated=False, **kwargs)[source]

Create a triangle marker with a vertical line from the x-axis to self.v_graph at the given x coordinate x_val.

This method adds to the Scene:

  • a Vertical line from the x-axis to the corresponding point on the graph/curve.

  • a small vertical Triangle whose top point lies on the base of the vertical line

  • a MathTex to be a label for the Line and Triangle, at the bottom of the Triangle.

The scene needs to have the graph have the identifier/variable name self.v_graph.

Parameters
  • x_val (float) – The x value at which the secant enters, and intersects the graph for the first time.

  • side (Optional[Sequence[float]]) – A tuple, list, or array identifying the side on which the label should be drawn. For example, [1,0,0] would be RIGHT.

  • label (Optional[str]) – The label to give the vertical line and triangle

  • color (Optional[str]) – The hex color of the label.

  • animated (Optional[bool]) – Whether or not to animate the addition of the T_label

  • **kwargs – Any valid keyword argument of a self.play() call.

angle_of_tangent(x, graph, dx=0.01)[source]

Returns the angle to the x axis of the tangent to the plotted curve at a particular x-value.

Parameters
  • x (Optional[float]) – The x value at which the tangent must touch the curve.

  • graph (ParametricFunction) – The ParametricFunction for which to calculate the tangent.

  • dx (Optional[float]) – The small change in x with which a small change in y will be compared in order to obtain the tangent.

Returns

The angle of the tangent with the x axis.

Return type

float

animate_secant_slope_group_change(secant_slope_group, target_dx=None, target_x=None, run_time=3, added_anims=None, **anim_kwargs)[source]

This method animates the change of the secant slope group from the old secant slope group, into a new secant slope group.

Parameters
  • secant_slope_group (VGroup) – The old secant_slope_group

  • target_dx (Optional[float]) – The new dx value.

  • target_x (Optional[float]) – The new x value at which the secant should be.

  • run_time (Optional[float]) – The run time for this change when animated.

  • added_anims (Optional[Sequence[Animation]]) – Any exta animations that should be played alongside.

  • **anim_kwargs – Any valid kwargs of a self.play() call.

  • NOTE (At least one of target_dx and target_x should be not None.) –

coords_to_point(x, y)[source]

Maps a pair of coordinates (with respect to the axes) to a point in the scene.

The graph is smaller than the scene. Because of this, coordinates in the graph don’t map to coordinates on the scene.

This method returns a scaled coordinate for the scene, given coordinates that correspond to the graph.

Parameters
  • x (float) – The x coordinate with respect to the graph.

  • y (float) – The y coordinate with respect to the graph.

Returns

Coordinates of a point in the scene.

Return type

numpy.ndarray

Examples

Example: SequencePlot

../_images/SequencePlot-1.png
from manim import *

class SequencePlot(GraphScene):
    def __init__(self, **kwargs):
        GraphScene.__init__(
            self,
            y_axis_label=r"Concentration [\%]",
            x_axis_label="Time [s]",
            **kwargs
        )

    def construct(self):
        data = [1, 2, 2, 4, 4, 1, 3]
        self.setup_axes()
        for time, dat in enumerate(data):
            dot = Dot().move_to(self.coords_to_point(time, dat))
            self.add(dot)
get_animation_integral_bounds_change(graph, new_t_min, new_t_max, fade_close_to_origin=True, run_time=1.0)[source]

This method requires a lot of prerequisites: self.area must be defined from self.get_area() self.left_v_line and self.right_v_line must be defined from self.get_v_line self.left_T_label_group and self.right_T_label_group must be defined from self.add_T_label

This method will return a VGroup of new mobjects for each of those, when provided the graph/curve, the new t_min and t_max, the run_time and a bool stating whether or not to fade when close to the origin.

Parameters
  • graph (ParametricFunction) – The graph for which this must be done.

  • new_t_min (float) – The new lower bound.

  • new_t_max (float) – The new upper bound.

  • fade_close_to_origin (Optional[bool]) – Whether or not to fade when close to the origin.

  • run_time (Optional[float]) – The run_time of the animation of this change.

get_area(graph, t_min, t_max, bounded=None, dx_scaling=1, area_color='#FFFFFF')[source]

Returns a VGroup of Riemann rectangles sufficiently small enough to visually approximate the area under the graph passed.

Parameters
  • graph (ParametricFunction) – The graph/curve for which the area needs to be gotten.

  • t_min (float) – The lower bound of x from which to approximate the area.

  • t_max (float) – The upper bound of x until which the area must be approximated.

Returns

The VGroup containing the Riemann Rectangles.

Return type

VGroup

get_derivative_graph(graph, dx=0.01, **kwargs)[source]

Returns the curve of the derivative of the passed graph.

Parameters
  • graph (ParametricFunction) – The graph for which the derivative must be found.

  • dx (Optional[float]) – The small change in x with which a small change in y will be compared in order to obtain the derivative.

  • **kwargs – Any valid keyword argument of ParametricFunction

Returns

The curve of the derivative.

Return type

ParametricFunction

get_graph(func, color=None, x_min=None, x_max=None, **kwargs)[source]

This method gets a curve to plot on the graph.

Parameters
  • func (function) – The function to plot. It’s return value should be the y-coordinate for a given x-coordinate

  • color (Optional[str]) – The string of the RGB color of the curve. in Hexadecimal representation.

  • x_min (Optional[float]) – The lower x_value from which to plot the curve.

  • x_max (Optional[float]) – The higher x_value until which to plot the curve.

  • **kwargs – Any valid keyword arguments of ParametricFunction.

Returns

The Parametric Curve for the function passed.

Return type

ParametricFunction

get_graph_label(graph, label='f(x)', x_val=None, direction=array([1., 0., 0.]), buff=0.25, color=None)[source]

This method returns a properly positioned label for the passed graph, styled with the passed parameters.

Parameters
  • graph (ParametricFunction) – The curve of the function plotted.

  • label (Optional[str]) – The label for the function’s curve.

  • x_val (Optional[float]) – The x_value with which the label should be aligned.

  • direction (Sequence[float]) – The cartesian position, relative to the curve that the label will be at. e.g LEFT, RIGHT

  • buff (Optional[float]) – The buffer space between the curve and the label

  • color (Optional[str]) – The color of the label.

Returns

The LaTeX of the passed ‘label’ parameter

Return type

MathTex

get_riemann_rectangles(graph, x_min=None, x_max=None, dx=0.1, input_sample_type='left', bounded_graph=None, stroke_width=1, stroke_color='#000000', fill_opacity=1, start_color=None, end_color=None, show_signed_area=True, width_scale_factor=1.001)[source]

This method returns the VGroup() of the Riemann Rectangles for a particular curve.

Parameters
  • graph (ParametricFunction) – The graph whose area needs to be approximated by the Riemann Rectangles.

  • x_min (Optional[float]) – The lower bound from which to start adding rectangles

  • x_max (Optional[float]) – The upper bound where the rectangles stop.

  • dx (Optional[float]) – The smallest change in x-values that is considered significant.

  • input_sample_type (Optional[str]) – Can be any of “left”, “right” or “center”. Refers to where the sample point for the height of each Riemann Rectangle will be inside the segments of the partition.

  • stroke_width (Optional[float]) – The stroke_width of the border of the rectangles.

  • stroke_color (Optional[str]) – The string of hex colour of the rectangle’s border.

  • fill_opacity (Optional[float]) – The opacity of the rectangles. Takes values from 0 to 1.

  • start_color (Optional[str]) – The hex starting colour for the rectangles, this will, if end_color is a different colour, make a nice gradient.

  • end_color (Optional[str]) – The hex ending colour for the rectangles, this will, if start_color is a different colour, make a nice gradient.

  • show_signed_area (Optional[bool]) – Whether or not to indicate -ve area if curve dips below x-axis.

  • width_scale_factor (Optional[float]) – How much the width of the rectangles are scaled by when transforming.

Returns

A VGroup containing the Riemann Rectangles.

Return type

VGroup

get_riemann_rectangles_list(graph, n_iterations, max_dx=0.5, power_base=2, stroke_width=1, **kwargs)[source]

Returns a list of Riemann Rectangles.

This method returns a list of multiple VGroups of Riemann Rectangles. The initial rectangles are relatively inaccurate, but the closer you get to the end the more accurate the Riemann rectangles become.

Parameters
  • graph (ParametricFunction) – The graph whose area needs to be approximated by the Riemann Rectangles.

  • n_iterations (int) – The number of VGroups of successive accuracy that are needed.

  • max_dx (Optional[float]) – The maximum change in x between two VGroups of Riemann Rectangles

  • power_base (Optional[float]) – Defaults to 2

  • stroke_width (Optional[float]) – The stroke_width of the border of the rectangles.

  • **kwargs – Any valid keyword arguments of get_riemann_rectangles().

Returns

The list of Riemann Rectangles of increasing accuracy.

Return type

List[VGroup]

get_secant_slope_group(x, graph, dx=None, dx_line_color=None, df_line_color=None, dx_label=None, df_label=None, include_secant_line=True, secant_line_color=None, secant_line_length=10)[source]

This method returns a VGroup of (two lines representing dx and df, the labels for dx and df, and the Secant to the Graph/curve at a particular x value.

Parameters
  • x (float) – The x value at which the secant enters, and intersects the graph for the first time.

  • graph (ParametricFunction) – The curve/graph for which the secant must be found.

  • dx (Optional[float]) – The change in x after which the secant exits.

  • dx_line_color (Optional[str]) – The line color for the line that indicates the change in x.

  • df_line_color (Optional[str]) – The line color for the line that indicates the change in y.

  • dx_label (Optional[str]) – The label to be provided for the change in x.

  • df_label (Optional[str]) – The label to be provided for the change in y.

  • include_secant_line (Optional[bool]) – Whether or not to include the secant line in the graph, or just have the df and dx lines and labels.

  • secant_line_color (Optional[str]) – The color of the secant line.

  • secant_line_length (Optional[float]) – How long the secant line should be.

Returns

A group containing the elements dx_line, df_line, and if applicable also dx_label, df_label, secant_line.

Return type

VGroup

get_vertical_line_to_graph(x, graph, line_class=<class 'manim.mobject.geometry.Line'>, **line_kwargs)[source]

This method returns a Vertical line from the x-axis to the corresponding point on the graph/curve.

Parameters
  • x (float) – The x-value at which the line should be placed/calculated.

  • graph (ParametricFunction) – The graph on which the line should extend to.

  • line_class (Line and similar) – The type of line that should be used. Defaults to Line.

  • **line_kwargs – Any valid keyword arguments of the object passed in line_class If line_class is Line, any valid keyword arguments of Line are allowed.

Returns

An object of type passed in line_class Defaults to Line.

Return type

Mobject

get_vertical_lines_to_graph(graph, x_min=None, x_max=None, num_lines=20, **kwargs)[source]

Obtains multiple lines from the x axis to the Graph/curve.

Parameters
  • graph (ParametricFunction) – The graph on which the line should extend to.

  • x_min (Optional[float]) – The lower bound from which lines can appear.

  • x_max (Optional[float]) – The upper bound until which the lines can appear.

  • num_lines (Optional[int]) – The number of lines (evenly spaced) that are needed.

Returns

The VGroup of the evenly spaced lines.

Return type

VGroup

input_to_graph_point(x, graph)[source]

This method returns a coordinate on the curve given an x_value and a the graph-curve for which the corresponding y value should be found.

Parameters
Returns

The array of the coordinates on the graph.

Return type

numpy.ndarray

point_to_coords(point)[source]

Maps a point in the scene to a pair of coordinates (with respect to the axes)

The scene is smaller than the graph. Because of this, coordinates in the graph don’t map to coordinates on the scene.

This method returns a scaled coordinate for the graph, given coordinates that correspond to the scene.

Parameters

point (numpy.ndarray) – Coordinates of a point in the scene.

Returns

The coordinates with respect to the graph.

Return type

Tuple[float]

setup()[source]

This method is used internally by Manim to set up the scene for proper use.

setup_axes(animate=False)[source]

This method sets up the axes of the graph.

Parameters

animate (Optional[bool]) – Whether or not to animate the setting up of the Axes.

slope_of_tangent(*args, **kwargs)[source]

Returns the slope of the tangent to the plotted curve at a particular x-value.

Parameters
  • x (float) – The x value at which the tangent must touch the curve.

  • graph (ParametricFunction) – The ParametricFunction for which to calculate the tangent.

  • dx (Optional[float]) – The small change in x with which a small change in y will be compared in order to obtain the tangent.

Returns

The slope of the tangent with the x axis.

Return type

float

transform_between_riemann_rects(curr_rects, new_rects, **kwargs)[source]

Transform between groups of Riemann Rectangles.

This method is used to transform between two VGroups of Riemann Rectangles, if they were obtained by get_riemann_rectangles or get_riemann_rectangles_list. No animation is returned, and the animation is directly played.

Parameters
  • curr_rects (VGroup) – The current Riemann Rectangles

  • new_rects (VGroup) – The Riemann Rectangles to transform to.

  • **kwargs

Other Parameters

added_anims (Animation) – Any other animations to play simultaneously.