yvard - 1 year ago 64
Python Question

# Function that should result in a graph is not working?

I wrote a program which graphs data points on a map, and it worked perfectly fine. When I turned it into a function, however, it stopped working. Here it is:

``````import numpy as np
from scipy.io import netcdf_file
#import math
import matplotlib.pyplot as plt
#from scipy.stats.stats import nanmean
from mpl_toolkits.basemap import Basemap
#from mpl_toolkits.axes_grid1 import make_axes_locatable
def Argo_Data(file_location):
#imports file
fname = (file_location)
ncfile = netcdf_file(fname,'r');
#reads file and prepares it for usage
temp = np.array(ncfile.variables['TEMP'][:])
lon = np.array(ncfile.variables['LONGITUDE'][:])
lat = np.array(ncfile.variables['LATITUDE'][:])
return temp, lon, lat
#setup of the map
def Plot_Data(temp, lon, lat):
map = Basemap(projection='robin',lon_0=0,resolution='l')
map.drawcoastlines(linewidth=0.25)
map.drawcountries(linewidth=0.25)
map.fillcontinents(color='coral',lake_color='grey')
map.drawmapboundary(fill_color='grey')
map.drawmeridians(np.arange(0,360,60),labels=[True,False,False,True],fontsize=8)
map.drawparallels(np.arange(-90,90,30),labels=[True,False,False,True],fontsize=8)
#transforms longitude and latitude to graphable x and y
x, y = map(lon,lat)
data = temp[...,0]
#plots the points by color according to their individual surface temperature
cs = map.scatter(x,y,c=data,marker='.',lw=.05)
#executes the graph
plt.plot()
plt.show(block=False)

Argo_Data('/Users/epsuser/Argo/Data/July/20160702_prof.nc')
``````

Original Code (that worked fine):

``````# IMPORT FILES
import numpy as np
from scipy.io import netcdf_file
#import math
import matplotlib.pyplot as plt
#from scipy.stats.stats import nanmean
from mpl_toolkits.basemap import Basemap
#from mpl_toolkits.axes_grid1 import make_axes_locatable
#imports file
fname = ('/Users/epsuser/Dropbox/Argo/Data/2016/July/20160702_prof.nc')
ncfile = netcdf_file(fname,'r');
#reads file and prepares it for usage
temp = np.array(ncfile.variables['TEMP'][:])
lon = np.array(ncfile.variables['LONGITUDE'][:])
lat = np.array(ncfile.variables['LATITUDE'][:])
#setup of the map
map = Basemap(projection='robin',lon_0=0,resolution='l')
map.drawcoastlines(linewidth=0.25)
map.drawcountries(linewidth=0.25)
map.fillcontinents(color='coral',lake_color='grey')
map.drawmapboundary(fill_color='grey')
map.drawmeridians(np.arange(0,360,60),labels=[True,False,False,True],fontsize=8)
map.drawparallels(np.arange(-90,90,30),labels=[True,False,False,True],fontsize=8)
#transforms longitude and latitude to graphable x and y
x, y = map(lon,lat)
data = temp[...,0]
#plots the points by color according to their individual surface temperature
cs = map.scatter(x,y,c=data,marker='.',lw=.05)
#executes the graph
plt.plot()
plt.show(block=False)
``````

The last line is there to show that I tried plugging in the file... Anyways, like I mentioned before, the program worked properly before I indented it in order for it to become a function. So the problem is with how I formatted the function, the the content itself. Could anyone please help me see what the problem is?

Calling `Argo_Data` does not also call all of the nested functions contained within it. Instead, you need something inside `Argo_Data` to call `Read_Data` and `Plot_Data` and to pass the appropriate arguments. I cannot see a good reason for you defining these functions within `Argo_Data` though. I cannot test the actual code, but assuming that this was the only change that you made, then the following should work.

``````import numpy as np
from scipy.io import netcdf_file
#import math
import matplotlib.pyplot as plt
#from scipy.stats.stats import nanmean
from mpl_toolkits.basemap import Basemap
#from mpl_toolkits.axes_grid1 import make_axes_locatable
def Argo_Data(file_location):
#imports file
fname = (file_location)
ncfile = netcdf_file(fname,'r');
#reads file and prepares it for usage
temp = np.array(ncfile.variables['TEMP'][:])
lon = np.array(ncfile.variables['LONGITUDE'][:])
lat = np.array(ncfile.variables['LATITUDE'][:])
return temp, lon, lat
#setup of the map
def Plot_Data(temp, lon, lat):
map = Basemap(projection='robin',lon_0=0,resolution='l')
map.drawcoastlines(linewidth=0.25)
map.drawcountries(linewidth=0.25)
map.fillcontinents(color='coral',lake_color='grey')
map.drawmapboundary(fill_color='grey')
map.drawmeridians(np.arange(0,360,60),labels=[True,False,False,True],fontsize=8)
map.drawparallels(np.arange(-90,90,30),labels=[True,False,False,True],fontsize=8)
#transforms longitude and latitude to graphable x and y
x, y = map(lon,lat)
data = temp[...,0]
#plots the points by color according to their individual surface temperature
cs = map.scatter(x,y,c=data,marker='.',lw=.05)
#executes the graph
plt.plot()
plt.show(block=False)

# Something to call the nested functions