简体   繁体   English

使用 matlotlib:为什么 imshow 和 contourf 不是 plot 在一起? (轮廓“覆盖”imshow)

[英]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.我正在尝试将 plot 一些气象数据放到 map 上,我想使用 imshow 添加飞机图像。 Plotting i) the trajectory, ii) some contour-data and iii) the image, works fine.绘制 i)轨迹,ii)一些轮廓数据和 iii)图像,工作正常。 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):没有contourf(上面的代码注释掉了第14-20行):

在此处输入图像描述

with contourf:与轮廓:

带轮廓

Thank you 1000 times @JohanC (see comments).感谢@JohanC 1000 次(见评论)。 I simply had to place the z-order:我只需要放置 z 顺序:

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

which made the plane show up!这让飞机出现了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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