简体   繁体   中英

Using matlotlib: why do imshow and contourf not plot together? (contourf "overrides" imshow)

I am trying to plot some meteorological data onto a map and I would like to add an image of a plane using imshow. Plotting i) the trajectory, ii) some contour-data and iii) the image, works fine. But as soon as I add a contourf-plot (see below) the image dissapears!

Any ideas how to fix this?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import cartopy.crs as crs
import cartopy.feature as cfeature

def plot_test():
    #DEFINE DATA

    x,y = np.meshgrid(np.linspace(0,90,100),np.linspace(0,90,100))
    z = x**3 + y**3
    
    #BEGIN FIGURE (IN THIS CASE A MAP, IM PLOTTING METEOROLOGICAL DATA)
    fig = plt.figure(figsize = (6,6))
    ax1 = plt.axes(projection=crs.PlateCarree(central_longitude=0))
    ax1.set_extent([0,90,0,90], crs=crs.PlateCarree())
    ax1.coastlines(resolution='auto', color='k')
    
    #EXAMPLE DATA PLOTTED AS CONTOURF
    v_max = int(z.max())    
    v_min = int(z.min())
    qcs = ax1.contourf(x, y, z, cmap = "Blues", vmin = v_min, vmax = v_max)
    sm = plt.cm.ScalarMappable(cmap="Blues",norm=qcs.norm)
    sm._A = []
    cbar = plt.colorbar(sm, ax=ax1,orientation="vertical")
    cbar.ax.set_ylabel("some contourf data", rotation=90, fontsize = 15)
            
    #PLOT IMAGE OF A PLANE (THIS IS NOT SHOWING UP ON THE PLOT!)
    x0 = 50
    y0 = 40
    img=plt.imread("plane2.png")
    ax1.imshow(img,extent=[x0,x0 - 10, y0, y0-10], label = "plane")

    plt.show()

without contourf (code from above with lines 14-20 commented out):

在此处输入图像描述

with contourf:

带轮廓

Thank you 1000 times @JohanC (see comments). I simply had to place the z-order:

ax1.imshow(img, ...., zorder=3)

which made the plane show up!

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