I’m trying to plot Cartopy tiles as a surface for my 3D scatter at z=0
.
To achieve this, I’ve created a 2D figure for the map data using the extents of my coordinates. The 2D figure looks correct. It shows the map correctly, and the extents are the same as the ones calculated from the data.
When plotting the map on the 3D figure, I used fig_2d.canvas.draw()
and fig_2d.canvas.renderer.buffer_rgba()
to recover the map image as a numpy array.
After doing so, I plotted an empty surface and the map together. The extent of the map has not changed and looks correct. However, the map is not plotted over the entire surface and whitespaces are still showing on the sides.
Here is the code I put in place:
# Create a new figure
fig = plt.figure(figsize=(12, 8), layout='tight')
ax = fig.add_subplot(111, projection='3d',computed_zorder=False)
# Define the map boundaries
min_lat, max_lat = np.round(data_ts['Lat'].min() - 0.1, 5), np.round(data_ts['Lat'].max() + 0.1, 5)
min_lon, max_lon = np.round(data_ts['Lon'].min() - 0.1, 5), np.round(data_ts['Lon'].max() + 0.1, 5)
print(min_lat, max_lat, min_lon, max_lon)
# Set up the map tiles
tiles = cimgt.GoogleTiles()
tiler = tiles.crs
# Create a 2D plot to fetch the map tiles and then use the extent to plot in 3D
fig_2d = plt.figure(figsize=(10, 10), frameon=False)
ax_2d = fig_2d.add_subplot(111, projection=tiler)
ax_2d.set_axis_off()
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)
# plt.axis('off')
plt.margins(0,0)
ax_2d.set_extent([min_lon, max_lon, min_lat, max_lat], crs=ccrs.PlateCarree())
ax_2d.add_image(tiles, 12) # Zoom level
plt.tight_layout()
fig_2d.savefig(os.path.join(output_path,f"{flight}.2d3d.tif"), format='tif',dpi=300)
# Extract the map image from the 2D plot
fig_2d.canvas.draw()
map_img = np.array(fig_2d.canvas.renderer.buffer_rgba())
map_img = np.flipud(map_img)
# Convert the map image to a texture
x = np.linspace(min_lon, max_lon, map_img.shape[1])
y = np.linspace(min_lat, max_lat, map_img.shape[0])
print(y.min(), y.max(), x.min(),x.max())
x, y = np.meshgrid(x, y)
z = np.zeros_like(x)
# Plot the texture on the z=0 plane
ax.plot_surface(x, y, z, rstride=1, cstride=1, zorder=0)
ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=map_img / 255, zorder=1)
# Plot the 3D scatter plot
sc = ax.scatter(data_ts['Lon'], data_ts['Lat'], data_ts['Alt'], c=data_ts['Alt'], vmin=cmin, vmax=cmax, cmap='turbo', marker='o', alpha=0.5, zorder=2)
ax.scatter([min_lon, min_lon, max_lon, max_lon], [min_lat, max_lat, min_lat, max_lat])
ax.set_xlim(min_lon, max_lon)
ax.set_ylim(min_lat, max_lat)
ax.set_zlim(0, data_ts['Alt'].max())
# Add color bar
cb = plt.colorbar(sc, ax=ax, shrink=0.5, aspect=5)
cb.set_label('Altitude (m)')
# Label axes
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_zlabel('Altitude (m)')
# Set the view angle for better visualization
ax.view_init(elev=30, azim=-60)
data_ts
is a pandas dataframe with the Lat, Lon, Alt, Positions.
The result shows that the image has some whitespace still around.
Resulting image
Charbel Abdallah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.