0

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):

enter image description here

with contourf:

with contourf

C Schwenk
  • 21
  • 3
  • 3
    Did you try to change the z-order? `ax1.imshow(img, ...., zorder=3)`? – JohanC Jan 26 '22 at 10:00
  • I cannot reproduce your problem with the code provided on Ubuntu/matplotlib 3.5.1 . However, the code is not the one producing your second image; `alpha = 0.4` or similar is not specified to generate a semitransparent contour plot. – Mr. T Jan 26 '22 at 11:41
  • Thank you JohanC! Im new here so I don't know if I can reply to your comment directly but I posted your answer :) – C Schwenk Jan 27 '22 at 09:53

1 Answers1

0

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!

C Schwenk
  • 21
  • 3