JustANoob
JustANoob

Reputation: 620

Visualize terrain ground elevation and water depth in the same plot

I would like to get some tips on how to properly visualize/plot two 2-dimensional arrays of the same shape, say ground_arr and water_arr. ground_arr represents the elevation of some surface, and water_arr represents the height/depth of water on top of that surface. The total elevation is then ofc ground_arr + water_arr.

For now im using plt.imshow(water_arr, cmap=...) to only see the water and plt.imshow(water_arr+ ground_arr) to see the total elevation but i would like to merge both of them in the same plot, to get some map alike plot.

Any tips?

Upvotes: 0

Views: 476

Answers (1)

JohanC
JohanC

Reputation: 80409

Supposing you have 2D arrays of height values for the terrain and for the water level. And that the water level is set to zero at the places without water.

Just set the water level to Nan where you want the water image to be transparent.

import numpy as np
import matplotlib.pyplot as plt

# Generate test data, terrain is some sine on the distance to the center
terrain_x, terrain_y = np.meshgrid(np.linspace(-15, 15, 1000), np.linspace(-15, 15, 1000))
r = np.sqrt(terrain_x * terrain_x + terrain_y * terrain_y)
terrain_z = 5 + 5 * np.sin(r)

# test data for water has some height where r is between 3 and 4 pi, zero everywhere else
water_z = np.where(3 * np.pi < r, 3 - terrain_z, 0)
water_z = np.where(4 * np.pi > r, water_z, 0)

extent = [-15, 15, -15, 15]

fig, (ax1, ax2, ax3) = plt.subplots(ncols=3)

ax1.imshow(terrain_z, cmap="YlOrBr", extent=extent)
ax1.set_title('Terrain')

ax2.imshow(water_z, cmap="Blues", extent=extent)
ax2.set_title('Water')

ax3.imshow(terrain_z, cmap="YlOrBr", extent=extent)
water_z = np.where(water_z > 0, water_z, np.nan)
ax3.imshow(water_z, cmap="Blues", extent=extent)
ax3.set_title('Combined')

plt.show()

resulting plots

Upvotes: 1

Related Questions