Python Beginners(8) -- GeoDataViz

Read csv files of airline data and check datatype

# conda install basemap --- to install basemap from matplotlib
import pandas as pd
airlines = pd.read_csv("airlines.csv")
airports = pd.read_csv("airports.csv")
routes = pd.read_csv("routes.csv")
print (airlines.iloc[0])
print (airports.iloc[0])
print (routes.iloc[0])
print (airports.dtypes)  # check datatype

Build a basemap constructor

  • llcrnrlat --- latitude of lower left hand corner of the desired map domain
  • urcrnrlat --- latitude of upper right hand corner of the desired map domain
  • llcrnrlon --- longitude of lower left hand corner of the desired map domain
  • urcrnrlon --- longitude of upper right hand corner of the desired map domain
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
m = Basemap(projection = 'merc', llcrnrlat = -80, urcrnrlat = 80,
            llcrnrlon = -180, urcrnrlon = 180)

Pass list format lat and lon to Basemap instance and return projected lat and lon

lat = airports['latitude'].tolist()
lon = airports['longitude'].tolist()
x, y = m(lon, lat)
m.scatter(x, y, s=1) # draw scatter
m.drawcoastlines() # draw coastlines
plt.show()

Add title and set figsize

fig, ax = plt.subplots(figsize = (20, 15))
ax.set_title("Scaled Up Earth With Coastlines")
m = Basemap(projection='merc', llcrnrlat=-80, urcrnrlat=80, llcrnrlon=-180, urcrnrlon=180)
longitudes = airports["longitude"].tolist()
latitudes = airports["latitude"].tolist()
x, y = m(longitudes, latitudes)
m.scatter(x, y, s=1)
m.drawcoastlines()
plt.show()

Draw great circles

def create_great_circle(df):
    fig, ax = plt.subplots(figsize=(15,20))
    m = Basemap(projection='merc', llcrnrlat=-80, urcrnrlat=80,         llcrnrlon=-180, urcrnrlon=180)
    m.drawcoastlines()
    for index, row in df.iterrows():
        lat1 = row['start_lat']
        lat2 = row['end_lat']
        lon1 = row['start_lon']
        lon2 = row['end_lon']      
        if abs(lat2-lat1) < 180 and abs(lon2 - lon1) < 180:
            m.drawgreatcircle(lon1, lat1, lon2, lat2)
    plt.show()  
dfw = geo_routes.loc[geo_routes['source'] == 'DFW']
create_great_circle(dfw)

你可能感兴趣的:(Python Beginners(8) -- GeoDataViz)