|
| 1 | +""" |
| 2 | +=================== |
| 3 | +3D box surface plot |
| 4 | +=================== |
| 5 | +
|
| 6 | +Given data on a gridded volume ``X``, ``Y``, ``Z``, this example plots the |
| 7 | +data values on the volume surfaces. |
| 8 | +
|
| 9 | +The strategy is to select the data from each surface and plot |
| 10 | +contours separately using `.axes3d.Axes3D.contourf` with appropriate |
| 11 | +parameters *zdir* and *offset*. |
| 12 | +""" |
| 13 | + |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +import numpy as np |
| 16 | + |
| 17 | +# Define dimensions |
| 18 | +Nx, Ny, Nz = 100, 300, 500 |
| 19 | +X, Y, Z = np.meshgrid(np.arange(Nx), np.arange(Ny), -np.arange(Nz)) |
| 20 | + |
| 21 | +# Create fake data |
| 22 | +data = (((X+100)**2 + (Y-20)**2 + 2*Z)/1000+1) |
| 23 | + |
| 24 | +kw = { |
| 25 | + 'vmin': data.min(), |
| 26 | + 'vmax': data.max(), |
| 27 | + 'levels': np.linspace(data.min(), data.max(), 10), |
| 28 | +} |
| 29 | + |
| 30 | +# Create a figure with 3D ax |
| 31 | +fig = plt.figure(figsize=(5, 4)) |
| 32 | +ax = fig.add_subplot(111, projection='3d') |
| 33 | + |
| 34 | +# Plot contour surfaces |
| 35 | +_ = ax.contourf( |
| 36 | + X[:, :, 0], Y[:, :, 0], data[:, :, 0], |
| 37 | + zdir='z', offset=0, **kw |
| 38 | +) |
| 39 | +_ = ax.contourf( |
| 40 | + X[0, :, :], data[0, :, :], Z[0, :, :], |
| 41 | + zdir='y', offset=0, **kw |
| 42 | +) |
| 43 | +C = ax.contourf( |
| 44 | + data[:, -1, :], Y[:, -1, :], Z[:, -1, :], |
| 45 | + zdir='x', offset=X.max(), **kw |
| 46 | +) |
| 47 | +# -- |
| 48 | + |
| 49 | + |
| 50 | +# Set limits of the plot from coord limits |
| 51 | +xmin, xmax = X.min(), X.max() |
| 52 | +ymin, ymax = Y.min(), Y.max() |
| 53 | +zmin, zmax = Z.min(), Z.max() |
| 54 | +ax.set(xlim=[xmin, xmax], ylim=[ymin, ymax], zlim=[zmin, zmax]) |
| 55 | + |
| 56 | +# Plot edges |
| 57 | +edges_kw = dict(color='0.4', linewidth=1, zorder=1e3) |
| 58 | +ax.plot([xmax, xmax], [ymin, ymax], 0, **edges_kw) |
| 59 | +ax.plot([xmin, xmax], [ymin, ymin], 0, **edges_kw) |
| 60 | +ax.plot([xmax, xmax], [ymin, ymin], [zmin, zmax], **edges_kw) |
| 61 | + |
| 62 | +# Set labels and zticks |
| 63 | +ax.set( |
| 64 | + xlabel='X [km]', |
| 65 | + ylabel='Y [km]', |
| 66 | + zlabel='Z [m]', |
| 67 | + zticks=[0, -150, -300, -450], |
| 68 | +) |
| 69 | + |
| 70 | +# Set distance and angle view |
| 71 | +ax.view_init(40, -30) |
| 72 | +ax.dist = 11 |
| 73 | + |
| 74 | +# Colorbar |
| 75 | +fig.colorbar(C, ax=ax, fraction=0.02, pad=0.1, label='Name [units]') |
| 76 | + |
| 77 | +# Show Figure |
| 78 | +plt.show() |
0 commit comments