pythonmatplotlib-basemap

Python Basemap pcolormesh valueError


I want to use Python and map an aggregated value of income in various cities. I have created a dictionary of cities and their respective income. When plotting the code, I am getting an error message:

ValueError: not enough values to unpack (expected 2, got 1)

and I do not know what is wrong there. Here is my code:

%matplotlib inline
import pandas as pd
import numpy as np
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from geopy.geocoders import Nominatim

j = {'Aach': 38.0, 'Aachen': 380.0, 'Aalen': 348.0, 'Aalen-Waldhausen': 10.0, 'Aarbergen': 17.0, 'Abenberg': 2.0, 'Abstatt': 6.0}
lat = list()
lon = list()
con = list()
count = 0
geolocator = Nominatim(user_agent = "geoapiExercises")
for i in j:
   #print(i) 
   location = geolocator.geocode(i)
   try: 
       print("Country Name: ", location)
       loc_dict=location.raw
       k=(loc_dict['display_name'].rsplit(',' , 1)[1])
       #print(loc_dict["lon"])
       lat.append(loc_dict["lat"])
       lon.append(loc_dict["lon"])
       count =(int)(basedict[i])
       con.append(count)
   except:
        continue

lat = np.asarray(lat)
lon = np.asarray(lon)
coun = np.asarray(con)
lon, lat = np.meshgrid(lon, lat)

fig = plt.figure(figsize=(10, 8))
m = Basemap(projection='lcc', resolution='c',
            width=8E6, height=8E6, 
            lat_0=45, lon_0=8,)

m.shadedrelief(scale=0.5)
m.pcolormesh(lon, lat, coun,
             latlon=True, cmap='RdBu_r')

Any idea what is wrong here?

Many thanks in advance!


Solution

  • pcolor mesh is expecting an array of lon and lat as its first argument. Some square brackets should solve the error e.g:

    m.pcolormesh([lon, lat], coun,
                 latlon=True, cmap='RdBu_r')