在Matplotlib中模拟Matlab网格图生成阴影效果

uinbv5nw  于 2023-01-21  发布在  Matlab
关注(0)|答案(1)|浏览(215)

我有这个网格图,在matlab中看起来非常干净的3d表面(忽略红色边界线):

我试图在matplotlib中模拟相同的图像,但是,我得到了这种奇怪的阴影效果,表面的顶部是纯黑色,只有底部是白色:

我有两个图的代码在这里 * 我的表面u可以替换为任何三维数据(即峰值)
matlab软体

dx=0.01;
dt=0.001; %Attention : they have to be integer-multiples of one another
T=1.999; %length of simulation

x=(0:dx:1);
t=(0:dt:T);
u = load('u.mat', 'u');
u = u.u;
u2 = load('u2.mat','u');
u2 = u2.u;
pstep = 3;
tstep = 30;
xzeros = repelem(1, 1, length(t));
zline = interp2(x, t, u, xzeros, t);
zline2 = interp2(x, t, u2, xzeros, t);
subplot(1, 2, 1);
mesh(x(1:pstep:end),t(1:tstep:end),u(1:tstep:end,1:pstep:end), "edgecolor", "black");
    view(90, 10);
    xlabel('x', 'FontName', 'Arial', 'FontSize',18)
    ylabel('Time', 'FontName', 'Arial', 'FontSize',18)
    zlabel('u(x,t)', 'FontName', 'Arial', 'FontSize',18)
    set(gca,'FontName', 'Arial', 'FontSize',18)
    hold on
    plot3(xzeros, t, zline, 'r', 'linewidth', 3);
subplot(1, 2, 2)   
mesh(x(1:pstep:end),t(1:tstep:end),u2(1:tstep:end,1:pstep:end), "edgecolor", "black");
    view(90, 10);
    xlabel('x', 'FontName', 'Arial', 'FontSize',18)
    ylabel('Time', 'FontName', 'Arial', 'FontSize',18)
    zlabel('u(x,t)', 'FontName', 'Arial', 'FontSize',18)
    set(gca,'FontName', 'Arial', 'FontSize',18)
    hold on
   plot3(xzeros, t, zline2, 'r', 'linewidth', 3);
set(gcf, 'PaperPositionMode', 'auto');
sgtitle("$$\hat{u}$ for PDE Solutions Using $\hat{k}$$", 'interpreter', 'latex', 'FontSize', 32)
saveas(gca, "matlab.png");

和 matplotlib

fig = plt.figure(figsize=(8, 4))
plt.subplots_adjust(left=0.03, bottom=0, right=0.98, top=1, wspace=0.1, hspace=0)
subfigs = fig.subfigures(nrows=1, ncols=1, hspace=0)

subfig = subfigs
subfig.suptitle(r"$\hat{u}$ for PDE Solutions Using $\hat{k}$")
ax = subfig.subplots(nrows=1, ncols=2, subplot_kw={"projection": "3d"})
ax[0].plot_surface(x, t, uarr[0], edgecolor="black",lw=0.2, rstride=30,
                       cstride=3,
                        alpha=1, color="white")
ax[0].view_init(5, 5)
ax[0].set_xlabel("x", labelpad=10)
ax[0].set_ylabel("Time")
ax[0].set_zlabel(r"$\hat{u}(x, t)$", labelpad=5)
ax[0].zaxis.set_rotate_label(False)
ax[0].yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax[1].plot_surface(x, t, uarr[2], edgecolor="black",lw=0.2, rstride=30,
                       cstride=3,
                        alpha=1,color="white")
ax[1].view_init(5, 5)
ax[1].set_xlabel("x", labelpad=10)
ax[1].set_ylabel("Time")
ax[1].set_zlabel(r"$\hat{u}(x, t)$", labelpad=5)
ax[1].zaxis.set_rotate_label(False)
ax[1].yaxis.set_major_formatter(FormatStrFormatter('%.1f'))

如有任何缓解这一问题的想法,我们将不胜感激。

ou6hu8tu

ou6hu8tu1#

为了模拟matlab绘图,正确设置cstride和rstride与matlab相同是非常重要的。在本例中,为了匹配绘图,我设置cstride = 3,rstride为30。

相关问题