Barplot
Axes3D.``bar(left, height, zs=0, zdir=‘z’, *args, **kwargs)
其他参数向下传递给bar函数,返回Patch3DCollection对象
| Argument | Description |
|---|---|
| left | 条形图水平坐标 |
| height | 条形的高度 |
| zs | Z方向 |
| zdir | 同上 |
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
colors = ['r', 'g', 'b', 'y']
yticks = [3, 2, 1, 0]
for c, k in zip(colors, yticks):
# Generate the random data for the y=k 'layer'.
xs = np.arange(20)
ys = np.random.rand(20)
# You can provide either a single color or an array with the same length as
# xs and ys. To demonstrate this, we color the first bar of each set cyan.
cs = [c] * len(xs)
# Plot the bar graph given by xs and ys on the plane y=k with 80% opacity.
ax.bar(xs, ys, zs=k, zdir='y', color=cs, alpha=0.8)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# On the y axis let's only label the discrete values that we have data for.
ax.set_yticks(yticks)
plt.show()

这篇博客展示了如何利用matplotlib库的Axes3D模块创建3D条形图。通过设置不同颜色、高度和Z轴位置,生成了多个条形,并调整了Z轴方向。最后,对Y轴进行了定制,只显示有数据的刻度值,增强了可视化效果。

997

被折叠的 条评论
为什么被折叠?



