Städte in Basmap

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
Samoth
User
Beiträge: 41
Registriert: Freitag 24. November 2017, 15:13

Freitag 12. Januar 2018, 11:13

Hallo,
ich habe auf einer Seite folgendes gesehen:

Code: Alles auswählen

1 cities = ['London ', 'New York ', 'Madrid ', 'Cairo ', 'Moscow ', 'Delhi ', 'Dakar ']
lat = [51.507778 , 40.716667 , 40.4 , 30.058 , 55.751667 , 28.61 , 14.692778]
 lon = [ -0.128056 , -74, -3.683333 , 31.229 , 37.617778 , 77.23 , -17.446667]

 m = Basemap ( projection ='ortho ', lat_0 =45 , lon_0 =10)
 m. drawmapboundary ()
 m. drawcoastlines ()
 m. fillcontinents ()
 x, y = m(lon , lat )
 plt . plot (x, y, 'ro ')
 for city , xc , yc in zip(cities , x, y):
 plt. text (xc +250000 , yc -150000 , city , bbox = dict ( facecolor ='yellow ', alpha =0.5))
Das sieht ganz nett aus ;)... nun wollte ich es auf mein Problem anwenden. Allerdings bekomme ich nach dem umändern einen Fehler, denn ich jetzt nicht auf anhieb verstehen.

Code: Alles auswählen

#Map create
# projection, lat/lon extents and resolution of polygons to draw
# resolutions: c - crude, l - low, i - intermediate, h - high, f - full
print('Creat a Basemap instance')

llcrnrlon=-29.	#Lon. definition left border
llcrnrlat=1.	#Lat. bottom border
urcrnrlon=29.	#lon. right
urcrnrlat=39.	#Lat. top
map = Basemap(projection='merc',llcrnrlon=llcrnrlon,llcrnrlat=llcrnrlat,urcrnrlon=urcrnrlon,urcrnrlat=urcrnrlat,resolution='i')

print('Location: \nEast:',llcrnrlon,'\nWest:',urcrnrlon,'\nNorth: ',urcrnrlat,'\nSouth: ',llcrnrlat)

#add the states, counties, coasts, national borders and a land-sea colored mask
map.drawcoastlines()
map.drawstates()
map.drawcountries()
map.drawlsmask(land_color='Linen', ocean_color='#CCFFFF') # can use HTML names or codes for colors
map.drawcounties() # you can even add counties (and other shapefiles!)

#plt.show()

#add Lat/Lon
parallels = np.arange(0,40,5.) # make latitude lines ever 5 degrees from 30N-50N
meridians = np.arange(-30,30,5.) # make longitude lines every 5 degrees from 95W to 70W
map.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)
map.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10)

#Cities definition for Visualisation
cities = ['Niamey', 'Bamako', 'Dakar', 'Bobo-Dioulasso','Marrakesch', 'Tunis', 'Abuja']
lat = [13.511596 ,  12.617098 ,  14.716677, 11.164922 ,  31.629472 , 36.806495,  9.076479]
lon = [ 2.125385 ,  -7.981084,  -17.467686 ,  -4.305154 ,  -7.981084 , 10.181532,  7.398574]

#Plot the data on top of the map
lon,lat = np.meshgrid(lons,lats)
x,y = map(lon,lat)

# display blue marble image ( from NASA )
# as map background
map.bluemarble()
for city , xc , yc in zip(cities , x, y):
	plt.text(xc, yc , city , bbox=dict(
	facecolor='yellow', alpha =0.5))
Dies erzeugt den Fehler:
TypeError: only length-1 arrays can be converted to Python scalars

was müsste ich da ändern?

Gruß
__deets__
User
Beiträge: 3111
Registriert: Mittwoch 14. Oktober 2015, 14:29

Freitag 12. Januar 2018, 11:21

Bitte den gesamten Traceback zeigen, so weiß keiner, in welcher Zeile das Problem auftaucht.
Samoth
User
Beiträge: 41
Registriert: Freitag 24. November 2017, 15:13

Freitag 12. Januar 2018, 11:30

Anbei der komplette Traceback:

Code: Alles auswählen

In [27]: for city , xc , yc in zip(cities , x, y):
    ...: ^Iplt.text(xc, yc , city , bbox=dict(
    ...: ^Ifacecolor='yellow', alpha =0.5))
    ...:  

In [28]: plt.show()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/samothkociok/.conda/envs/thomypy/lib/python3.4/site-packages/matplotlib/backends/backend_qt5agg.py in __draw_idle_agg(self, *args)
    174     def __draw_idle_agg(self, *args):
    175         try:
--> 176             FigureCanvasAgg.draw(self)
    177             self.update()
    178         finally:

/home/samothkociok/.conda/envs/thomypy/lib/python3.4/site-packages/matplotlib/backends/backend_agg.py in draw(self)
    472 
    473         try:
--> 474             self.figure.draw(self.renderer)
    475         finally:
    476             RendererAgg.lock.release()

/home/samothkociok/.conda/envs/thomypy/lib/python3.4/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     59     def draw_wrapper(artist, renderer, *args, **kwargs):
     60         before(artist, renderer)
---> 61         draw(artist, renderer, *args, **kwargs)
     62         after(artist, renderer)
     63 

/home/samothkociok/.conda/envs/thomypy/lib/python3.4/site-packages/matplotlib/figure.py in draw(self, renderer)
   1157         dsu.sort(key=itemgetter(0))
   1158         for zorder, a, func, args in dsu:
-> 1159             func(*args)
   1160 
   1161         renderer.close_group('figure')

/home/samothkociok/.conda/envs/thomypy/lib/python3.4/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     59     def draw_wrapper(artist, renderer, *args, **kwargs):
     60         before(artist, renderer)
---> 61         draw(artist, renderer, *args, **kwargs)
     62         after(artist, renderer)
     63 

/home/samothkociok/.conda/envs/thomypy/lib/python3.4/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
   2322 
   2323         for zorder, a in dsu:
-> 2324             a.draw(renderer)
   2325 
   2326         renderer.close_group('axes')

/home/samothkociok/.conda/envs/thomypy/lib/python3.4/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     59     def draw_wrapper(artist, renderer, *args, **kwargs):
     60         before(artist, renderer)
---> 61         draw(artist, renderer, *args, **kwargs)
     62         after(artist, renderer)
     63 

/home/samothkociok/.conda/envs/thomypy/lib/python3.4/site-packages/matplotlib/text.py in draw(self, renderer)
    747 
    748         with _wrap_text(self) as textobj:
--> 749             bbox, info, descent = textobj._get_layout(renderer)
    750             trans = textobj.get_transform()
    751 

/home/samothkociok/.conda/envs/thomypy/lib/python3.4/site-packages/matplotlib/text.py in _get_layout(self, renderer)
    332         of a rotated text when necessary.
    333         """
--> 334         key = self.get_prop_tup()
    335         if key in self._cached:
    336             return self._cached[key]

/home/samothkociok/.conda/envs/thomypy/lib/python3.4/site-packages/matplotlib/text.py in get_prop_tup(self)
    901         need to know if the text has changed.
    902         """
--> 903         x, y = self.get_unitless_position()
    904         return (x, y, self.get_text(), self._color,
    905                 self._verticalalignment, self._horizontalalignment,

/home/samothkociok/.conda/envs/thomypy/lib/python3.4/site-packages/matplotlib/text.py in get_unitless_position(self)
    883         # This will get the position with all unit information stripped away.
    884         # This is here for convienience since it is done in several locations.
--> 885         x = float(self.convert_xunits(self._x))
    886         y = float(self.convert_yunits(self._y))
    887         return x, y

TypeError: only length-1 arrays can be converted to Python scalars
__deets__
User
Beiträge: 3111
Registriert: Mittwoch 14. Oktober 2015, 14:29

Freitag 12. Januar 2018, 11:41

Dann gib doch mal xc und yc vorher aus, damit man die mal betrachten kann. Da scheint die Ursache zu liegen.
Samoth
User
Beiträge: 41
Registriert: Freitag 24. November 2017, 15:13

Freitag 12. Januar 2018, 13:51

Hmm... komisch...
Die Städte habe ich so definiert...
#Cities definition for Visualisation
cities = ['Niamey', 'Bamako', 'Dakar', 'Bobo-Dioulasso','Marrakesch', 'Tunis', 'Abuja']
lati = [13.511596 , 12.617098 , 14.716677, 11.164922 , 31.629472 , 36.806495, 9.076479]
loni = [ 2.125385 , -7.981084, -17.467686 , -4.305154 , -7.981084 , 10.181532, 7.398574]

Rein von der Überlegung müssten nur noch die zwei Variabeln übereinander geplottet werden...

In [50]: xc
Out[50]:
array([ 27798.71857117, 83396.15571351, 138993.59285585,
194591.02999819, 250188.46714053, 305785.90428287,
361383.34142521, 416980.77856755, 472578.2157099 ,
528175.65285224, 583773.08999458, 639370.52713692,
694967.96427926, 750565.4014216 , 806162.83856394,
861760.27570628, 917357.71284862, 972955.14999096,
1028552.5871333 , 1084150.02427564, 1139747.46141798,
1195344.89856032, 1250942.33570266, 1306539.772845 ,
1362137.20998734, 1417734.64712969, 1473332.08427203,
1528929.52141437, 1584526.95855671, 1640124.39569905,
1695721.83284139, 1751319.26998373, 1806916.70712607,
1862514.14426841, 1918111.58141075, 1973709.01855309,
2029306.45569543, 2084903.89283777, 2140501.32998011,
2196098.76712245, 2251696.20426479, 2307293.64140713,
2362891.07854947, 2418488.51569182, 2474085.95283416,
2529683.3899765 , 2585280.82711884, 2640878.26426118,
2696475.70140352, 2752073.13854586, 2807670.5756882 ,
2863268.01283054, 2918865.44997288, 2974462.88711522,
3030060.32425756, 3085657.7613999 , 3141255.19854224,
3196852.63568458, 3252450.07282692, 3308047.50996927,
3363644.94711161, 3419242.38425395, 3474839.82139629,
3530437.25853863, 3586034.69568097, 3641632.13282331,
3697229.56996565, 3752827.00710799, 3808424.44425033,
3864021.88139267, 3919619.31853501, 3975216.75567735,
4030814.19281969, 4086411.62996203, 4142009.06710437,
4197606.50424671, 4253203.94138905, 4308801.3785314 ,
4364398.81567374, 4419996.25281608, 4475593.68995842,
4531191.12710076, 4586788.5642431 , 4642386.00138544,
4697983.43852778, 4753580.87567012, 4809178.31281246,
4864775.7499548 , 4920373.18709714, 4975970.62423948,
5031568.06138182, 5087165.49852416, 5142762.9356665 ,
5198360.37280884, 5253957.80995119, 5309555.24709353,
5365152.68423587, 5420750.12137821, 5476347.55852055,
5531944.99566289, 5587542.43280523, 5643139.86994757,
5698737.30708991, 5754334.74423225, 5809932.18137459,
5865529.61851693, 5921127.05565927, 5976724.49280161,
6032321.92994395, 6087919.3670863 , 6143516.80422864,
6199114.24137098, 6254711.67851332, 6310309.11565566,
6365906.552798 , 6421503.98994034])
Benutzeravatar
noisefloor
User
Beiträge: 2427
Registriert: Mittwoch 17. Oktober 2007, 21:40
Wohnort: Görgeshausen
Kontaktdaten:

Freitag 12. Januar 2018, 14:53

Hallo,
Die Städte habe ich so definiert...
#Cities definition for Visualisation
cities = ['Niamey', 'Bamako', 'Dakar', 'Bobo-Dioulasso','Marrakesch', 'Tunis', 'Abuja']
lati = [13.511596 , 12.617098 , 14.716677, 11.164922 , 31.629472 , 36.806495, 9.076479]
loni = [ 2.125385 , -7.981084, -17.467686 , -4.305154 , -7.981084 , 10.181532, 7.398574]
Aber nicht im Code, den du weiter oben gezeigt hast... da steht `lat` (nicht `lati`) und `lon` (nicht `loni`). Beides überschreibst du aber in der Zeile nach `#Plot the data on top of the map` aber wieder, wobei an der Stelle `lats` und `lons` nicht definiert ist.

Du musst schon den Code _tatsächliche_ Code posten, der genau _die_ von dir gezeigte Fehlermeldung generiert. Sonst kommen wir hier nicht weiter...

Gruß, noisefloor
Samoth
User
Beiträge: 41
Registriert: Freitag 24. November 2017, 15:13

Freitag 12. Januar 2018, 15:46

Hallo,
ja der ändert sich, da ich an ihm gerade rumwurschtel...

Code: Alles auswählen

#Load in the netCDF file
nc = NetCDFFile(ROOT+'LE_v2-1-000_conc-sfc_20170501.nc') 

# Extract data from NetCDF file (Global Parameter)
lats = nc.variables['latitude'][:]		#lat; Unit degrees_east
lons = nc.variables['longitude'][:]		#lon; Unit degrees_north
lev = nc.variables['level'][:]			#Layer; Hight
time = nc.variables['time'][:]			#Time; Sevonds since 2012-01-01
	
#Concentrations
accum_dust = nc.variables['tdust'][1,:,:,:]			#total dust Aerosol
accum_dust_units = nc.variables['tdust'].units		#kg m-3

#Map create
# projection, lat/lon extents and resolution of polygons to draw
# resolutions: c - crude, l - low, i - intermediate, h - high, f - full
print('Creat a Basemap instance')

llcrnrlon=-29.	#Lon. definition left border
llcrnrlat=1.	#Lat. bottom border
urcrnrlon=29.	#lon. right
urcrnrlat=39.	#Lat. top
map = Basemap(projection='merc',llcrnrlon=llcrnrlon,llcrnrlat=llcrnrlat,urcrnrlon=urcrnrlon,urcrnrlat=urcrnrlat,resolution='i')

print('Location: \nEast:',llcrnrlon,'\nWest:',urcrnrlon,'\nNorth: ',urcrnrlat,'\nSouth: ',llcrnrlat)

#add the states, counties, coasts, national borders and a land-sea colored mask
map.drawcoastlines()
map.drawstates()
map.drawcountries()
map.drawlsmask(land_color='Linen', ocean_color='#CCFFFF') # can use HTML names or codes for colors
map.drawcounties() # you can even add counties (and other shapefiles!)

#plt.show()

#add Lat/Lon
parallels = np.arange(0,40,5.) # make latitude lines ever 5 degrees from 30N-50N
meridians = np.arange(-30,30,5.) # make longitude lines every 5 degrees from 95W to 70W
map.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)
map.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10)

#Cities definition for Visualisation
cities = ['Niamey', 'Bamako', 'Dakar', 'Bobo-Dioulasso','Marrakesch', 'Tunis', 'Abuja']
lati = [13.511596 ,  12.617098 ,  14.716677, 11.164922 ,  31.629472 , 36.806495,  9.076479]
loni = [ 2.125385 ,  -7.981084,  -17.467686 ,  -4.305154 ,  -7.981084 , 10.181532,  7.398574]

#Plot the data on top of the map
lon,lat = np.meshgrid(lons,lats)
x,y = map(lon,lat)
xc,yc=map(lati,loni)

# display blue marble image ( from NASA )
# as map background
#map.bluemarble()
for city , xc , yc in zip(cities , x, y):
	plt.text(xc, yc , city , bbox=dict(facecolor='yellow', alpha =0.5))

accum_dust_corr= abs(accum_dust)

#Plot Temperature cmap=plt.cm.RdBu_r für Temp
tdustplot= map.pcolor(x,y,np.squeeze(accum_dust_corr), 
norm=colors.LogNorm(0,10),cmap=plt.cm.RdBu_r)

plt.title(nc.variables['tdust'].long_name +' for 01.05.2017 0100 UTC' )
cbar = map.colorbar(tdustplot, location='right', pad="10%")
cbar.set_label(accum_dust_units)

plt.show()
Das ist gerade der momentane stand.
Samoth
User
Beiträge: 41
Registriert: Freitag 24. November 2017, 15:13

Montag 15. Januar 2018, 08:18

Kann es sein das es ein Problem bei der Koordinatenzuordnung gibt?

Gruß
Antworten