给我一个在Python中3D的折线图,散点图,柱状图,曲面图的代码其中含有import numpy as np
时间: 2024-05-13 16:15:52 浏览: 98
以下是Python中绘制3D折线图、散点图、柱状图和曲面图的代码,依赖于`numpy`和`matplotlib`库。
3D折线图:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 生成数据
x = np.linspace(0, 10, 100)
y = np.sin(x)
z = np.cos(x)
# 绘制3D折线图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
```
3D散点图:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 生成数据
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
# 绘制3D散点图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
```
3D柱状图:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 生成数据
x = np.linspace(0, 10, 10)
y = np.random.rand(10)
z = np.zeros(10)
# 绘制3D柱状图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.bar(x, y, zs=z, zdir='y', alpha=0.8)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
```
3D曲面图:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 生成数据
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
# 绘制3D曲面图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
```
阅读全文