简体   繁体   English

Matplotlib.pyplot 只显示最终的 plot

[英]Matplotlib.pyplot only displays final plot

I'm trying to create a simple 1 by 3 subplot of the maps I have.我正在尝试为我拥有的地图创建一个简单的 1 x 3 子图。 They only have 2 dimensions (longitude and latitude) after time averaging.在时间平均之后,它们只有 2 个维度(经度和纬度)。 The final map plots perfectly, but the first two subplots are just blank.最终的 map 绘图完美,但前两个子图只是空白。 Thanks in advance for any advice!提前感谢您的任何建议!

import numpy as np
import xarray as xa
import cmocean.cm as cm
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

lgm = xa.open_dataset('lgm.nc', decode_times=False)
pre = xa.open_dataset('pre-i.nc', decode_times=False)
pd = xa.open_dataset('present.nc', decode_times=False)

def pco2_diff():
    lgm_pco2 = lgm.O_pco2sur
    pre_pco2 = pre.O_pco2sur
    pd_pco2 = pd.O_pco2sur

    #-------------------------Time averaged data-------------------------------
    lgm_pco2_mean = lgm_pco2.mean("time")
    pre_pco2_mean = pre_pco2.mean("time")
    pd_pco2_mean = pd_pco2.mean("time")

    #-----------------Get the ocean-atmosphere fluxes--------------------------
    lgm_pco2_diff = lgm_pco2_mean - 189.65
    pre_pco2_diff = pre_pco2_mean - 277.44
    pd_pco2_diff = pd_pco2_mean - 368.89

    #---------------------Basic plots, 1 at a time-----------------------------
    lgm_pco2_diff.plot()
    pre_pco2_diff.plot()
    pd_pco2_diff.plot()

    #-----------------------------Subplots-------------------------------------
    f, (ax1, ax2, ax3) = plt.subplots(1, 3, sharey=True, sharex=False)
    #1 row, 3 columns, sharing the y-axis, not sharing the x-axis
    ax1 = lgm_pco2_diff.plot(vmin=-300, vmax=300, add_colorbar=False)
    ax2 = pre_pco2_diff.plot(vmin=-300, vmax=300, add_colorbar=False)
    ax3 = pd_pco2_diff.plot(vmin=-300, vmax=300,cmap=cm.thermal)

Maybe try the following:也许尝试以下方法:

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, sharey=True, sharex=False)
ax1.plot(lgm_pco2_diff, vmin=-300, vmax=300, add_colorbar=False)
ax2.plot(pre_pco2_diff, vmin=-300, vmax=300, add_colorbar=False)
ax3.plot(pd_pco2_diff, vmin=-300, vmax=300, cmap=cm.thermal)

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

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