检查y=6x与y=6x²围成区域及绕轴旋转绘图正确性并添加图例
代码修正与解释
问题说明
编写Python代码绘制$y=6x$与$y=6x2$围成的有界区域,并生成该区域绕x轴、y轴旋转形成的旋转体。需要检查代码绘图正确性,添加图例明确蓝色线条对应$y=6x$,橙色线条对应$y=6x2$。
原代码
# Compare the plot at xy axis with the solid of revolution toward x and y axis # For region bounded by the line y = 6x and y = 6x^2 import matplotlib.pyplot as plt import numpy as np n = 100 fig = plt.figure(figsize=(14, 7)) ax1 = fig.add_subplot(221) ax2 = fig.add_subplot(222, projection='3d') ax3 = fig.add_subplot(223) ax4 = fig.add_subplot(224, projection='3d') y = np.linspace(0, 10, n) x1 = (y/6) x2 = (y/6) ** (1/2) t = np.linspace(0, np.pi * 2, n) u = np.linspace(-1, 2, 60) v = np.linspace(0, 2*np.pi, 60) U, V = np.meshgrid(u, v) X = U Y1 = (6*U**2)*np.cos(V) Z1 = (6*U**2)*np.sin(V) Y2 = (6*U)*np.cos(V) Z2 = (6*U)*np.sin(V) Y3 = ((1/6)*U**(1/2))*np.cos(V) Z3 = ((1/6)*U**(1/2))*np.sin(V) Y4 = (U/6)*np.cos(V) Z4 = (U/6)*np.sin(V) xn = np.outer(x1, np.cos(t)) yn = np.outer(x1, np.sin(t)) zn = np.zeros_like(xn) for i in range(len(x1)): zn[i:i + 1, :] = np.full_like(zn[0, :], y[i]) ax1.plot(x1, y, x2, y) ax1.set_title("$f(x)$") ax2.plot_surface(X, Y3, Z3, alpha=0.3, color='red', rstride=6, cstride=12) ax2.plot_surface(X, Y4, Z4, alpha=0.3, color='blue', rstride=6, cstride=12) ax2.set_title("$f(x)$: Revolution around $y$") # find the inverse of the function x_inverse = y y1_inverse = np.power(x_inverse, 1) y2_inverse = np.power(x_inverse, 1 / 2) ax3.plot(x_inverse, y1_inverse, x_inverse, y2_inverse) ax3.set_title("Inverse of $f(x)$") ax4.plot_surface(X, Y1, Z1, alpha=0.3, color='red', rstride=6, cstride=12) ax4.plot_surface(X, Y2, Z2, alpha=0.3, color='blue', rstride=6, cstride=12) ax4.set_title("$f(x)$: Revolution around $x$") plt.tight_layout() plt.show()
原代码问题
- 坐标范围错误:$y=6x$与$y=6x^2$交点为$(0,0)$和$(1,6)$,原代码绘制了超出该范围的无效区域
- 旋转体计算错误:绕y轴旋转的变量对应关系混乱,绕x轴旋转的x范围超出有界区域
- 图例缺失:2D图无图例,无法区分两条曲线对应的函数
- 逆函数绘图错误:ax3未正确绘制原函数的逆函数曲线
修正后的代码
import matplotlib.pyplot as plt import numpy as np n = 100 fig = plt.figure(figsize=(14, 7)) ax1 = fig.add_subplot(221) ax2 = fig.add_subplot(222, projection='3d') ax3 = fig.add_subplot(223) ax4 = fig.add_subplot(224, projection='3d') # -------------------------- 2D有界区域(x关于y的函数,绕y轴旋转基础) -------------------------- y = np.linspace(0, 6, n) x_line = y / 6 # y=6x的逆函数:x=y/6 x_parabola = np.sqrt(y / 6)# y=6x²的逆函数:x=√(y/6) # 绘制曲线并添加图例 line_plot, = ax1.plot(x_line, y, color='blue', label='$y=6x$') parabola_plot, = ax1.plot(x_parabola, y, color='orange', label='$y=6x^2$') ax1.legend(handles=[line_plot, parabola_plot]) ax1.fill_betweenx(y, x_line, x_parabola, alpha=0.1, color='gray') ax1.set_title('有界区域(x-y轴)') ax1.set_xlabel('x') ax1.set_ylabel('y') # -------------------------- 绕y轴旋转的旋转体 -------------------------- u_y = np.linspace(0, 6, 60) v = np.linspace(0, 2*np.pi, 60) U_y, V = np.meshgrid(u_y, v) # y=6x绕y轴旋转 X_line_y = (U_y / 6) * np.cos(V) Z_line_y = (U_y / 6) * np.sin(V) Y_line_y = U_y # y=6x²绕y轴旋转 X_parabola_y = np.sqrt(U_y / 6) * np.cos(V) Z_parabola_y = np.sqrt(U_y / 6) * np.sin(V) Y_parabola_y = U_y surf_line_y = ax2.plot_surface(X_line_y, Y_line_y, Z_line_y, alpha=0.3, color='blue', rstride=6, cstride=12) surf_parabola_y = ax2.plot_surface(X_parabola_y, Y_parabola_y, Z_parabola_y, alpha=0.3, color='orange', rstride=6, cstride=12) ax2.legend([surf_line_y, surf_parabola_y], ['$y=6x$ 绕y轴旋转', '$y=6x^2$ 绕y轴旋转'], loc='upper right') ax2.set_title('绕y轴旋转的旋转体') ax2.set_xlabel('x') ax2.set_ylabel('y') ax2.set_zlabel('z') # -------------------------- 原函数2D绘图(y关于x的函数,绕x轴旋转基础) -------------------------- x = np.linspace(0, 1, n) y_line = 6 * x # y=6x y_parabola = 6 * x**2 # y=6x² line_plot_inv, = ax3.plot(x, y_line, color='blue', label='$y=6x$') parabola_plot_inv, = ax3.plot(x, y_parabola, color='orange', label='$y=6x^2$') ax3.legend(handles=[line_plot_inv, parabola_plot_inv]) ax3.fill_between(x, y_line, y_parabola, alpha=0.1, color='gray') ax3.set_title('原函数曲线(x-y轴)') ax3.set_xlabel('x') ax3.set_ylabel('y') # -------------------------- 绕x轴旋转的旋转体 -------------------------- u_x = np.linspace(0, 1, 60) U_x, V = np.meshgrid(u_x, v) # y=6x绕x轴旋转 Y_line_x = 6 * U_x * np.cos(V) Z_line_x = 6 * U_x * np.sin(V) X_line_x = U_x # y=6x²绕x轴旋转 Y_parabola_x = 6 * U_x**2 * np.cos(V) Z_parabola_x = 6 * U_x**2 * np.sin(V) X_parabola_x = U_x surf_line_x = ax4.plot_surface(X_line_x, Y_line_x, Z_line_x, alpha=0.3, color='blue', rstride=6, cstride=12) surf_parabola_x = ax4.plot_surface(X_parabola_x, Y_parabola_x, Z_parabola_x, alpha=0.3, color='orange', rstride=6, cstride=12) ax4.legend([surf_line_x, surf_parabola_x], ['$y=6x$ 绕x轴旋转', '$y=6x^2$ 绕x轴旋转'], loc='upper right') ax4.set_title('绕x轴旋转的旋转体') ax4.set_xlabel('x') ax4.set_ylabel('y') ax4.set_zlabel('z') plt.tight_layout() plt.show()
修正要点
- 范围调整:将x限制在$[0,1]$、y限制在$[0,6]$,仅绘制两条曲线围成的有效区域
- 图例完善:2D和3D图均添加图例,明确蓝色对应$y=6x$,橙色对应$y=6x^2$
- 旋转体修正:
- 绕x轴旋转:以x为自变量,将y值绕x轴生成旋转曲面
- 绕y轴旋转:以y为自变量,将x值绕y轴生成旋转曲面
- 视觉优化:填充有界区域增加透明度,提升图形直观性
内容的提问来源于stack exchange,提问作者Freya the Goddess
相关产品推荐
相关产品推荐

