matplotlib绘制三维曲面
用到个简单曲面,自己用PS或者PPT怎么也画不出来,那就用matplotlib画吧
import numpy as np
import matplotlib.pyplot as mp
from mpl_toolkits.mplot3d import Axes3D
# 准备数据
#n越大,图像越细腻
n = 10000
x, y = np.meshgrid(np.linspace(-1, 1, n),
np.linspace(-0.05, 0.05, n))
#核心函数,控制曲面形状
z =-(10*x*x+y)
# 绘制图片
fig = mp.figure("3D Surface", facecolor="lightgray")
mp.title("3D Surface", fontsize=18)
# 设置为3D图片类型
ax3d = Axes3D(fig)
# ax3d = mp.gca(projection="3d") # 同样可以实现
ax3d.set_xlabel("X")
ax3d.set_ylabel("Y")
ax3d.set_zlabel("Z")
#ax3d.set_zlim3d(-10,0)
mp.tick_params(labelsize=10)
#颜色是有高级感的紫
ax3d.plot_surface(x, y, z, cstride=20, rstride=20,color='#826677')
#改变视角,elev:沿着y轴旋转,azim:沿着z轴旋转
ax3d.view_init(elev=70., azim=82)
#关闭坐标轴
ax3d.set_xticks([])
ax3d.set_yticks([])
ax3d.set_zticks([])
# Get rid of the panes
ax3d.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax3d.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax3d.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# Get rid of the spines
ax3d.w_xaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
ax3d.w_yaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
ax3d.w_zaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
#400dpi已经很清楚了
mp.savefig('/content/sample_data/pic.jpg',dpi=400)
mp.show()