注意: 这不是一个转换问题。这是为了看看Python是否有能力像Matlab一样生成3D绘图
我试着用Python绘制它,但是我不能得到像Matlab那样好的结果。是否有任何软件包可以像原始软件包一样绘制上述内容?如果是,请将我的代码转换为Python版本。这是我的Matlab代码
set(groot,'defaultAxesTickLabelInterpreter','latex');
set(groot,'defaulttextinterpreter','latex');
set(groot,'defaultLegendInterpreter','latex');
x0=0;
y0=0;
width=3000;
height=2000;
set(gcf,'position',[x0,y0,width,height])
[X,Y] = meshgrid(-1:.01:1);
a = 3;
b = 2;
Z = a*X.^2 + b*Y.^2;
subplot(1,3,1)
s = surf(X,Y,Z,'FaceColor','r', 'FaceAlpha',0.5, 'EdgeColor','none');
s.EdgeColor = 'none';
xlabel('$x_1$','Interpreter','latex','FontSize', 15)
ylabel('$x_2$','Interpreter','latex','FontSize', 15)
zlabel('$f(\mathbf{x};\mathbf{\theta})$','Interpreter','latex','FontSize', 15)
legend({'$f([x_1, x_2]^\top; [\theta_1=3,\theta_2=2]^\top)=3x_1^2+2x_2^2$'},'interpreter','latex','FontSize', 10)
subplot(1,3,2)
Z2 = a*X.^2 ;
s2 = surf(X,Y,Z2,'FaceColor','b', 'FaceAlpha',0.5, 'EdgeColor','none');
s2.EdgeColor = 'none';
xlabel('$x_1$','Interpreter','latex','FontSize', 15)
ylabel('$x_2$','Interpreter','latex','FontSize', 15)
zlabel('$f(\mathbf{x};\mathbf{\theta})$','Interpreter','latex','FontSize', 15)
legend({'$f([x_1, x_2]^\top; [\theta_1=3,\theta_2=0]^\top)=3x_1^2$'},'interpreter','latex','FontSize', 10)
subplot(1,3,3)
s3 = surf(X,Y,Z,'FaceColor','r', 'FaceAlpha',0.5, 'EdgeColor','none');
s3.EdgeColor = 'none';
hold
s4 = surf(X,Y,Z2,'FaceColor','b', 'FaceAlpha',0.5, 'EdgeColor','none');
s4.EdgeColor = 'none';
xlabel('$x_1$','Interpreter','latex','FontSize', 15)
ylabel('$x_2$','Interpreter','latex','FontSize', 15)
zlabel('$f(\mathbf{x};\mathbf{\theta})$','Interpreter','latex','FontSize', 15)
legend({'$f(\mathbf{x};\mathbf{\theta})=3x_1^2+2x_2^2$', '$f(\mathbf{x};\mathbf{\theta})=3x_1^2$'},'interpreter','latex','FontSize', 10)
对于Python中的3D图表,我已经用
matplotlib.pyplot
获得了最好的结果https://www.python-graph-gallery.com/371-surface-plot
您可以通过添加更多数据点来增加图表的平滑度,使用鼠标沿x、y、z轴旋转图表,还可以添加标题、图例和其他吸引眼球的内容
matplotlib.mplot3d看起来像是欧几里德连续曲面
https://matplotlib.org/stable/gallery/mplot3d/contour3d_2.html#sphx-glr-gallery-mplot3d-contour3d-2-py
您正在使用matlab的
meshgrid(...)
工具生成x,y,z数据。Python可以通过将numpy.meshgrid
馈送到matplotlib.pyplot
中获得相同的结果https://jakevdp.github.io/PythonDataScienceHandbook/04.12-three-dimensional-plotting.html
对
numpy
+plotly
是一个有效的Matlab替代品-您可能会认识到一些代码:)。作为一个好处,绘图呈现为html,这意味着它们具有高度的可移植性,可以保存为单个文件,并且可以嵌入到网页中。可能会有不同的小细节(我不知道latex axis标签的当前状态),但是,如果您安装了python、numpy和plotly,下面是第一个plot的很好替代:请注意,python中的go to plotting包是Matplotlib。在我看来,它继承了Matlab绘图中最糟糕的部分,但没有一个好的部分(性能渲染)。从性能(esp 3D渲染)、交互性和API的角度来看,Plotly性能优越
相关问题 更多 >
编程相关推荐