在Python中关于海面温度的图中,如何解决“ NameError:未定义名称” cartopy”?

时间:2019-04-10 23:20:45

标签: python python-3.x python-2.7 sst

我对python真的很陌生,我需要在python中绘制海面温度(sst)的数据netcdf,但它会不断出错。

我在另一个笔记本中使用相同的代码,它运行得很好。

###SST CÓDIGO PLOT
import numpy as np                                                                     
import matplotlib.pyplot as plt                                                    
from numpy import pi                                                                
from mpl_toolkits.basemap import Basemap                                               
from netCDF4 import Dataset                                                               
import pandas as pd                                                               
from scipy import stats                                                           
import seaborn as sns                                                                
import xarray as xr                                                                 
import cartopy.crs as ccrs                                                          
import os                                                                           
from netCDF4 import Dataset as netcdf_dataset                                          
from cartopy import config                                                                       
import statistics                                                                         
import glob                                                                         
import seaborn as sns                                                                                                                          

ds = xr.open_dataset('/home/mayna/Downloads/d86/20190327010000-OSISAF-L3C_GHRSST-SSTsubskin-GOES16-ssteqc_goes16_20190327_010000-v02.0-fv01.0.nc')                                                                              

plt.figure(figsize=(8,4))                                                            
ax = plt.axes(projection=ccrs.PlateCarree())                                         
ax.add_feature(cartopy.feature.BORDERS, linestyle='-', linewidth=0.25)                 
ax.coastlines(linewidth=0.25)                                                        

ds['sea_surface_temperature'][0,:,:].plot.contourf(levels=20, ax=ax, transform=ccrs.PlateCarree(),cmap='coolwarm')                                                                                                                        

它说erro在“ ax.add_feature(cartopy.feature.BORDERS,linestyle ='-',linewidth = 0.25)”行中,“ NameError:未定义名称“ cartopy””。
您认为是什么问题?
附注:我知道我使用了很多不需要的lib

3 个答案:

答案 0 :(得分:1)

您似乎从未定义过cartopy。也许顶部的import cartopy会解决您的问题。

答案 1 :(得分:0)

python中的名称错误意味着特定的属性/方法未导入程序。在代码中,您使用的是cartopy.crs,cartopy.config和cartopy.features.Border,但是只有前两个是通过语句导入的。

import cartopy.crs as crash

from cartopy import config

因此对于feature.Border,您要么

import cartopy 

from cartopy import features.Border #use just features.Border in that line if you are doing this.

答案 2 :(得分:0)

对我来说,我在jupyter中安装了cartopy程序包,但是仍然显示出cartopy的错误(未定义)(我用cartopy.ccrs绘制了地块图)

解决方案:在导入和执行cartopy.ccrs之前,只需在第一行本身中执行“ import cartopy”,然后运行代码即可。