为什么我的直方图没有显示在 Jupyter notebook 上?

Why isn't my histogram showing on Jupyter notebook?

所以我对编码还比较陌生,最近承担了一项艰巨的任务,即为我的理学硕士论文构建一些气候模型。使用此代码,我对其进行了调整,现在它不显示任何错误消息,只是现在它不显示任何数字作为输出。有什么解决办法吗?

我输入

%matplotlib notebook 在代码的顶部,并将 plt.show(); 放在脚本的底部(根据一些类似查询的一些建议)......但仍然不起作用.在此之前,它显示

我认为这可能是问题所在,但我无法弄清楚为什么有 0 个轴?

任何 recommendations/solutions?

谢谢!

根据要求 - 我的代码:

import iris.quickplot as qplt
import iris.analysis.cartography
import matplotlib.dates as mdates

def main():
    Current45 = '....X.nc'

    Current45 = iris.load_cube(Current45)

    lats = iris.coords.DimCoord(Current45.coords()[1].points[:,0], \
                                standard_name='latitude', units='degrees')
    lons = Current45.coords()[2].points[0]
    for i in range(len(lons)):
        if lons[i]>100.:
            lons[i] = lons[i]-360.
    lons = iris.coords.DimCoord(lons, \
                                standard_name='longitude', units='degrees')
    Current45.remove_coord('latitude')
    Current45.remove_coord('longitude')
    Current45.add_dim_coord(lats, 1)
    Current45.add_dim_coord(lons, 2)

    Current45.convert_units('Celsius') 

    Colombia = iris.Constraint(longitude=lambda v: -74.73 <= v <= -76.20, \
                               latitude=lambda v: 5.30 <= v <= 4.43) 

    Current45 = Current45.extract(Colombia) 

    iriscc.add_day_of_year(Current45, 'time') 

    Current45.coord('latitude').guess_bounds()
    Current45.coord('longitude').guess_bounds()

    Current45_grid_areas = iris.analysis.cartography.area_weights(Current45)

    Current45 = Current45.collapsed(['latitude', 'longitude'],
                                               iris.analysis.MEAN,
                                               
    weights=Current45_grid_areas)    

    Histogram = Current45.data

    #frq, bins, patches = plt.hist(Histogram, bins=np.arange(20,37,2))
    frq, bins, patches = plt.hist(Histogram, bins=np.arange(16,45,2), color='blue')
    print (frq)


    thresh = 32
    plt.axvline(x=thresh, color='green', linestyle='dashed', linewidth=2)      

    plt.xlabel("Daily Max Temperature / Celsius")
    plt.ylabel("Number of days")

fig = plt.gcf()
plt.show();

My code with blank figure at the bottom

在代码中,您从未调用 main 函数,因此您显示的图形是空的。

您应该在 plt.gcf()plt.show 之前的某个位置调用 main()

编辑

更详细:

您正在这段代码中编写您的 main() 函数,然后在没有缩进的情况下调用 pyplot 以获取当前的 figure,其中 pyplot 只是给你一个空的 figure 返回(在你的代码中 gcf()-call 不是必需的)并且 plt.show() 没有显示空图。

您可以或不能将 plt.show() 移动到您的 main() 函数中,但在某一时刻您一定要调用该函数,否则 none 将被执行。

编辑 2:

# function definition
def main():
    ...

# function call
main()

# show figure
plt.show()