如何解决Matplotlib仿Matlab Mesh Plot的阴影异常问题?
问题:Matplotlib复刻Matlab 3D Mesh图时出现阴影异常
Matlab绘制的3D曲面Mesh Plot效果整洁(红色边框线可忽略):
白色曲面搭配黑色网格线,视角清晰,无异常阴影,整体呈现干净的网格曲面效果
但用Matplotlib复刻该图像时,出现怪异的阴影问题:曲面顶部呈纯黑色,仅底部显示白色:
曲面顶部大面积被黑色覆盖,仅底部区域为白色,阴影效果不符合预期
以下是两种绘图的代码(曲面数据u可替换为任意3D数据,如peaks):
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'))
求解决该阴影异常问题的思路。
内容的提问来源于stack exchange,提问作者Luke Bhan
相关产品推荐
相关产品推荐

