【python】matplotlib画3D图

2022/7/29 1:22:56

本文主要是介绍【python】matplotlib画3D图,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

画点:

点击查看代码
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
 
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
 
fig = plt.figure()
ax = Axes3D(fig, auto_add_to_figure=False)
fig.add_axes(ax)
 
ax.scatter(x,y,z,s=10,color="r",marker='o')
 
plt.show()

画线:

点击查看代码
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
 
zline = np.linspace(0,15,1000)
xline = np.sin(zline)
yline = np.cos(zline)
 
fig = plt.figure()
ax = Axes3D(fig, auto_add_to_figure=False)
fig.add_axes(ax)
 
ax.plot(xline,yline,zline)
 
plt.show()

关于ax = Axes3D(fig)的警告:/var/folders/y1/x8ktr6kd0jz8dms037zl0_t00000gn/T/ipykernel_83156/2478856790.py:1: MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure is deprecated since 3.4. Pass the keyword argument auto_add_to_figure=False and use fig.add_axes(ax) to suppress this warning. The default value of auto_add_to_figure will change to False in mpl3.5 and True values will no longer work in 3.6. This is consistent with other Axes classes.

改成下述版本
ax = Axes3D(fig, auto_add_to_figure=False)
fig.add_axes(ax)

在控制台中逐行运行代码,不报错也不显示图片的解决办法:

下述代码不能在控制台中分开,要写在一起运行
fig = plt.figure()
ax = Axes3D(fig, auto_add_to_figure=False)
fig.add_axes(ax)

写成图中形式会报错



这篇关于【python】matplotlib画3D图的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程