Graphs
Axes, curves drawn from a Python function, number lines and grids.
Axes and a curve
from pymations import *
axes = Axes(x_range=[-3, 3, 1], y_range=[0, 9, 3], x_length=8, y_length=5)
parabola = axes.plot(lambda x: x ** 2, color=BLUE)
label = axes.get_graph_label(parabola, "y = x^2", x_val=2)
play(Create(axes))
play(Create(parabola), Write(label))
wait()
x_range=[start, end, step]is the numbers the x axis covers, and how far apart its ticks are.y_rangeis the same for y.x_lengthandy_lengthare how big the axes are on screen, in picture units. Without them, axes are 12 wide and 6 tall.tips=Falseleaves off the arrow tips.axis_config={"color": GREEN}colours both axes.
axes.plot(f) draws the function f across the x range. The function gets an x and returns a y; lambda x: x ** 2 is a short way to write one. np.sin, np.cos, np.exp and np.sqrt are ready to use. axes.plot(f, x_range=[0, 2]) draws only part of it.
Points on the axes
The axes have their own numbers, which are not picture units. axes.c2p(x, y) ("coordinates to point") turns graph numbers into a place in the picture:
from pymations import *
axes = Axes(x_range=[0, 5, 1], y_range=[0, 5, 1], x_length=6, y_length=6)
dot = Dot(axes.c2p(3, 2), color=YELLOW)
label = MathTex("(3, 2)").next_to(dot, UR, buff=0.1)
play(Create(axes))
play(GrowFromCenter(dot), Write(label))
wait()
| Helper | What it gives |
|---|---|
axes.c2p(x, y) | The place in the picture for the graph point (x, y) |
axes.i2gp(x, graph) | The place on graph above x |
axes.get_graph_label(graph, "f(x)", x_val=2) | A maths label beside the curve, at x_val |
axes.get_axis_labels("x", "y") | Labels at the ends of the two axes |
axes.x_axis, axes.y_axis | Each axis on its own |
Curves drawn by a parameter
For a curve that is not "y of x", such as a circle or a spiral, give a function of t that returns a point:
from pymations import *
axes = Axes(x_range=[-2, 2, 1], y_range=[-2, 2, 1], x_length=6, y_length=6)
spiral = axes.plot_parametric_curve(
lambda t: [t * np.cos(t) / 6, t * np.sin(t) / 6, 0],
t_range=[0, 4 * PI],
color=ORANGE,
)
play(Create(axes))
play(Create(spiral), run_time=3)
Number lines and grids
from pymations import *
plane = NumberPlane()
line = NumberLine(x_range=[-5, 5, 1], include_numbers=True).shift(DOWN * 3)
play(Create(plane), run_time=2)
play(Create(line))
wait()
NumberPlane()is a grid of blue lines covering the whole picture, with the axes in white. It has the samex_range,y_rangeandc2pasAxes.NumberLine(x_range=[-5, 5, 1])is one line with ticks.include_numbers=Truewrites the numbers under the ticks;line.n2p(2)is the place of the number 2.
Related
- Tutorial: plotting a graph
- Placing things
- Animations:
Createdraws a curve from its start,MoveAlongPathmoves a dot along it