python15种3D绘图函数总结

📊35种绘图函数总结📊28种极坐标绘图函数总结

基础图

一般来说,二维绘图函数在不新增坐标轴的情况下,可以指定一个z轴方向,然后在z=0的平面内进行绘图,支持这种特性的函数有下面这几个

函数坐标参数图形类别
plotx,y曲线图默认z=0
scatterx,y散点图默认z=0
stepx,y步阶图默认z=0
barx,y条形图默认z=0
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(25)/3
y = np.sin(x)


keys = ["plot", "scatter", "step", "bar"]

fig = plt.figure(figsize=(14,4))
for i,key in enumerate(keys, 1):
    ax = fig.add_subplot(1, 4, i, projection="3d")
    fDct = {"plot" : ax.plot,  "scatter"  : ax.scatter,         
            "step" : ax.step,  "bar"      : ax.bar}
    fDct[key](x,y)
    plt.title(key)

plt.tight_layout()
plt.show()

在这里插入图片描述

如果可以输入三个坐标,那么就不必指定zdir了,但x,y,z必须是一维数组,只有scatter例外。

函数坐标参数图形类别同名函数
plotx, y, z曲线图
scatterx, y, z散点图可以是二维网格
stepx, y, z步阶图
barx, y, z条形图
stemx, y, z茎叶图
x = np.arange(35)/3
y, z = np.sin(x), np.cos(x)

fig = plt.figure(figsize=(9,5))

ax = fig.add_subplot(231, projection="3d")
ax.plot(x, y, z)
plt.title("plot/plot3D")

ax = fig.add_subplot(232, projection="3d")
ax.scatter(x, y, z)
plt.title("scatter/scatter3D")

ax = fig.add_subplot(233, projection="3d")
ax.step(x, y, z)
plt.title("step")

ax = fig.add_subplot(234, projection="3d")
ax.bar(x, y, z)
plt.title("bar")

ax = fig.add_subplot(235, projection="3d")
ax.stem(x, y, z)
plt.title("stem/stem3D")

x,y = np.indices([10, 10])/5
z = np.cos(x)+np.sin(y)
ax = fig.add_subplot(236, projection="3d")
ax.scatter(x, y, z)
plt.title("scatter/scatter3D")

plt.tight_layout()
plt.show()

在这里插入图片描述

三维图

绘图函数坐标绘图类型坐标说明
plot_surfacex,y,z三维曲面图x,y必须是网格
plot_wireframex,y,z三维网格图x,y必须是网格
plot_trisurfx,y,z三角曲面图x,y,z是一维数组
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np

X, Y = np.indices([30, 30])/3 - 5
Z = np.sin(np.sqrt(X**2 + Y**2))

fig = plt.figure(figsize=(12,5))

ax = fig.add_subplot(131, projection='3d')
ax.plot_surface(X, Y, Z)
plt.title("plot_surface")

ax = fig.add_subplot(132, projection='3d')
ax.plot_wireframe(X, Y, Z)
plt.title("plot_wireframe")

ax = fig.add_subplot(133, projection='3d')
ax.plot_trisurf(X.reshape(-1), Y.reshape(-1), Z.reshape(-1))
plt.title("plot_trisurf")

plt.tight_layout()
plt.show()

在这里插入图片描述

误差线

二维坐标中的误差线函数errorbar在三维投影下仍然适用,并且支持z方向的误差线

x = np.arange(25)/3
y = np.sin(x)
z = np.cos(x)
y1, y2 = 0.9*y, 1.1*y
x1, x2 = 0.9*x, 1.1*x
z1, z2 = 0.9*z, 1.1*z
xerr = np.abs([x1, x2])/10
yerr = np.abs([y1, y2])/10
zerr = np.abs([z1, z2])/10

fig = plt.figure(figsize=(10,3))

ax = fig.add_subplot(131, projection='3d')
ax.errorbar(x, y, z, yerr=yerr)
plt.title("errorbar with yerr")

ax = fig.add_subplot(132, projection='3d')
ax.errorbar(x, y, z, xerr=xerr)
plt.title("errorbar with xerr")

ax = fig.add_subplot(133, projection='3d')
ax.errorbar(x, y, z, zerr=zerr)
plt.title("errorbar with zerr")

plt.tight_layout()
plt.show()

效果如下

在这里插入图片描述

等高线

等高线理所当然地支持三维坐标图,毕竟在没有第三个轴的情况下,等高线图都会用伪彩色来显示高度,现在有了第三个轴,那必须得墙裂支持。

但对于contourf而言,并不会把登高平面衔接起来,所以看上去就像梯田一样,所以不建议在3D坐标中直接绘制contourf图像,而是声名一个z轴,绘制这个z轴方向的投影

X, Y = np.indices([100,100])/30 - 1.5
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)

fig = plt.figure(figsize=(10,3))

ax = fig.add_subplot(131, projection='3d')
ax.contour(X, Y, Z)
plt.title("contour")


ax = fig.add_subplot(132, projection='3d')
ax.contourf(X, Y, Z)
plt.title("contourf")

ax = fig.add_subplot(133, projection='3d')
ax.contourf(X, Y, Z, zdir='y', offset=0)
plt.title("contourf with zdir=y")

plt.tight_layout()
plt.show()

在这里插入图片描述

其中,第三幅图是整个等高线图在z轴方向的投影,可以理解为左视图。

场图

场图也是可以支持三维数据的,只不过需要6个坐标而已,记作x,y,z,u,v,w,但流场和风场就不支持三维图像了。

Y, X, Z = np.indices([4,4,4])/0.5 - 3
U = X + Y
V = Y - X
W = X + Y - Z

ax = plt.subplot(projection='3d')
ax.quiver(X, Y, Z, U, V, W,  length=0.2)
plt.title("quiver")


plt.tight_layout()
plt.show()

在这里插入图片描述

统计图

hist和boxplot在3D坐标中是可以绘制出来的,但是并不支持指定坐标轴,也不支持指定绘图位置,所以并不建议在3D坐标系中使用,下面仅做下展示

x = np.random.standard_normal(size=1000)

fig = plt.figure(figsize=(9,4))

ax = fig.add_subplot(121, projection='3d')
ax.hist(x)
plt.title("hist")


ax = fig.add_subplot(122, projection='3d')
ax.boxplot(x)
plt.title("boxplot")

plt.tight_layout()
plt.show()

在这里插入图片描述

非结构坐标图

tricontour和tricontourf支持三维坐标,而triplot尽管在3维坐标系下不报错,但绘制的其实是二维图像,所以并不推荐。另一方面,和contourf类似,tricontourf所产生从登高面,同样并不会彼此衔接,所以观感其实是比较奇怪的,所以在三维坐标系中,比较推荐的非结构坐标图就只有tricontour这一个。

x = np.random.uniform(-4, 4, 256)
y = np.random.uniform(-2, 2, 256)
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)

levels = np.linspace(z.min(), z.max(), 7)

fig = plt.figure(figsize=(9,4))

ax = fig.add_subplot(121, projection='3d')
ax.plot(x, y, z, 'o', markersize=1, alpha=0.5)
ax.tricontour(x, y, z, levels=levels)
plt.title("tricontour")

ax = fig.add_subplot(122, projection='3d')
ax.plot(x, y, z, 'o', markersize=1, alpha=0.5)
ax.tricontourf(x, y, z, levels=levels)
plt.title("tricontourf")


plt.tight_layout()
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微小冷

请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值