2017-12-02 7 views
0

Ich kann nicht herausfinden, warum mein Graph nicht korrekt ausgegeben wird. Ich versuche, die Tiefe einer Kartenprojektion mit netCDF4 und mpl_toolkits.Basemap darzustellen. Ich habe mehrere Projektionen versucht, aber keine hat funktioniert. Unten ist der Code, den ich verwende:matplotlib.basemap - Geowissenschaft - ValueError: Operanden konnten nicht zusammen mit Shapes gesendet werden (12529,15661) (12529,15662)

from netCDF4 import Dataset 
from mpl_toolkits.basemap import Basemap 
import mpl_toolkits as mplt 
import matplotlib.pyplot as plt 
import numpy as np 

f = Dataset("/geosci/path2data/depth_data.nc") 

# extract and label column 
lats = f.variables['lat'][:] # extract/copy the latitude data 
lons = f.variables['lon'][:] # extract/copy the longitude data 
depth = f.variables['Band1'][:] 
lon_0 = lons.mean() 
lat_0 = lats.mean() 


#create map projection 
m = Basemap(projection = 'stere', width = 5*10**6, height = 35*10**5, resolution = 'l', lat_ts = 40, lat_0 = lat_0, lon_0 = lon_0) 

# Plot of global temperature on our random day 
fig = plt.figure() 
fig.subplots_adjust(left=0., right=1., bottom=0., top=0.9) 

# Make the plot continuous 
depth_cyclic, lons_cyclic = mplt.basemap.addcyclic(depth, lons) 

# compute map projection coordinates for lat/lon grid 
# Create 2D lat/lon arrays for Basemap 
lon2d, lat2d = np.meshgrid(lons_cyclic, lats) 
# Transforms lat/lon into plotting coordinates for projection 
x, y = m(lon2d, lat2d) 

# Plot of air temperature with 11 contour intervals 
cs = m.contourf(lon2d, lat2d, depth, 11, cmap=plt.cm.Spectral_r) 
cbar = plt.colorbar(cs, orientation='horizontal', shrink=0.5) 
m.plot(x,y) 
plt.show() 

Dies ist der Fehler, den ich bekomme. Ich lehre mich immer noch selbst zu programmieren und brauche ein wenig Hilfe bei diesem Thema.

ValueError        Traceback (most recent call last) 
<ipython-input-7-7b81b2da3866> in <module>() 
    18 
    19 # Plot of air temperature with 11 contour intervals 
---> 20 cs = m.contourf(lon2d, lat2d, depth, 11, cmap=plt.cm.Spectral_r) 
    21 cbar = plt.colorbar(cs, orientation='horizontal', shrink=0.5) 
    22 m.plot(x,y) 

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.pyc 
in with_transform(self, x, y, data, *args, **kwargs) 
    519    # convert lat/lon coords to map projection coords. 
    520    x, y = self(x,y) 
--> 521   return plotfunc(self,x,y,data,*args,**kwargs) 
    522  return with_transform 
    523 

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.pyc 
in contourf(self, x, y, data, *args, **kwargs) 
    3669     data = ma.asarray(data) 
    3670     # combine with data mask. 
-> 3671     mask = np.logical_or(ma.getmaskarray(data),xymask) 
    3672     data = ma.masked_array(data,mask=mask) 
    3673     CS = ax.contourf(x,y,data,*args,**kwargs) 

ValueError: operands could not be broadcast together with shapes (12529,15661) (12529,15662) 
+0

Was diese Fehler Ihnen sagen, ist, dass die Daten 'depth' eine Spalte mehr als x und/oder y ('lon2d, lat2d'). Da dies kein [mcve] ist, denke ich, das ist alles, was wir dir helfen können. – ImportanceOfBeingErnest

+0

Verzeihen Sie mir die Frage: Wie kann ich das zu einer besseren Frage machen? Ich persönlich glaube, dass ich das Thema ziemlich klar dargelegt habe. – Earthly

+1

Aus den Handbüchern * "Fragen, die Debugging-Hilfe suchen (" Warum funktioniert dieser Code nicht? ") Müssen das gewünschte Verhalten, ein spezifisches Problem oder einen Fehler und den kürzesten Code enthalten, um ** in der Frage selbst zu reproduzieren **. "* Eine gute Frage würde also keine Eingabedatei verwenden, sondern das Problem mit einigen Daten innerhalb der Frage reproduzieren. – ImportanceOfBeingErnest

Antwort

0

Ich habe die kontinuierliche Tiefe vergessen, die den anderen Informationen zugewiesen wurde.

cs = m.contourf (x, y, depth_cyclic, 11, cmap = plt.cm.Spectral_r)

Verwandte Themen