TypeError: LinearGradient.__new__() 需要精确的4个参数(只提供了2个)

huangapple go评论106阅读模式
英文:

Error in manim TypeError: LinearGradient.__new__() takes exactly 4 arguments (2 given)

问题

问题显然出现在定义抛物面(paraboloid)的Surface部分,你错在哪里了?错误信息是:
TypeError: LinearGradient.new() 接受正好 4 个参数(给定了 2 个)。
似乎在定义表面时缺少一些参数。看起来 LinearGradient 方法位于 Surface 类内部。

要解决这个问题,你需要查看 Surface 类的文档,确保正确地传递参数。在你的代码中,你正在尝试通过填充颜色(fill_color)和渐变颜色(checkerboard_colors)来定义表面的外观。根据错误信息,似乎你需要传递 LinearGradient 方法所需的正确参数。

请查阅 Manim 文档或 Surface 类的源代码,以确定正确的参数传递方式,并相应地更新你的代码以解决这个问题。

英文:
  1. from manim import *
  2. class Solids(ThreeDScene):
  3. def construct(self):
  4. x_min, x_max, x_step = 0, 5, 1
  5. y_min, y_max, y_step = -5, 5, 1
  6. z_max, z_min, z_step = -5, 5, 1
  7. axes = ThreeDAxes(
  8. x_range=[x_min, x_max + x_step, x_step],
  9. y_range=[y_min, y_max + y_step, y_step],
  10. z_range=[z_min, z_max + z_step, z_step],)
  11. function = axes.plot(lambda x: np.sqrt(
  12. x), color=YELLOW, x_range=[0, 5])
  13. area = axes.get_area(function, x_range=[0, 5], color=BLUE)
  14. rec = axes.get_riemann_rectangles(function, dx=0.2, x_range=[
  15. 1, 1.2], input_sample_type="left", stroke_width=0.5, fill_opacity=0.75, color=BLUE)
  16. rec_label = Text("dx", font_size=20)
  17. rec_label.next_to(rec, DOWN, buff=0,)
  18. group = VGroup(function, area, rec)
  19. # parametric surface of sqrt of x but in 3d roatetd about x axis
  20. def funcs(u, v):
  21. return np.array([
  22. np.sqrt((u)-(v**2)),
  23. u
  24. ])
  25. start_color = BLUE
  26. end_color = YELLOW
  27. paraboloid = Surface(
  28. lambda u, v: axes.c2p(*funcs(u, v)), u_range=[0, 5], v_range=[-5, 5], fill_color=end_color,
  29. color=start_color, resolution=8, checkerboard_colors=[start_color, end_color], fill_opacity=0.75, stroke_color=BLUE, stroke_width=0.5,)
  30. self.play(FadeIn(axes))
  31. self.wait(2)
  32. self.play(FadeIn(function))
  33. self.play(FadeIn(rec))
  34. self.play(Write(rec_label))
  35. self.play(FadeIn(area))
  36. self.wait(2)
  37. self.begin_ambient_camera_rotation()
  38. self.set_camera_orientation(phi=75 * DEGREES, theta=-30 * DEGREES)
  39. self.play(Rotating(group, axis=RIGHT, radians=2*PI,
  40. about_point=ORIGIN), run_time=5, rate_func=linear)
  41. self.wait(2)
  42. self.play(FadeIn(paraboloid))
  43. self.wait(2)

the problem is clearly in defining paraboloid Surface what did I get wrong? error:
TypeError: LinearGradient.new() takes exactly 4 arguments (2 given)
seems like I am missing some args when defining the surface. seems like the LinearGradient method is inside the Surface class

答案1

得分: 1

以下是代码的翻译部分:

  1. 存在于您的`Surface()`对象中的两个问题
  2. a) 要绘制的函数需要返回三个值xyz以覆盖该表面
  3. b) 您的函数至少在某些点上返回复数因为`np.sqrt((u)-v**2)`的参数小于零
  4. 我删除了其他所有内容只保留了`Surface()`的最简版本我不知道这是否符合您的期望但至少这段代码可以正常运行

接下来是两个编辑后的版本,一个是在柱坐标系中参数化表面,另一个是在x/y平面上映射u和v,但需要限制y的范围以避免复数值。

英文:

There are 2 problems in your Surface() object:

a) the function to be plotted needs to return 3 values, the x, y and z value to span the surface

b) your function at least at some points delivers complex numbers as a result of the argument of np.sqrt((u)-v**2) becoming less than zero

I cut out everything else and made a minimalistic version of just the Surface() I don't know if this is what you expect to see, but at least the code runs without error.

  1. class SolidsMini(ThreeDScene):
  2. def construct(self):
  3. x_min, x_max, x_step = 0, 5, 1
  4. y_min, y_max, y_step = -5, 5, 1
  5. z_max, z_min, z_step = -5, 5, 1
  6. axes = ThreeDAxes(
  7. x_range=[x_min, x_max + x_step, x_step],
  8. y_range=[y_min, y_max + y_step, y_step],
  9. z_range=[z_min, z_max + z_step, z_step],)
  10. def funcs(u, v):
  11. return np.array([
  12. u, v, np.sqrt(abs((u)-(v**2))),
  13. ])
  14. start_color = BLUE
  15. end_color = YELLOW
  16. paraboloid = Surface(
  17. lambda u, v: axes.c2p(*funcs(u, v)),
  18. u_range=[0, 5],
  19. v_range=[-5, 5],
  20. #fill_color=end_color,
  21. #color=start_color,
  22. resolution=8,
  23. checkerboard_colors=[start_color, end_color],
  24. fill_opacity=0.75,
  25. stroke_color=BLUE,
  26. stroke_width=0.5,
  27. )
  28. self.play(FadeIn(axes))
  29. self.set_camera_orientation(phi=75 * DEGREES, theta=-30 * DEGREES)
  30. self.wait(2)
  31. self.play(FadeIn(paraboloid))
  32. self.wait(2)

TypeError: LinearGradient.__new__() 需要精确的4个参数(只提供了2个)

Edit 2023-06-14
Ok, with this new information: The easiest way would be to parametrize your surface in cylindrical coordinates then:

  1. def construct(self):
  2. x_min, x_max, x_step = -5, 5, 1
  3. y_min, y_max, y_step = -5, 5, 1
  4. z_min, z_max, z_step = -5, 5, 1
  5. axes = ThreeDAxes(
  6. x_range=[x_min, x_max + x_step, x_step],
  7. y_range=[y_min, y_max + y_step, y_step],
  8. z_range=[z_min, z_max + z_step, z_step],
  9. )
  10. def funcs(u, v):
  11. # parametrize u as x and v as phi
  12. # then the radius increases as the sqrt(u)
  13. return [u, np.sqrt(u)*np.cos(v), np.sqrt(u)*np.sin(v)]
  14. start_color = BLUE
  15. end_color = YELLOW
  16. paraboloid = Surface(
  17. lambda u, v: axes.c2p(*funcs(u, v)),
  18. u_range=[0.01, 5],
  19. v_range=[0, 2*PI],
  20. resolution=20,
  21. checkerboard_colors=[start_color, end_color],
  22. fill_opacity=0.75,
  23. stroke_color=BLUE,
  24. stroke_width=0.5,
  25. )
  26. self.set_camera_orientation(phi=75 * DEGREES, theta=-30 * DEGREES)
  27. self.add(axes)
  28. self.add(paraboloid)
  29. self.wait()

TypeError: LinearGradient.__new__() 需要精确的4个参数(只提供了2个)

Alternatively you can map u and v onto the x/y plane but y needs to be limited since python will happily draw the root out of a negative number and return a complex value....

  1. class SolidsMini2(ThreeDScene):
  2. def construct(self):
  3. x_min, x_max, x_step = -5, 5, 1
  4. y_min, y_max, y_step = -5, 5, 1
  5. z_min, z_max, z_step = -5, 5, 1
  6. axes = ThreeDAxes(
  7. x_range=[x_min, x_max + x_step, x_step],
  8. y_range=[y_min, y_max + y_step, y_step],
  9. z_range=[z_min, z_max + z_step, z_step],
  10. )
  11. def upper(u, v):
  12. # parametrize u as x and v as y
  13. # y needs to be limited
  14. # -np.sqrt(u) <= y <= +np.sqrt(u)
  15. x = u
  16. y = v/5.01 * np.sqrt(u)
  17. arg = x - y**2
  18. if arg < 0:
  19. arg = 0
  20. z = np.sqrt(arg)
  21. return [x,y,z]
  22. def lower(u, v):
  23. # parametrize u as x and v as y
  24. #
  25. x = u
  26. y = v if abs(v) < np.sqrt(u) else np.sign(v)*np.sqrt(u)
  27. arg = x - y**2
  28. if arg < 0:
  29. arg = 0
  30. z = -np.sqrt(arg)
  31. return [x,y,z]
  32. start_color = BLUE
  33. end_color = YELLOW
  34. paraboloid = Surface(
  35. lambda u, v: axes.c2p(*upper(u, v)),
  36. u_range=[0.01, 5],
  37. v_range=[-5, 5],
  38. resolution=20,
  39. checkerboard_colors=[start_color, end_color],
  40. fill_opacity=0.75,
  41. stroke_color=BLUE,
  42. stroke_width=0.5,
  43. )
  44. self.set_camera_orientation(phi=75 * DEGREES, theta=-30 * DEGREES)
  45. self.add(axes)
  46. self.add(paraboloid)
  47. self.wait()

TypeError: LinearGradient.__new__() 需要精确的4个参数(只提供了2个)

huangapple
  • 本文由 发表于 2023年6月13日 05:59:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定