首页 > 解决方案 > Matplotlib.pyplot 只显示最终图

问题描述

我正在尝试为我拥有的地图创建一个简单的 1 x 3 子图。在时间平均之后,它们只有 2 个维度(经度和纬度)。最终的地图绘制完美,但前两个子图只是空白。提前感谢您的任何建议!

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)

标签: pythonmatplotlibsubplot

解决方案


也许尝试以下方法:

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)

推荐阅读