w-d-plot.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D # registers the 3D projection
  4. import matplotlib.cm as cm
  5. import matplotlib.colors as colors
  6. w_vals = np.arange(1, 25)
  7. d_vals = np.arange(1, 25)
  8. W, D = np.meshgrid(w_vals, d_vals) # shape (len(d_vals), len(w_vals))
  9. heights = (W - 1) * (D - 1) + 1 # same shape
  10. x = W.ravel()
  11. y = D.ravel()
  12. z = np.zeros_like(x) # base of bars
  13. dx = dy = 0.6 # bar width in x and y
  14. dz = heights.ravel()
  15. norm = colors.Normalize(vmin=dz.min(), vmax=dz.max())
  16. colors_map = cm.viridis(norm(dz))
  17. fig = plt.figure(figsize=(10, 6))
  18. ax = fig.add_subplot(111, projection='3d')
  19. ax.bar3d(x - dx/2, y - dy/2, z, dx, dy, dz, shade=True, color=colors_map, zsort='average')
  20. ax.set_xlabel('Width')
  21. ax.set_ylabel('Depth')
  22. ax.set_zlabel('#Atomic Models')
  23. ax.set_xticks(w_vals)
  24. ax.set_yticks(d_vals)
  25. ax.set_title("#Atomic Models in function of width and depth")
  26. ax.view_init(elev=30, azim=-135) # example values
  27. # colorbar
  28. mappable = cm.ScalarMappable(norm=norm, cmap=cm.viridis)
  29. mappable.set_array(dz)
  30. cbar = fig.colorbar(mappable, ax=ax, shrink=0.6, pad=0.1)
  31. cbar.set_label('value')
  32. plt.tight_layout()
  33. plt.show()
  34. # Optionally save:
  35. # fig.savefig("3d_bars_matplotlib.png", dpi=200)