编程语言
首页 > 编程语言> > 【python】matplotlib画3D图

【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)

写成图中形式会报错

标签:figure,python,matplotlib,np,add,fig,Axes3D,ax,3D
来源: https://www.cnblogs.com/xyq-deeplearning/p/16529639.html