How to fix 'NameError: name 'cartopy' is not defined', in plot about sea surface temperature in python?

How to fix 'NameError: name 'cartopy' is not defined', in plot about sea surface temperature in python?

我是 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')                                                                                                                        

它说错误在"ax.add_feature(cartopy.feature.BORDERS, linestyle='-', linewidth=0.25)"行,"NameError: name 'cartopy' is not defined"。
您认为是什么问题?
P.S.: 我知道我使用了很多不需要的库

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

Name error in python 表示程序中没有导入特定的 attribute/method。在您使用 cartopy.crs、cartopy.config 和 cartopy.features.Border 的代码中,但只有前两个是通过您的语句导入的

import cartopy.crs as crash

from cartopy import config

所以对于 features.Border,你要么

import cartopy 

或者

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

对我来说,我在 jupyter 中安装了 cartopy 包,但它仍然显示 cartopy not defined 错误。(我正在用 cartopy.ccrs 绘制立体图)

解决方案:在导入和执行 cartopy.ccrs 之前,只需在第一行本身执行 'import cartopy' 然后 运行 codes.It 对我有效