import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(9,9))
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.1)
Y = np.arange(-5, 5, 0.1)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X ** 2 + Y ** 2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.winter)
ax.grid(False)#默认True,风格线。
ax.set_xticks([])#不显示x坐标轴
ax.set_yticks([])#不显示y坐标轴
ax.set_zticks([])#不显示z坐标轴
plt.axis('off')#关闭所有坐标轴
plt.subplots_adjust(0, 0, 1, 1)
plt.savefig(f'没有背景.png', transparent=True, bbox_inches='tight', pad_inches=0.0)
使用plt.savefig()保存图像,其中参数transparent=True,这个设置会让坐标轴,以及图像补丁(也就是alpha为0的位置)都变为透明;bbox_inches和pad_inches的设置是为了保存图像时删除图像的白边。
效果如下:
简单曲面:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(9,9))
ax = fig.gca(projection='3d')
x1 = np.arange(0, 5, 0.1)
x2 = np.arange(5, -0.1, -0.1)
X = np.append(x1,x2)
y1 = np.arange(0, 5, 0.1)
y2 = np.arange(5, -0.1, -0.1)
Y = np.append(y1,y2)
X, Y = np.meshgrid(X, Y)
R = X ** 2 + Y ** 2
Z = R
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.winter)
ax.grid(False)#默认True,风格线。
ax.set_xticks([])#不显示x坐标轴
ax.set_yticks([])#不显示y坐标轴
ax.set_zticks([])#不显示z坐标轴
plt.axis('off')#关闭所有坐标轴
plt.subplots_adjust(0, 0, 1, 1)
plt.savefig(f'没有背景的.png', transparent=True, bbox_inches='tight', pad_inches=0.0)
凸面:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(9,9))
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.1)
Y = np.arange(-5, 5, 0.1)
X, Y = np.meshgrid(X, Y)
# R = np.sqrt(X ** 2 + Y ** 2)
# Z = np.sin(R)
R = X ** 2 + Y ** 2
Z = 50 - R # 让z等于R就是凹面
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.winter)
ax.grid(False)#默认True,风格线。
ax.set_xticks([])#不显示x坐标轴
ax.set_yticks([])#不显示y坐标轴
ax.set_zticks([])#不显示z坐标轴
plt.axis('off')#关闭所有坐标轴
plt.subplots_adjust(0, 0, 1, 1)
plt.savefig(f'没有背景的.png', transparent=True, bbox_inches='tight', pad_inches=0.0)
默认凹面画法:文章来源:https://www.toymoban.com/news/detail-756431.html
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(9,9))
ax = fig.gca(projection='3d')
x1 = np.arange(0, 5, 0.1)
x2 = np.arange(5, -0.1, -0.1)
X = np.append(x1,x2)
y1 = np.arange(0, 5, 0.1)
y2 = np.arange(5, -0.1, -0.1)
Y = np.append(y1,y2)
X, Y = np.meshgrid(X, Y)
R = X ** 2 + Y ** 2
Z = R
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.winter)
ax.grid(False)#默认True,风格线。
ax.set_xticks([])#不显示x坐标轴
ax.set_yticks([])#不显示y坐标轴
ax.set_zticks([])#不显示z坐标轴
# plt.axis('off')#关闭所有坐标轴
plt.subplots_adjust(0, 0, 1, 1)
plt.savefig(f'没有背景的.png', transparent=True, bbox_inches='tight', pad_inches=0.0)
文章来源地址https://www.toymoban.com/news/detail-756431.html
到了这里,关于使用python绘制3D图,并保存没有背景的png格式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!