import netCDF4 as nc import numpy as np from netCDF4 import Dataset import matplotlib.pyplot as plt from matplotlib.cm import get_cmap from matplotlib.colors import from_levels_and_colors import cartopy.crs as crs import cartopy.feature as cfeature from cartopy.feature import NaturalEarthFeature from wrf import to_np, getvar, interplevel, smooth2d, get_cartopy, cartopy_xlim, cartopy_ylim, latlon_coords, vertcross, smooth2d, CoordPair, GeoBounds,interpline import warnings warnings.filterwarnings('ignore') file = 'D:/transfer/wrfout_d01_2016-03-01_00_00_00' dataset = nc.Dataset(file) latitude = dataset.variables['XLAT'][0][:] longitude = dataset.variables['XLONG'][0][:] tp1 = dataset.variables['RAINC'][1][:][:] co = dataset.variables['co'][1][1][:][:] time = dataset.variables['Times'][:] co2 = dataset.variables['co2'][:] #var = ds.variables['co2'] #print(co2[:]) plt.imshow(co2[ :, :, 98, 78], cmap='hot_r', vmax=400, vmin=350, alpha=0.5) plt.colorbar() #plt.scatter(latitude,longitude, c=co, s=3, cmap='Reds', vmax=1, vmin=0) proj = crs.PlateCarree(central_longitude=180) proj_data = crs.PlateCarree()#LambertCylindrical() #plt.contourf(co[:, :, 98, 78], cmap='hot') fig , ax = plt.subplots(1,1,figsize=(8,8),subplot_kw={'projection':proj}) #plt.imshow(longitude, latitude, co) ax.set_title('CO2 concentration') #ax.set_xlabel('Longitude') #ax.set_ylabel('Latitude') ax.add_feature(cfeature.COASTLINE.with_scale('50m'),lw=0.5) ax.add_feature(cfeature.BORDERS) leftlon, rightlon, lowerlat, upperlat = (90, 110, 4, 31) ######## 调节绘图经纬度范围 Region = [leftlon, rightlon, lowerlat, upperlat] ax.set_extent(Region, crs=proj_data) #经纬度范围,坐标参考系转换 plt.show()
时间: 2024-02-13 13:01:39 浏览: 29
好的,以下是对代码的优化建议:
1. 合并import语句,将所有的import语句放在代码开头。
2. 删除无用的import语句,例如`from wrf import to_np, getvar, interplevel, smooth2d, get_cartopy, cartopy_xlim, cartopy_ylim, latlon_coords, vertcross, smooth2d, CoordPair, GeoBounds,interpline`。
3. 将文件路径和变量名使用下划线(_)分隔,以符合Python的命名规范。
4. 避免使用全局变量,将变量定义在函数内部并通过函数参数传递。
5. 删除无用的代码,例如`co`和`time`这两个变量没有在后面的代码中使用到。
6. 将图形的标题、坐标轴标签等设置放在`ax`对象的方法中,避免使用`plt`函数。
7. 删除注释中的中文字符。
8. 将代码缩进调整为4个空格,以符合Python的缩进规范。
下面是优化后的代码:
```python
import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as crs
import cartopy.feature as cfeature
warnings.filterwarnings('ignore')
def plot_co2_concentration(file_path):
dataset = nc.Dataset(file_path)
latitude = dataset.variables['XLAT'][0][:]
longitude = dataset.variables['XLONG'][0][:]
co2 = dataset.variables['co2'][:]
fig, ax = plt.subplots(1, 1, figsize=(8, 8), subplot_kw={'projection': crs.PlateCarree(central_longitude=180)})
ax.set_title('CO2 concentration')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.add_feature(cfeature.COASTLINE.with_scale('50m'), lw=0.5)
ax.add_feature(cfeature.BORDERS)
leftlon, rightlon, lowerlat, upperlat = (90, 110, 4, 31)
region = [leftlon, rightlon, lowerlat, upperlat]
proj_data = crs.PlateCarree()
ax.set_extent(region, crs=proj_data)
plt.imshow(co2[:, :, 98, 78], cmap='hot_r', vmax=400, vmin=350, alpha=0.5)
plt.colorbar()
plt.show()
```