0

I am trying to plot a map of the US and mark the various cities across the country. I got the map to work. But I am having two issues: the first is, I am getting this error message:

AttributeError: 'NoneType' object has no attribute 'longitude'

Secondly, I have tried to enlarge the graph using the plt.figsize attribute however my map still stays the same size.

Lastly, this is not really an issue but what if i wanted to label the dots with the city names How can i do so?

Here is my code for the map:

 import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from geopy.geocoders import Nominatim
import math

city_list = list(flight_data["OriginCityName"].unique())
cities = city_list
scale = 1

map = Basemap(width=10000000,height=6000000,projection='lcc',
            resolution=None,lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)
plt.figure(figsize=(19,20))
map.bluemarble()


# Get the location of each city and plot it
geolocator = Nominatim()
for city in cities:
        loc = geolocator.geocode(city)
        if not loc:
            print("Could not locate {}".format(city))
            continue
        x, y = map(loc.longitude, loc.latitude)
        map.plot(x,y,marker='o',color='Red',markersize=5)
        plt.annotate(city, xy = (x,y), xytext=(-20,20))
plt.show()

enter image description here

1 Answer 1

2
  1. I guess there is something in your city_list Nominatim can't resolve. I added a check for that below.

  2. You have to call figure(num=1,figsize=(8,9)) before you plot anything (here: the map).

  3. You can use plt.annotate, see below.

Hope this helps.

    for city in cities:
        loc = geolocator.geocode(city)
        if not loc:
            print("Could not locate {}".format(city))
            continue
        x, y = map(loc.longitude, loc.latitude)
        map.plot(x,y,marker='o',color='Red',markersize=int(math.sqrt(count))*scale)
        plt.annotate(city, xy = (x,y), xytext=(-20,20)) 
Sign up to request clarification or add additional context in comments.

8 Comments

Hey the figsize works . but when i used the for loop you gave me the red dots on the map dont appear ,also the plt.annotate doesnt work it says label not defined
@DeepakM I think I was a bit unclear. I posted only the modification of the beggining of the for loop. I added the rest to my answer above. The label is the city name shown next to the dots. Also added aboved.
hey man really appreciate all the help sorry to bother you again but why does it through an error NameError: name 'count' is not defined @e-dschungel
You should try to read and understand the error messages carefully. `` NameError: name 'count' is not defined`` means that no value is was assigned to the variable count before the usage in the map.plot line. In the example you copy-pasted your code from count was used to size the dots according to the number of conference praticipants. As you what a constant dot size just use a fixed value like map.plot(x,y,marker='o',color='Red',markersize=1) or leave it out completely.
I tried it again, no error messages, this time but it still does not plot the cities, is there something i am not doing on why it is isnt working? @e-dschungel
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.