给我在Python中3D的折线图,散点图,柱状图,曲面图的代码含有import numpy as np
时间: 2024-04-30 09:24:25 浏览: 7
3D折线图:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Generate some data
t = np.linspace(0, 10*np.pi, 1000)
x = np.sin(t)
y = np.cos(t)
z = t
# Create figure and 3D axis
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot 3D line
ax.plot(x, y, z)
# Show plot
plt.show()
```
3D散点图:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Generate some data
np.random.seed(19680801)
n = 100
x, y, z = np.random.rand(3, n)
# Create figure and 3D axis
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot 3D scatter plot
ax.scatter(x, y, z, c=z, cmap='viridis')
# Show plot
plt.show()
```
3D柱状图:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Generate some data
np.random.seed(19680801)
n = 10
x, y, z = np.indices((n, n, n))
voxels = (x < 5) & (y < 5) & (z < 5)
# Create figure and 3D axis
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot 3D bar plot
ax.voxels(voxels)
# Show plot
plt.show()
```
3D曲面图:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Generate some data
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Create figure and 3D axis
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot 3D surface
ax.plot_surface(X, Y, Z)
# Show plot
plt.show()
```