Python 绘制数据(配置文件)

标签 python matplotlib plot netcdf netcdf4

我的物理项目有一个问题,即使我不知道如何编程,我也必须使用 Python netCDF4。我已经阅读了我的数据,并且看到了许多不同的教程,但我不知道下一步该做什么。我为此挣扎了很长时间。

from netCDF4 import Dataset as ncf
file='atmPrf_C001.2010.227.00.03.G04_2013.3520_nc'
data=ncf(file,'r’)
print(infile.dimensions)
print(data.variables)

vertical=data.variables['MSL_alt']
latitude=data.variables['Lat']
longitude=data.variables['Lon']
ang=data.variables['Bend_ang']

输出:

OrderedDict([(u'Lat', <type 'netCDF4._netCDF4.Variable'>
float32 Lat(MSL_alt)
    units: deg
    valid_range: [-90.  90.]
    _FillValue: -999.0
    long_name: Latitude of perigee point
unlimited dimensions:
current shape = (2849,)
filling off
), (u'Lon', <type 'netCDF4._netCDF4.Variable'>
float32 Lon(MSL_alt)
    units: deg
    valid_range: [-180.  180.]
    _FillValue: -999.0
    long_name: Longitude of perigee point
unlimited dimensions:
current shape = (2849,)
filling off
), (u'MSL_alt', <type 'netCDF4._netCDF4.Variable'>
float32 MSL_alt(MSL_alt)
    units: km
    valid_range: [   0.  999.]
    _FillValue: -999.0
    long_name: Mean sea level altitude of perigee point
unlimited dimensions:
current shape = (2849,)
filling off
), (u'Ref', <type 'netCDF4._netCDF4.Variable'>
float32 Ref(MSL_alt)
    units: N
    valid_range: [    0.  9999.]
    _FillValue: -999.0
    long_name: Refractivity
unlimited dimensions:
current shape = (2849,)
filling off
), (u'Azim', <type 'netCDF4._netCDF4.Variable'>
float32 Azim(MSL_alt)
    units: deg
    valid_range: [    0.  1500.]
    _FillValue: -999.0
    long_name: Azimuth of occultation plane wrt North
unlimited dimensions:
current shape = (2849,)
filling off
), (u'Pres', <type 'netCDF4._netCDF4.Variable'>
float32 Pres(MSL_alt)
    units: mb
    valid_range: [    0.  1500.]
    _FillValue: -999.0
    long_name: Dry pressure
unlimited dimensions:
current shape = (2849,)
filling off
), (u'Temp', <type 'netCDF4._netCDF4.Variable'>
float32 Temp(MSL_alt)
    units: C
    valid_range: [-200.  100.]
    _FillValue: -999.0
    long_name: Dry temperature
unlimited dimensions:
current shape = (2849,)
filling off
), (u'Bend_ang', <type 'netCDF4._netCDF4.Variable'>
float64 Bend_ang(MSL_alt)
    units: rad
    valid_range: [  0.  10.]
    _FillValue: -999.0
    long_name: Raw (unoptimized) bending angle
unlimited dimensions:
current shape = (2849,)
filling off
), (u'Opt_bend_ang', <type 'netCDF4._netCDF4.Variable'>
float64 Opt_bend_ang(MSL_alt)
    units: rad
    valid_range: [  0.  10.]
    _FillValue: -999.0
    long_name: Optimized bending angle
unlimited dimensions:
current shape = (2849,)
filling off
), (u'Impact_height', <type 'netCDF4._netCDF4.Variable'>
float32 Impact_height(MSL_alt)
    units: km
    valid_range: [   0.  999.]
    _FillValue: -999.0
    long_name: Impact height
unlimited dimensions:
current shape = (2849,)
filling off
), (u'Bend_ang_stdv', <type 'netCDF4._netCDF4.Variable'>
float32 Bend_ang_stdv(MSL_alt)
    units: rad
    valid_range: [  0.  10.]
    _FillValue: -999.0
    long_name: Bending angle uncertainty
unlimited dimensions:
current shape = (2849,)
filling off
), (u'Bend_ang_conf', <type 'netCDF4._netCDF4.Variable'>
int32 Bend_ang_conf(MSL_alt)
    units: %
    valid_range: [  0 100]
    _FillValue: -999
    long_name: Bending angle confidence
unlimited dimensions:
current shape = (2849,)
filling off
), (u'OL_par', <type 'netCDF4._netCDF4.Variable'>
float64 OL_par(OL_par)
    _FillValue: -999.0
unlimited dimensions:
current shape = (20,)
filling off
), (u'OL_ipar', <type 'netCDF4._netCDF4.Variable'>
int32 OL_ipar(OL_par)
    _FillValue: -999
unlimited dimensions:
current shape = (20,)
filling off
), (u'OL_vec1', <type 'netCDF4._netCDF4.Variable'>
float64 OL_vec1(OL_vec)
    _FillValue: -999.0
unlimited dimensions:
current shape = (7949,)
filling off
), (u'OL_vec2', <type 'netCDF4._netCDF4.Variable'>
float64 OL_vec2(OL_vec)
    _FillValue: -999.0
unlimited dimensions:
current shape = (7949,)
filling off
), (u'OL_vec3', <type 'netCDF4._netCDF4.Variable'>
float64 OL_vec3(OL_vec)
    _FillValue: -999.0
unlimited dimensions:
current shape = (7949,)
filling off
), (u'OL_vec4', <type 'netCDF4._netCDF4.Variable'>
float64 OL_vec4(OL_vec)
    _FillValue: -999.0
unlimited dimensions:
current shape = (7949,)
filling off
)])

我不知道下一步该做什么,我能做的最后一件事就是声明变量,我应该关注弯曲角度,但我在这个数据中没有看到它,它应该有多大或应该有多大我知道声明尺寸吗?我正在向某人寻求提示,因为这个自愿的科学项目已经让我发疯了。谢谢。

更新:我只是尝试将其原始绘制:

plt.plot(ang)
plt.show()

得到这个:enter image description here

最佳答案

当您编写 ang=data.variables['Bend_ang'] 时,会返回一个对象(NetCDF4-variable),该对象(除了数据之外)还包含通常的单位、尺寸等内容,名称,您可以通过以下方式访问:

ang_units = data.variables['Bend_ang'].units
ang_dims  = data.variables['Bend_ang'].dimensions

可以使用以下方法将数据本身读取到 Numpy 数组:

vertical_data = data.variables['MSL_alt'][:]
ang_data      = data.variables['Bend_ang'][:]

在您的情况下,这些将是一维数组,可以简单地使用以下方式绘制:

plt.plot(ang_data, vertical_data)

关于Python 绘制数据(配置文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46229737/

相关文章:

python - 如何从python字符串中删除连续的单字母字符

matplotlib - Seaborn 中的堆积条形图

python - ipython 笔记本水平排列图

python - 垂直填充 3d matplotlib 图

python - 如何更改 Pandas 时间序列图的 x-ticks 密度?

python - 如何使 plotly 图例跨越两列

python - NoReverseMatch 位于/polls/

python - 将 neo4j 查询结果加载到 python 的 `igraph` 图中

python - 在 Pandas 数据框中删除特定的 multiIndex 列

python - pandas matplotlib .plot(kind ='hist') vs .plot(kind ='bar') 问题