相关疑难解决方法(0)

在matplotlib中Matlab的surf(x,y,z,c)的等价物是什么?

我想实现的功能等surf(x,y,z,c)在MATLAB中,在这里x,y并且z是坐标,c是一个变量的值,我可以使用c来定义颜色.我不知道如何实现它matplotlib.

python matlab matplotlib

8
推荐指数
1
解决办法
9760
查看次数

通过3D表面绘制2D平面

我正在尝试使用Numpy和Matplotlib可视化2D平面切割3D图形来解释偏导数的直觉.

具体来说,我使用的函数是J(θ1,θ2)=θ1^ 2 +θ2^ 2,我想在θ2= 0处绘制θ1-J(θ1,θ2)平面.

我设法使用下面的代码绘制2D平面,但2D平面和3D图形的叠加不太正确,2D平面略微偏离,因为我希望平面看起来像在θ2处切割3D = 0.

如果我可以借鉴你的专业知识,那将是很好的,谢谢.

    def f(theta1, theta2):
        return theta1**2 + theta2**2

    fig, ax = plt.subplots(figsize=(6, 6), 
                           subplot_kw={'projection': '3d'})

    x,z = np.meshgrid(np.linspace(-1,1,100), np.linspace(0,2,100))
    X = x.T
    Z = z.T
    Y = 0 * np.ones((100, 100))
    ax.plot_surface(X, Y, Z)

    r = np.linspace(-1,1,100)
    theta1_grid, theta2_grid = np.meshgrid(r,r)
    J_grid = f(theta1_grid, theta2_grid)
    ax.contour3D(theta1_grid,theta2_grid,J_grid,500,cmap='binary')

    ax.set_xlabel(r'$\theta_1$',fontsize='large')
    ax.set_ylabel(r'$\theta_2$',fontsize='large')
    ax.set_zlabel(r'$J(\theta_1,\theta_2)$',fontsize='large')
    ax.set_title(r'Fig.2 $J(\theta_1,\theta_2)=(\theta_1^2+\theta_2^2)$',fontsize='x-large')

    plt.tight_layout()
    plt.show()
Run Code Online (Sandbox Code Playgroud)

这是代码输出的图像:

绘图显示抛物面与垂直平面奇怪地叠加在其上

python 3d numpy matplotlib

7
推荐指数
1
解决办法
1957
查看次数

标签 统计

matplotlib ×2

python ×2

3d ×1

matlab ×1

numpy ×1