# 使用 Seaborn 和 Matplotlib 创建 3D 绘图

Salman Mehmood 2022年12月21日 2022年5月26日

## 使用 Seaborn 和 Matplotlib 创建 3D 绘图

``````import seaborn as seaborn
import matplotlib.pyplot as plot
import numpy as np
``````

``````mean = 3
number =50

x1 = np.random.normal(mean, 1, size=number)
y1 = np.random.normal(mean, 1, size=number)
z1 = np.random.normal(mean, 1, size=number)
``````

``````plot.figure(figsize=(6,5))
axes = plot.axes(projection='3d')
``````

``````print(type(axes))
``````

``````<class 'matplotlib.axes._subplots.Axes3DSubplot'>
``````

``````import seaborn as seaborn
import matplotlib.pyplot as plot
import numpy as np

seaborn.set_style("darkgrid")

mean = 3
number =50

x1 = np.random.normal(mean, 1, size=number)
y1 = np.random.normal(mean, 1, size=number)
z1 = np.random.normal(mean, 1, size=number)

plot.figure(figsize=(6,5))
axes = plot.axes(projection='3d')
print(type(axes))
axes.scatter3D(x1, y1, z1)

axes.set_xlabel('x')
axes.set_ylabel('y')
axes.set_zlabel('z')
plot.show()
``````

``````import seaborn as seaborn
import matplotlib.pyplot as plot
import numpy as np

seaborn.set_style("darkgrid")

mean = 3
number =50

x1 = np.random.normal(mean, 1, size=number)
y1 = np.random.normal(mean, 1, size=number)
z1 = np.random.normal(mean, 1, size=number)

plot.figure(figsize=(6,5))
axes = plot.axes(projection='3d')
print(type(axes))
axes.scatter3D(x1, y1, z1,s=100)

axes.set_xlabel('x')
axes.set_ylabel('y')
axes.set_zlabel('z')
plot.show()
``````

``````import seaborn as seaborn
import matplotlib.pyplot as plot
import numpy as np

seaborn.set_style("darkgrid")

mean = 3
number =50

x1 = np.random.normal(mean, 1, size=number)
y1 = np.random.normal(mean, 1, size=number)
z1 = np.random.normal(mean, 1, size=number)

plot.figure(figsize=(6,5))
axes = plot.axes(projection='3d')
print(type(axes))
axes.scatter3D(x1, y1, z1)

axes.set_xlabel('x')
axes.set_ylabel('y')
axes.set_zlabel('z')

axes.view_init(45, 215)
plot.show()
``````

Matplotlib 提供了创建线图的选项，我们将创建一些新数据来炫耀。我们需要创建 `z`，一个从 0 到 10 的线性空间，然后根据 `z` 轴的余弦和正弦创建 `x``y`

`scatter3D()` 一样，我们调用 `plot3D()`，这将给我们一个线图。

``````import seaborn as sb
import matplotlib.pyplot as plot
import numpy as np

sb.set_style("whitegrid")

OMEGA = 2

Z1 = np.linspace(0, 10, 100)
X1 = np.cos(OMEGA*Z1)
Y1= np.sin(OMEGA*Z1)

plot.figure(figsize=(6,5))
axes = plot.axes(projection='3d')
axes.plot3D(X1,Y1,Z1)
# keeps padding between figure elements
plot.tight_layout()
plot.show()
``````

``````import seaborn as sb
import matplotlib.pyplot as plot
import numpy as np

sb.set_style("whitegrid")

OMEGA =4

Z1 = np.linspace(0, 10, 100)
X1 = np.cos(OMEGA*Z1)
Y1= np.sin(OMEGA*Z1)

plot.figure(figsize=(6,5))
axes = plot.axes(projection='3d')
axes.plot3D(X1,Y1,Z1,lw=3)
# keeps padding between figure elements
plot.tight_layout()
plot.show()
``````

``````def FUNC_Z(x, y):
return 50 - (x**2 + y**2)
``````

``````X1, Y1 = np.meshgrid(X_VAL, Y_VAL)
``````

``````import seaborn as sb
import matplotlib.pyplot as plot
import numpy as np

def FUNC_Z(x, y):
return 50 - (x**2 + y**2)

sb.set_style('whitegrid')
N = 50

X_VAL = np.linspace(-5, 5, N)
Y_VAL = np.linspace(-5, 5, N)

X1, Y1 = np.meshgrid(X_VAL, Y_VAL)

Z1 = FUNC_Z(X1, Y1)

axes = plot.axes(projection='3d')
axes.plot_surface(X1, Y1, Z1)
plot.show()
``````

``````import seaborn as sb
import matplotlib.pyplot as plot
import numpy as np

def FUNC_Z(x, y):
return 50 - (x**2 + y**2)

sb.set_style('whitegrid')
N = 50

X_VAL = np.linspace(-5, 5, N)
Y_VAL = np.linspace(-5, 5, N)

X1, Y1 = np.meshgrid(X_VAL, Y_VAL)

Z1 = FUNC_Z(X1, Y1)

axes = plot.axes(projection='3d')
axes.plot_wireframe(X1, Y1, Z1)
plot.show()
``````

``````import seaborn as sb
import matplotlib.pyplot as plot
import numpy as np

def FUNC_Z(x, y):
return 50 - (x**2 + y**2)

sb.set_style('whitegrid')
N = 10

X_VAL = np.linspace(-5, 5, N)
Y_VAL = np.linspace(-5, 5, N)

X1, Y1 = np.meshgrid(X_VAL, Y_VAL)

Z1 = FUNC_Z(X1, Y1)

axes = plot.axes(projection='3d')
axes.plot_wireframe(X1, Y1, Z1)
plot.show()
``````

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.