Location>code7788 >text

manim learning-by-doing - generalized 2D coordinate system

Popularity:245 ℃/2024-10-30 13:47:25

Manim(used form a nominal expression)Axesobject is the generic coordinate system object, introduced a few posts ago in thenumber lineand variouscoordinate planeIt's all inheritance.AxesObject.

AxesThe main role of the object is to create and manage 2D axes for the various needs we have when animating math.

Specifically.AxesObjects can help:

  1. Defining the coordinate system: Define a well-defined coordinate system and create axes that meet your needs by setting the range, step, etc. of the x-axis and y-axis.
  2. Customized Axis Styles: You can set the color of the axes, the length of the tick marks, the length of the arrows, and other attributes, as well as control whether or not to display tick marks, labels, and comments.
  3. Plotting function curves: Plot a variety of customized function curves, such as quadratic, trigonometric, exponential functions, etc.
  4. Add graphics and labels: graphical elements (e.g., circles, triangles, etc.) and labels (e.g., axis labels, headings, etc.) can be added

1. Main parameters

AxesThe main parameters are:

Parameter name typology clarification
x_range Sequence[float] horizontal coordinate axisRange, Interval
y_range Sequence[float] vertical coordinate axisRange, Interval
x_length float horizontal axiswidths
y_length float vertical axisheights
axis_config dict General configuration options for coordinate axes
x_axis_config dict horizontal axisConfiguration options for the
y_axis_config dict vertical axisConfiguration options for the
tips bool horizontal axisand the right side of thevertical axisWhether or not arrows are displayed at the top

parametersaxis_configcap (a poem)x_axis_configy_axis_configThe configurable items in the

The difference is thataxis_configis configured for the entire axis.

x_axis_configcap (a poem)y_axis_configare configured for the horizontal and vertical axes respectively.

If they set the same item, then thex_axis_configcap (a poem)y_axis_configwill override the corresponding settings in axis_config.

There are many items that can be set for the axes, and the following is a list of some common ones.

  • stroke_color: Setting the color of coordinate axes
  • stroke_width(orline_width): Set the width of the coordinate axis
  • include_numbers: Whether to display numbers on the axes
  • numbers_to_include: Specifies a list of numbers to be displayed.
  • numbers_with_elongated_ticks: Specifies a list of numbers to lengthen the scale. Used to highlight specific scales
  • include_ticks: Whether or not the scale is displayed
  • tick_size: Setting the length of the scale
  • include_tip: Whether or not to display arrows at the end of axes
  • tip_length: Sets the length of the arrow. Wheninclude_tipbecause ofTrueThis option is available when
  • font_size: Setting the font size of numbers on the axes
  • label_direction: Setting the orientation of the axis labels
  • scaling: Setting the scale of the axes, e.g. linear scale, logarithmic scale, etc.

2. Main approaches

AxesThe main purpose of the method is to dynamically obtain various information on the axes and to graph functions on the axes.

Some of the more used ones are:

name (of a thing) clarification
coords_to_point Converting coordinates to the position of a point on the screen
point_to_coords Converts the position of points on the screen to coordinates
get_axes Get information about each axis
plot_line_graph Graphing functions in axes

3. Examples of use

Below is a step-by-step demonstration, from simple to complex examples, of how to use theAxesObject.

3.1 Basic coordinate system

utilizationAxesobject creates a basic two-dimensional coordinate system, setting thex-axiscap (a poem)y-axisrange and displays the axes of thedigital (electronics etc)cap (a poem)graduated line

The following example setupx-axisrealm[-5,5]y-axisrealm[-3,3]on the coordinate axesInterval of 1Displays numbers and tick marks.

# The x-axis ranges from -5 to 5 at intervals of 1
# The y-axis ranges from -3 to 3 at intervals of 1
# Display the numbers on the axes
Axes(
    x_range=[-5, 5, 1],
    x_range=[-5, 5, 1], y_range=[-3, 3, 1], # display the numbers on the axes.
    axis_config={"include_numbers": True},
)

3.2. Customizing Axis Styles

Customize the style of the axes, including the color of the axes, the width of the lines, the length of the tick marks, and whether or not to display arrows.

The following example willx-axisSet to red with a line width of 2;y-axisis set to blue, the line width is 1.5, and thex-axiscap (a poem)y-axisThe ends all show different arrows.

axes = Axes(
    x_range=[-5, 5, 1],
    y_range=[-3, 3, 1],
    x_axis_config={
        "color": RED,
        "stroke_width": 5,
        "include_tip": True, # Show arrows
        "tip_shape": ArrowSquareTip,
    },
    y_axis_config={
        "color": BLUE,
        "stroke_width": 1.5,
        "include_tip": True,
        "tip_shape": StealthTip,
    },
    axis_config={
        # Lengthening the scale for specific numbers
        "numbers_with_elongated_ticks": [0, -3, 3],
        "font_size": 20,
    },
)

3.3 Plotting function curves

Plot a function curve on the coordinate system.

For example, the following example draws a parabola ($ y=x^2)\() and a trigonometric curve (\) y=\sin(\theta) $)。

axes = Axes(
    x_range=[-5, 5, 1],
    y_range=[-5, 5, 1],
    x_axis_config={
        "color": RED,
    },
    y_axis_config={
        "color": BLUE,
    },
    axis_config={
        "include_numbers": True,
        "font_size": 20,
        "include_tip": True,
        "tip_shape": StealthTip,
        "tip_length": 2,
    },
)

# parabola
func = lambda x: x**2
graph = (func, x_range=[-2, 2], color=GREEN)
(Create(graph), run_time=run_time)

# trigonometric function
func = lambda x: (x)
graph = (func, x_range=[-5, 5], color=YELLOW)
(Create(graph), run_time=run_time)

3.4 Adding tags and notes

Add labels to the axes and comments to the function curves to explain the meaning of the graphs.

The following example is based on the function curves from the previous section, labeled with the names of the axesxcap (a poem)y, and select a point on the parabola and label the coordinates of the point.

axes = Axes(
    x_range=[-5, 5, 1],
    y_range=[-5, 5, 1],
    x_axis_config={
        "color": RED,
    },
    y_axis_config={
        "color": BLUE,
    },
    axis_config={
        "include_numbers": True,
        "font_size": 20,
        "include_tip": True,
        "tip_shape": StealthTip,
        "tip_length": 2,
    },
)

# plot
func = lambda x: x**2
graph = (func, x_range=[-2, 2], color=GREEN, stroke_width=2)

# Add Tags
x_label = axes.get_x_axis_label(
    MathTex("x", font_size=25, color=RED),
    direction=UP,
)
y_label = axes.get_y_axis_label(
    MathTex("y", font_size=25, color=BLUE),
    direction=RIGHT,
)

# Marking a point
x = 1.5
y = x * x
d = Dot(axes.coords_to_point(x, y), color=YELLOW)
txt = Matrix([[x], [y]]).scale(0.5).next_to(d.get_center(), RIGHT)

4. Annexes

The code in the article is just an extract of the key parts, the complete code is shared on a web disk (),

Download at.Full Code (Access code: 6872)