【问题标题】:Python Display NC File Variable Description as Plot TitlePython 将 NC 文件变量描述显示为绘图标题
【发布时间】:2021-09-29 23:35:53
【问题描述】:

我需要使用“描述”作为我的图表或绘图标题,但我在互联网搜索中找不到这样做的方法。具有我需要的“描述”的 .nc 文件变量的输出如下所示:

<class 'netCDF4._netCDF4.Variable'>
float64 M(lat, on)
    _FillValue: nan
    long_name: Wind Speed at 100m
    description: Anomaly for June 2021 vs the previous 30 years
unlimited dimensions: 
current shape = (2920, 7200) 

我的代码如下所示:

# -*- coding: utf-8 -*-
"""


@author: U321103
"""
from sys import exit
import netCDF4 as nc4
from netCDF4 import Dataset
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
#from mpl_toolkits.basemap import Basemap, cm
import datetime 
from datetime import datetime
import pandas as pd
import xarray as xr
import bottleneck as bn
import cartopy.crs as ccrs
from mpl_toolkits.basemap import Basemap
import os
os.environ["PROJ_LIB"] = 'C:\\Users\\Yury\\anaconda3\\Library\\share'



# -----------------------------------------------------------------------------------------------------------
# 
# -----------------------------------------------------------------------------------------------------------

#%matplotlib inline
#The easiest way to read the data is:
path = "//porfiler03/gtdshare/VORTEX/ANOMALY_FILES/anomaly.M.2021.06.vs30y/world.nc"

# Open the NetCDF file
fh = Dataset(path)

#read variables in fh
for var in fh.variables.values():
    print(var)

# Get the 100m wind speed
wind100 = fh['M'][:]
#wind100_units = fh['M'].units

# Get the latitude and longitude points
lats = fh.variables['lat'][:]
lons = fh.variables['lon'][:]

# Get some parameters for the Stereographic Projection
lon_0 = lons.mean()
lat_0 = lats.mean()

#m = Basemap(width=25000000,height=12000000,
#            resolution='l',projection='lcc',\
 #           lat_ts=50,lat_0=lat_0,lon_0=lon_0)
m = Basemap(projection='merc',llcrnrlat=-40,urcrnrlat=60,\
            llcrnrlon=-180,urcrnrlon=180,lat_ts=20,resolution='c')   
# help on coordinates: https://matplotlib.org/basemap/users/merc.html
fh.close()


# Because our lon and lat variables are 1D,
# use meshgrid to create 2D arrays
# Not necessary if coordinates are already in 2D arrays.
lon, lat = np.meshgrid(lons, lats)
xi, yi = m(lon, lat)

# Plot Data
cs = m.pcolor(xi,yi,np.squeeze(wind100))
# Add Grid Lines
m.drawparallels(np.arange(-80., 81., 40.), labels=[1,0,0,0], fontsize=10)
m.drawmeridians(np.arange(-180., 181., 40.), labels=[0,0,0,1], fontsize=10)

# Add Coastlines, States, and Country Boundaries
m.drawcoastlines()
m.drawstates()
m.drawcountries()

# Add Colorbar
cbar = m.colorbar(cs, location='bottom', pad="10%")
#cbar.set_label(wind100_units)

# Add Title
plt.title(' ')

plt.show()
exit()

所以,我需要将“2021 年 6 月与过去 30 年的异常情况”添加到下面的 plt.title() 行中 - 谢谢!

【问题讨论】:

    标签: python-3.x matplotlib plot title netcdf


    【解决方案1】:

    您应该在fh.close() 之前的某处添加这行代码wind100_description = fh['M'].description。然后只需使用plt.title(wind100_description) 而不是plt.title(' ')。此外,删除不需要的导入是一个好习惯,其中有很多:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-24
      • 2020-01-23
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多