|
| 1 | +Set equal aspect ratio for 3D plots |
| 2 | +----------------------------------- |
| 3 | + |
| 4 | +Users can set the aspect ratio for the X, Y, Z axes of a 3D plot to be 'equal', |
| 5 | +'equalxy', 'equalxz', or 'equalyz' rather than the default of 'auto'. |
| 6 | + |
| 7 | +.. plot:: |
| 8 | + :include-source: true |
| 9 | + |
| 10 | + aspects = ('auto', 'equal', 'equalxy', 'equalyz', 'equalxz') |
| 11 | + fig, axs = plt.subplots(1, len(aspects), subplot_kw={'projection': '3d'}) |
| 12 | + |
| 13 | + # Draw rectangular cuboid with side lengths [1, 1, 2] |
| 14 | + r = [0, 1] |
| 15 | + scale = np.array([1, 1, 2]) |
| 16 | + pts = itertools.combinations(np.array(list(itertools.product(r, r, r))), 2) |
| 17 | + for start, end in pts: |
| 18 | + if np.sum(np.abs(start - end)) == r[1] - r[0]: |
| 19 | + for ax in axs: |
| 20 | + ax.plot3D(*zip(start*scale, end*scale), color='C0') |
| 21 | +
|
| 22 | + # Set the aspect ratios |
| 23 | + for i, ax in enumerate(axs): |
| 24 | + ax.set_box_aspect((3, 4, 5)) |
| 25 | + ax.set_aspect(aspects[i]) |
| 26 | + ax.title(f"set_aspect('{aspects[i]}')") |
| 27 | + |
| 28 | + plt.show() |
0 commit comments