Skip to content

Commit 88a3f63

Browse files
leotrsbehackl
andauthored
# Added examples for GraphScene to docs (#444)
* # added examples for GraphScene * # added another example, where a function is defined outside of construct Co-authored-by: Benjamin Hackl <[email protected]> Co-authored-by: Leo Torres <[email protected]>
2 parents e011f64 + 3df021a commit 88a3f63

File tree

1 file changed

+176
-1
lines changed

1 file changed

+176
-1
lines changed

docs/source/examples/plots_examples.rst

Lines changed: 176 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,182 @@
11
Plotting with manim
22
=================================
33

4+
Examples to illustrate the use of :class:`.GraphScene` in manim.
5+
6+
7+
.. manim:: Plot1
8+
:quality: medium
9+
:save_last_frame:
10+
11+
class Plot1(GraphScene):
12+
def construct(self):
13+
self.setup_axes()
14+
func_graph=self.get_graph(lambda x: np.sin(x))
15+
self.add(func_graph)
16+
17+
.. manim:: Plot2yLabel
18+
:quality: medium
19+
:save_last_frame:
20+
21+
class Plot2yLabel(GraphScene):
22+
CONFIG = {
23+
"y_min": 0,
24+
"y_max": 100,
25+
"y_axis_config": {"tick_frequency": 10},
26+
"y_labeled_nums": np.arange(0, 100, 10)
27+
}
28+
29+
def construct(self):
30+
self.setup_axes(animate=True)
31+
dot = Dot().move_to(self.coords_to_point(PI / 2, 20))
32+
func_graph = self.get_graph(lambda x: 20 * np.sin(x))
33+
self.add(func_graph)
34+
self.add(dot)
35+
36+
.. manim:: Plot3DataPoints
37+
:quality: medium
38+
:save_last_frame:
39+
40+
class Plot3DataPoints(GraphScene):
41+
CONFIG = {
42+
"y_axis_label": r"Concentration[\%]",
43+
"x_axis_label": "Time [s]",
44+
}
45+
46+
def construct(self):
47+
data = [1, 2, 2, 4, 4, 1, 3]
48+
self.setup_axes(animate=True)
49+
for time, dat in enumerate(data):
50+
dot = Dot().move_to(self.coords_to_point(time, dat))
51+
self.add(dot)
52+
53+
.. manim:: Plot3bGaussian
54+
:quality: medium
55+
:save_last_frame:
56+
57+
amp = 5
58+
mu = 3
59+
sig = 1
60+
61+
def gaussian(x):
62+
return amp * np.exp((-1 / 2 * ((x - mu) / sig) ** 2))
63+
64+
class Plot3bGaussian(GraphScene):
65+
def construct(self):
66+
self.setup_axes()
67+
graph = self.get_graph(gaussian, x_min=-1, x_max=10).set_stroke(width=5)
68+
self.add(graph)
69+
70+
.. manim:: Plot3cGaussian
71+
:quality: medium
72+
:save_last_frame:
73+
74+
class Plot3cGaussian(GraphScene):
75+
def construct(self):
76+
def gaussian(x):
77+
amp = 5
78+
mu = 3
79+
sig = 1
80+
return amp * np.exp((-1 / 2 * ((x - mu) / sig) ** 2))
81+
self.setup_axes()
82+
graph = self.get_graph(gaussian, x_min=-1, x_max=10).set_style(stroke_width=5, stroke_color=GREEN)
83+
self.add(graph)
84+
85+
86+
.. manim:: Plot4SinCos
87+
:quality: medium
88+
:save_last_frame:
89+
90+
class Plot4SinCos(GraphScene):
91+
CONFIG = {
92+
"x_min": -10,
93+
"x_max": 10.3,
94+
"num_graph_anchor_points": 100,
95+
"y_min": -1.5,
96+
"y_max": 1.5,
97+
"graph_origin": ORIGIN,
98+
"function_color": RED,
99+
"axes_color": GREEN,
100+
"x_labeled_nums": range(-10, 12, 2),
101+
}
102+
103+
def construct(self):
104+
self.setup_axes(animate=False)
105+
106+
def func_cos(x):
107+
return np.cos(x)
108+
109+
def func_sin(x):
110+
return np.sin(x)
111+
112+
func_graph = self.get_graph(func_cos, self.function_color)
113+
func_graph2 = self.get_graph(func_sin)
114+
vert_line = self.get_vertical_line_to_graph(TAU, func_graph, color=YELLOW)
115+
graph_lab = self.get_graph_label(func_graph, label="\\cos(x)")
116+
graph_lab2 = self.get_graph_label(func_graph2, label="\\sin(x)", x_val=-10, direction=UP / 2)
117+
two_pi = MathTex(r"x = 2 \pi")
118+
label_coord = self.input_to_graph_point(TAU, func_graph)
119+
two_pi.next_to(label_coord, RIGHT + UP)
120+
self.add(func_graph, func_graph2, vert_line, graph_lab, graph_lab2, two_pi)
121+
122+
.. manim:: Plot5Area
123+
:quality: medium
124+
:save_last_frame:
125+
126+
class Plot5Area(GraphScene):
127+
CONFIG = {
128+
"x_min" : 0,
129+
"x_max" : 5,
130+
"y_min" : 0,
131+
"y_max" : 6,
132+
"y_tick_frequency" : 1,
133+
"x_tick_frequency" : 1,
134+
"x_labeled_nums" : [0,2,3]
135+
}
136+
def construct(self):
137+
self.setup_axes(animate=False)
138+
curve1 = self.get_graph(lambda x : 4*x-x**2, x_min=0,x_max=4)
139+
curve2 = self.get_graph(lambda x : 0.8*x**2-3*x+4, x_min=0,x_max=4)
140+
line1 = self.get_vertical_line_to_graph(2, curve1, DashedLine, color=YELLOW)
141+
line2 = self.get_vertical_line_to_graph(3, curve1, DashedLine, color=YELLOW)
142+
area1 = self.get_area(curve1, 0.3, 0.6, dx_scaling=10, area_color=BLUE)
143+
area2 = self.get_area(curve2, 2, 3, bounded=curve1)
144+
self.add(curve1, curve2, line1, line2, area1, area2)
145+
146+
.. manim:: Plot6HeatDiagram
147+
:quality: medium
148+
:save_last_frame:
149+
150+
class Plot6HeatDiagram(GraphScene):
151+
CONFIG = {
152+
"y_axis_label": r"T[$^\circ C$]",
153+
"x_axis_label": r"$\Delta Q$",
154+
"y_min": -8,
155+
"y_max": 30,
156+
"x_min": 0,
157+
"x_max": 40,
158+
"y_labeled_nums": np.arange(-5, 34, 5),
159+
"x_labeled_nums": np.arange(0, 40, 5),
160+
161+
}
162+
163+
def construct(self):
164+
data = [20, 0, 0, -5]
165+
x = [0, 8, 38, 39]
166+
self.setup_axes(animate=True)
167+
dot_collection = VGroup()
168+
for time, val in enumerate(data):
169+
dot = Dot().move_to(self.coords_to_point(x[time], val))
170+
self.add(dot)
171+
dot_collection.add(dot)
172+
l1 = Line(dot_collection[0].get_center(), dot_collection[1].get_center())
173+
l2 = Line(dot_collection[1].get_center(), dot_collection[2].get_center())
174+
l3 = Line(dot_collection[2].get_center(), dot_collection[3].get_center())
175+
self.add(l1, l2, l3)
176+
177+
178+
The following example illustrates how to draw parametric function plots.
179+
4180
.. manim:: ParamFunc1
5181
:quality: medium
6182
:save_last_frame:
@@ -11,4 +187,3 @@ Plotting with manim
11187
def construct(self):
12188
func=ParametricFunction(self.func, t_max=TAU, fill_opacity=0).set_color(RED)
13189
self.add(func.scale(3))
14-

0 commit comments

Comments
 (0)