python画图3d柱状图,3d直方图 ax.bar3d
时间: 2023-06-10 07:03:52 浏览: 221
您可以使用Python的Matplotlib库中的mpl_toolkits.mplot3d来绘制3D柱状图和直方图,其中ax.bar3d函数可以用于创建3D柱状图。
例如,以下是一个使用ax.bar3d绘制3D柱状图的示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 数据
x = np.random.randint(0, 10, 10)
y = np.random.randint(0, 10, 10)
z = np.random.randint(0, 10, 10)
dx = np.ones_like(x)
dy = np.ones_like(y)
dz = z
# 绘制3D柱状图
ax.bar3d(x, y, z, dx, dy, dz)
# 添加轴标签
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
```
如果您想要绘制3D直方图,可以使用ax.hist3d函数。以下是一个使用ax.hist3d绘制3D直方图的示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 数据
x = np.random.randn(100)
y = np.random.randn(100)
z = np.random.randn(100)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 绘制3D直方图
hist, xedges, yedges = np.histogram2d(x, y, bins=10)
xpos, ypos = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:])
xpos = xpos.flatten()/2.
ypos = ypos.flatten()/2.
zpos = np.zeros_like(xpos)
dx = xedges[1] - xedges[0]
dy = yedges[1] - yedges[0]
dz = hist.flatten()
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')
# 添加轴标签
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
```
希望这能帮到您!
阅读全文