在不同的平面中创建二维条形图

演示制作3D绘图,其中2D条形图投影到平面y = 0,y = 1等。

在不同的平面中创建二维条形图示例

  1. # This import registers the 3D projection, but is otherwise unused.
  2. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. # Fixing random state for reproducibility
  6. np.random.seed(19680801)
  7. fig = plt.figure()
  8. ax = fig.add_subplot(111, projection='3d')
  9. colors = ['r', 'g', 'b', 'y']
  10. yticks = [3, 2, 1, 0]
  11. for c, k in zip(colors, yticks):
  12. # Generate the random data for the y=k 'layer'.
  13. xs = np.arange(20)
  14. ys = np.random.rand(20)
  15. # You can provide either a single color or an array with the same length as
  16. # xs and ys. To demonstrate this, we color the first bar of each set cyan.
  17. cs = [c] * len(xs)
  18. cs[0] = 'c'
  19. # Plot the bar graph given by xs and ys on the plane y=k with 80% opacity.
  20. ax.bar(xs, ys, zs=k, zdir='y', color=cs, alpha=0.8)
  21. ax.set_xlabel('X')
  22. ax.set_ylabel('Y')
  23. ax.set_zlabel('Z')
  24. # On the y axis let's only label the discrete values that we have data for.
  25. ax.set_yticks(yticks)
  26. plt.show()

下载这个示例