【问题标题】:Why isn't my histogram showing on Jupyter notebook?为什么我的直方图没有显示在 Jupyter 笔记本上?
【发布时间】:2020-10-27 18:27:12
【问题描述】:

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

我输入

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

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

有什么建议/解决方案吗?

谢谢!

根据要求 - 我的代码:

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

【问题讨论】:

  • 如果你没有轴,据我所知pyplot 也无法显示任何内容。您能否发布您的代码 (minimal example) 以帮助我们帮助您解决问题?
  • 代码图像似乎表明函数main 从未被调用过。只需在没有缩进的地方添加main()
  • 感谢@Dorian,我现在已经包含了代码(不知道“最小”的真正含义,考虑到我认为所有这些对于理解为什么没有显示数字是必要的(TL:DR;我是代码新手)
  • 我认为这种混淆来自这样一个事实,即许多编程语言寻找一个main 函数并执行它。但是python不是那样的;您必须主动调用您编写的每个函数才能执行它
  • 如果问题得到解决,您可能会关闭该问题吗?谢谢。

标签: numpy matplotlib jupyter-notebook netcdf python-iris


【解决方案1】:

在代码中,你从不调用main函数,所以你显示的图是空的。

您应该在代码中的某个位置调用main(),位于plt.gcf()plt.show 之前。

编辑

更详细的:

您正在这段代码的 sn-p 中编写您的 main() 函数,然后,在没有缩进的情况下,您调用 pyplot 来获取当前的 figure,其中 pyplot 只是给你 en 空的 @987654329 @back(gcf()-call 在您的代码中无论如何都不是必需的)和plt.show() 没有显示空图。

您可以或不能将plt.show() 移动到main() 函数中,但在某一时刻,您必须绝对调用该函数,否则不会执行任何函数。

编辑 2:

# function definition
def main():
    ...

# function call
main()

# show figure
plt.show()

【讨论】:

  • 好的,谢谢你,我应该在什么时候输入 main() ?有点被你的解释弄糊涂了
  • 在您的代码末尾。您正在定义您的函数,但从不调用它。这基本上意味着,在您编写完所有代码后,它将不会被执行。
  • 想想你写了什么代码:在某处有一个print() 语句,但没有任何东西打印到控制台。这应该会提示您代码未执行。
  • 好的,伙计,我现在把它放在脚本的末尾,并且有一些重新设置,但我无法让这个脚本工作!现在它说:“OSError:指定的文件之一不存在:”并突出显示上面的 .nc 文件。我一生都无法理解为什么它无法识别 .nc 文件?
  • 这是一个单独的问题,因此请考虑针对该问题重新打开另一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-15
  • 2021-04-16
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 2020-07-22
相关资源
最近更新 更多