简体   繁体   中英

Plotting array over background map using cartopy

I am trying to plot an numpy array over a background map tile using cartopy. When including the background map, the array is not visible.

I am adding background map tiles using cimgt and geo_axes.add_image() . This method has worked for me before when plotting points with plt.scatter() . I have tried several projections (PlateCarree, Mercator, and EPSG32630) and map tiles (OSM, GoogleTiles). The array contains np.nans and floats.

Here is my code:

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt

# array creation
array = np.asarray([[1, np.nan, np.nan], [1, 1, 1], [2, 2, 1]])
x_coords = np.asarray([690000, 691000, 692000])
y_coords = np.asarray([4958000, 4959000, 496000])

# create figure
fig = plt.figure(figsize=(8, 6), dpi=100)

# create geo axes
projection = ccrs.epsg(32630)
geo_axes = plt.subplot(projection=projection)

# add open street map background
# when commenting the two following lines, the data array is plotted correctly
osm_background = cimgt.OSM()
geo_axes.add_image(osm_background, 14)

# plot dataset
plt.imshow(
    array,
    origin="upper",
    extent=(x_coords[0], x_coords[1], y_coords[0], y_coords[1]),
    transform=projection,
)

# show plot
plt.show()

I can't seem to find what is causing the issue. Has anyone encountered this before, or can anyone see what I am doing wrong?

You need some tricks to reveal all the plotted features. Here is the relevant code to update yours, and the output plot that shows both the (OSM) background and the array-image.

# plot dataset
plt.imshow(
    array,
    origin="upper",
    extent=(x_coords[0], x_coords[1], y_coords[0], y_coords[1]),
    transform=projection,
    alpha=0.25,  # allows the background image show-through
    zorder=10    # make this layer on top
)
# draw graticule and labels
geo_axes.gridlines(color='lightgrey', linestyle='-', draw_labels=True)

The result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM