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