pythonpython-3.xpython-2.7sst

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


I'm really new to python, and I need to plot a data netcdf of sea surface temperature(sst),in python, but it keep giving erro.

I use the same code in another notebook, and it run perfect fine.

###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')                                                                                                                        

It say that erro is in the line "ax.add_feature(cartopy.feature.BORDERS, linestyle='-', linewidth=0.25)", that "NameError: name 'cartopy' is not defined".
What you think it is the problem?
P.S.: I know that I'm using a lot of lib that don't need


Solution

  • Name error in python means that the specific attribute/method is not imported in the program. In the code you are using cartopy.crs, cartopy.config and cartopy.features.Border, but only the first two are imported through your statements

    import cartopy.crs as crash
    

    and

    from cartopy import config
    

    So for features.Border, you either do

    import cartopy 
    

    Or

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