def geoplot(canvas): obesity = pd.read_csv(gplt.datasets.get_path("obesity_by_state"), sep="\t") contiguous_usa = gpd.read_file(gplt.datasets.get_path("contiguous_usa")) result = contiguous_usa.set_index("state").join(obesity.set_index("State")) # https://stackoverflow.com/a/51621986/6622587 ax = canvas.figure.subplots(subplot_kw={"projection": gcrs.AlbersEqualArea()}) gplt.cartogram(result, scale="Percent", projection=gcrs.AlbersEqualArea(), ax=ax)
def plot_state_to_ax(state, ax): n = state_ticket_totals.loc[state]['Count'] gplt.choropleth(tickets_by_precinct(state), projection=gcrs.AlbersEqualArea(), cmap='Blues', linewidth=0.0, ax=ax) gplt.polyplot(boroughs, projection=gcrs.AlbersEqualArea(), edgecolor='black', linewidth=0.5, ax=ax) ax.set_title("{0} (n={1})".format(state, n))
def plot_state_to_ax(state, ax): """Reusable plotting wrapper.""" gplt.choropleth(tickets.set_index('id').loc[:, [state, 'geometry']], hue=state, projection=gcrs.AlbersEqualArea(), cmap='Blues', linewidth=0.0, ax=ax) gplt.polyplot(boroughs, projection=gcrs.AlbersEqualArea(), edgecolor='black', linewidth=0.5, ax=ax)
def test_webmap_input_restrictions(self): """Test webmap-specific plot restrictions.""" with pytest.raises(ValueError): webmap(self.p_df, projection=gcrs.AlbersEqualArea()) _, ax = plt.subplots(figsize=(2, 2)) with pytest.raises(ValueError): webmap(self.p_df, ax=ax) ax = plt.axes(projection=ccrs.PlateCarree()) with pytest.raises(ValueError): webmap(self.p_df, ax=ax) with pytest.warns(UserWarning): webmap(self.p_df)
def plot_point_map(gpd_gdf, percentile=0, save_file=None): """plot point data on a map""" # Choose points in which NSE value are bigger than the 25% quartile value range percentile_data = np.percentile(gpd_gdf['NSE'].values, percentile).astype(float) # the result of query is a tuple with one element, but it's right for plotting data_chosen = gpd_gdf.query("NSE > " + str(percentile_data)) contiguous_usa = gpd.read_file(gplt.datasets.get_path('contiguous_usa')) proj = gcrs.AlbersEqualArea(central_longitude=-98, central_latitude=39.5) polyplot_kwargs = {'facecolor': (0.9, 0.9, 0.9), 'linewidth': 0} pointplot_kwargs = {'hue': 'NSE', 'legend': True, 'linewidth': 0.01} # ax = gplt.polyplot(contiguous_usa.geometry, projection=proj, **polyplot_kwargs) ax = gplt.webmap(contiguous_usa, projection=gcrs.WebMercator()) gplt.pointplot(data_chosen, ax=ax, **pointplot_kwargs) ax.set_title("NSE " + "Map") plt.show() if save_file is not None: plt.savefig(save_file)
projection=gcrs.AlbersEqualArea(), cmap='Blues', linewidth=0.0, ax=ax) gplt.polyplot(boroughs, projection=gcrs.AlbersEqualArea(), edgecolor='black', linewidth=0.5, ax=ax) ax.set_title("{0} (n={1})".format(state, n)) # Finally, plot the data. f, axarr = plt.subplots(2, 2, figsize=(12, 12), subplot_kw={ 'projection': gcrs.AlbersEqualArea(central_latitude=40.7128, central_longitude=-74.0059) }) plt.suptitle('Parking Tickets Issued to State by Precinct, 2016', fontsize=16) plt.subplots_adjust(top=0.95) plot_state_to_ax('New York', axarr[0][0]) plot_state_to_ax('New Jersey', axarr[0][1]) plot_state_to_ax('Pennsylvania', axarr[1][0]) plot_state_to_ax('Connecticut', axarr[1][1]) plt.savefig("nyc-parking-tickets.png", bbox_inches='tight')
# Load the data (uses the `quilt` package). from quilt.data.ResidentMario import geoplot_data import geopandas as gpd boston_zip_codes = gpd.read_file(geoplot_data.boston_zip_codes()) boston_zip_codes = boston_zip_codes.assign(id=boston_zip_codes.id.astype(float)).set_index('id') listings = gpd.read_file(geoplot_data.boston_airbnb_listings()) listings = listings.assign(zipcode=listings.zipcode.astype(float)) # Plot the data. import geoplot as gplt import geoplot.crs as gcrs import numpy as np import matplotlib.pyplot as plt ax = gplt.polyplot(boston_zip_codes.geometry, projection=gcrs.AlbersEqualArea(), facecolor='lightgray', edgecolor='gray', linewidth=0) gplt.aggplot(listings, projection=gcrs.AlbersEqualArea(), hue='price', by='zipcode', geometry=boston_zip_codes.geometry, agg=np.median, ax=ax, linewidth=0) ax.set_title("Median AirBnB Price by Boston Zip Code, 2016") plt.savefig("boston-airbnb-aggplot.png", bbox_inches='tight', pad_inches=0.1)
import geoplot as gplt import geoplot.crs as gcrs import geopandas as gpd ## example code: https://melaniesoek0120.medium.com/data-visualization-how-to-plot-a-map-with-geopandas-in-python-73b10dcd4b4b # use the 'naturalearth_lowers' geopandas datasets world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowers')) # rename the columns world.columns=['continent', 'name', 'CODE', 'geometry'] # merge with your species occurrence data by the same "country code?" or any parameter that is available in both the world and GBIF dataframe df = pd.read_csv(data) merge = pd.merge(world, df, on = 'X') merge.plot(column = '', scheme = '', figsize =(,), legend = True, cmap = '') plt.show() ### kdeplot # link: https://geopandas.org/gallery/plotting_with_geoplot.html # under the kdeplot ax = gplt.polyplot(world, projection=gcrs.AlbersEqualArea()) ax = gplt.kdeplot(df, cmap='Reds', shade=True, shade_lowest=True, clip=world)
HUC8_Ecoregion_NH4 = pd.DataFrame(HUC8_diff.HUC_8, columns=['HUC_8']) HUC8_Ecoregion_NH4['NH4'] = 0 HUC8_Ecoregion = pd.merge(HUC8_Ecoregion_PTL, HUC8_Ecoregion_NH4, how='inner', on='HUC_8') HUC8_Ecoregion = HUC8_Ecoregion.set_index('HUC_8', drop=True) NARS_NLA_FINAL = pd.concat([NARS_NLA_group_aux, HUC8_Ecoregion], ignore_index=False, sort=False) HUC8_NARS_NLA_FINAL = HUC8.merge(NARS_NLA_FINAL, on='HUC_8') proj = gcrs.AlbersEqualArea() norm = colors.LogNorm() #cmap = matplotlib.cm.viridis #cmap=matplotlib.cm.get_cmap() cmap.set_under('grey') gplt.choropleth( HUC8_NARS_NLA_FINAL, hue='PTL', projection=proj, norm=norm, cmap='viridis', k=5, scheme='quantiles', legend=True, legend_kwargs={'loc': 'lower right'}, figsize=(12, 12),
import geopandas as gpd import geoplot as gplt import geoplot.crs as gcrs import matplotlib.pyplot as plt import mapclassify as mc continental_usa_cities = gpd.read_file(gplt.datasets.get_path('usa_cities')) continental_usa_cities = continental_usa_cities.query('STATE not in ["AK", "HI", "PR"]') contiguous_usa = gpd.read_file(gplt.datasets.get_path('contiguous_usa')) scheme = mc.Quantiles(continental_usa_cities['POP_2010'], k=5) ax = gplt.polyplot( contiguous_usa, zorder=-1, linewidth=1, projection=gcrs.AlbersEqualArea(), edgecolor='white', facecolor='lightgray', figsize=(8, 12) ) gplt.pointplot( continental_usa_cities, scale='POP_2010', limits=(2, 30), hue='POP_2010', cmap='Blues', scheme=scheme, legend=True, legend_var='scale', legend_values=[8000000, 2000000, 1000000, 100000], legend_labels=['8 million', '2 million', '1 million', '100 thousand'],
import geopandas as gpd from quilt.data.ResidentMario import geoplot_data boroughs = gpd.read_file(geoplot_data.nyc_boroughs()) fatal_collisions = gpd.read_file(geoplot_data.nyc_fatal_collisions()) injurious_collisions = gpd.read_file(geoplot_data.nyc_injurious_collisions()) # Plot the data. import geoplot as gplt import geoplot.crs as gcrs import matplotlib.pyplot as plt fig = plt.figure(figsize=(10, 5)) ax1 = plt.subplot(121, projection=gcrs.AlbersEqualArea(central_latitude=40.7128, central_longitude=-74.0059)) gplt.polyplot(boroughs, ax=ax1, projection=gcrs.AlbersEqualArea()) gplt.pointplot(fatal_collisions, projection=gcrs.AlbersEqualArea(), hue='BOROUGH', categorical=True, edgecolor='white', linewidth=0.5, zorder=10, scale='NUMBER OF PERSONS KILLED', limits=(2, 8), legend=True, legend_var='scale', legend_kwargs={'loc': 'upper left'}, legend_values=[2, 1], legend_labels=['2 Fatalities', '1 Fatality'],
def GIS_maps_module(latitude, longitude): import os import conda import pandas as pd import numpy as np # from shapely.geometry import Polygon as Poly pd.options.display.max_columns = 250 conda_file_dir = conda.__file__ conda_dir = conda_file_dir.split('lib')[0] proj_lib = os.path.join(os.path.join(conda_dir, 'share'), 'proj') os.environ["PROJ_LIB"] = proj_lib import matplotlib.pyplot as plt import matplotlib.cm import matplotlib.colors as colors import geopandas from geopandas.tools import sjoin import geoplot as gplt import geoplot.crs as gcrs input_point = np.array([latitude, longitude]) #input_point = np.array([39.014908, -98.010465]) input_point_df = pd.DataFrame({ 'Name': ['CAFO1'], 'Latitude': [input_point[0]], 'Longitude': [input_point[1]] }) poly = geopandas.GeoDataFrame.from_file('watershed/huc8sum.shp') point = geopandas.GeoDataFrame(input_point_df, geometry=geopandas.points_from_xy( input_point_df.Longitude, input_point_df.Latitude)) poly.crs = {'init': 'epsg:4326'} point.crs = {'init': 'epsg:4326'} # pointInPolys = sjoin(point, poly, how='left') # # HUC8ContPoint = float(pointInPolys['HUC_8']) #proj = gcrs.AlbersEqualArea() #ax = gplt.polyplot(poly, projection=proj, zorder= -100) #gplt.pointplot(point, ax=ax, projection=proj, color= 'red') #plt.savefig('zacarias.pdf') ## #NARS #NARS_points = pd.read_csv('NRSA 08-09.csv', sep=",", header=0) #Replaced by precomputed dataframe NARS_site_info = pd.read_csv('NARS_site_info/siteinfo_0.csv', sep=",", header=0) NARS_site_info_UIDindex = NARS_site_info.set_index('UID', drop=True) gdf_NARS_site_info_UIDindex = geopandas.GeoDataFrame( NARS_site_info_UIDindex, geometry=geopandas.points_from_xy(NARS_site_info_UIDindex.LON_DD83, NARS_site_info_UIDindex.LAT_DD83)) gdf_NARS_site_info_UIDindex.crs = {'init': 'epsg:4326'} #NLA #NLA_points = pd.read_csv('nla2012_waterchem_wide.csv', sep=",", header=0) #Replaced by precomputed dataframe # NLA_site_info = pd.read_csv('NLA_site_info/nla2012_wide_siteinfo_08232016.csv', sep=",", header=0) #Replaced by precomputed dataframe #HUC8 # watershed_metadata = pd.read_csv('watershed_metadata/huc8sum_20140326.csv', sep=",", header=0) #Replaced by precomputed dataframe HUC8 = geopandas.GeoDataFrame.from_file('watershed/huc8sum.shp') HUC8.crs = {'init': 'epsg:4326'} HUC8['HUC_8'] = HUC8['HUC_8'].astype(int) #Ecoregions Ecoregions = geopandas.GeoDataFrame.from_file( 'NARS_NP_values/narswsa_20110504.shp') Ecoregions.crs = {'init': 'epsg:4326'} #NARS points #proj = gcrs.AlbersEqualArea() #ax = gplt.polyplot(HUC8, projection=proj) #gplt.pointplot(gdf_NARS_site_info_UIDindex, ax=ax, projection=proj, s=1, color='red') #plt.savefig('NARS_locations.pdf') #plt.savefig("NARS_locations.png", bbox_inches='tight', pad_inches=0.1) # ============================================================================= # NARS (rivers and streams) # ============================================================================= # UID_HUC8_dict #UID_HUC8_dict = {key: NARS_site_info['HUC8'][NARS_site_info.UID == key].values for key in NARS_site_info['UID'].values} #UID_XLAT_DD_dict = {key: NARS_site_info['XLAT_DD'][NARS_site_info.UID == key].values for key in NARS_site_info['UID'].values} #UID_XLON_DD_dict = {key: NARS_site_info['XLON_DD'][NARS_site_info.UID == key].values for key in NARS_site_info['UID'].values} #UID_AGGR_ECO9_2015_dict = {key: (NARS_site_info['AGGR_ECO9_2015'][NARS_site_info.UID == key].values) for key in NARS_site_info['UID'].values} # #aux_list_HUC8 = [] #aux_list_XLAT_DD = [] #aux_list_XLON_DD = [] #aux_list_AGGR_ECO9_2015 = [] #for i in NARS_points['UID']: # aux_list_HUC8.append(int(UID_HUC8_dict[i])) # aux_list_XLAT_DD.append(float(UID_XLAT_DD_dict[i])) # aux_list_XLON_DD.append(-1*float(UID_XLON_DD_dict[i])) # aux_list_AGGR_ECO9_2015.append(str(UID_AGGR_ECO9_2015_dict[i])) # #NARS_points['HUC8'] = aux_list_HUC8 #NARS_points['XLAT_DD'] = aux_list_XLAT_DD #NARS_points['XLON_DD'] = aux_list_XLON_DD #NARS_points['AGGR_ECO9_2015'] = aux_list_AGGR_ECO9_2015 #NARS_points[['UID','HUC8','XLAT_DD','XLON_DD']] # #NARS_points = NARS_points.rename(columns={'HUC8':'HUC_8'}) #Precomputed dataframe #NARS_points = pd.read_csv('NARS_points.csv', sep=",", header=0) #---------------------------------------------------------------------------------- #NARS_points_filt_P = NARS_points.dropna(subset=['PTL']) #remove NAN values of PTL #NARS_points_filt_N = NARS_points.dropna(subset=['NH4']) #remove NAN values of NH4 #NARS_group_aux_filt_P = NARS_points_filt_P.groupby(['HUC_8']).mean() #NARS_group_aux_filt_N = NARS_points_filt_N.groupby(['HUC_8']).mean() # #HUC8.to_csv('HUC8.csv', index=False) # #HUC8_NARS_P = HUC8.merge(NARS_group_aux_filt_P, on='HUC_8') #HUC8_NARS_P.to_csv('HUC8_NARS_P.csv', index=False) #HUC8_NARS_N = HUC8.merge(NARS_group_aux_filt_N, on='HUC_8') #HUC8_NARS_N.to_csv('HUC8_NARS_N.csv', index=False) # #proj = gcrs.AlbersEqualArea() #norm = colors.LogNorm() #gplt.choropleth(HUC8_NARS_P, hue='PTL', projection=proj, norm=norm, cmap='viridis', k=5, scheme='quantiles', legend=True, legend_kwargs={'loc': 'lower right'},figsize=(12, 12)) #plt.savefig('NARS_TP.pdf') #plt.savefig("NARS_TP.png", bbox_inches='tight', pad_inches=0.1) # # #proj = gcrs.AlbersEqualArea() #norm = colors.LogNorm() #gplt.choropleth(HUC8_NARS_N, hue='NH4', projection=proj, norm=norm, cmap='viridis', k=5, scheme='quantiles', legend=True, legend_kwargs={'loc': 'lower right'},figsize=(12, 12)) #plt.savefig('NARS_NH4.pdf') #plt.savefig("NARS_NH4.png", bbox_inches='tight', pad_inches=0.1) # ============================================================================= # NLA (lakes) UID:HUC8 correspondence # ============================================================================= # UID_HUC8_dict #UID_HUC8_dict_NLA = {key: NLA_site_info['HUC8'][NLA_site_info.UID == key].values for key in NLA_site_info['UID'].values} #UID_LAT_DD83_dict_NLA = {key: NLA_site_info['LAT_DD83'][NLA_site_info.UID == key].values for key in NLA_site_info['UID'].values} #UID_LON_DD83_dict_NLA = {key: NLA_site_info['LON_DD83'][NLA_site_info.UID == key].values for key in NLA_site_info['UID'].values} #UID_AGGR_ECO9_2015_dict_NLA = {key: NLA_site_info['AGGR_ECO9_2015'][NLA_site_info.UID == key].values for key in NLA_site_info['UID'].values} # #aux_list_HUC8_NLA = [] #aux_list_LAT_DD83_NLA = [] #aux_list_LON_DD83_NLA = [] #aux_list_AGGR_ECO9_2015_NLA = [] #for i in NLA_points['UID']: # aux_list_HUC8_NLA.append(int(UID_HUC8_dict_NLA[i])) # aux_list_LAT_DD83_NLA.append(float(UID_LAT_DD83_dict_NLA[i])) # aux_list_LON_DD83_NLA.append(float(UID_LON_DD83_dict_NLA[i])) # aux_list_AGGR_ECO9_2015_NLA.append(str(UID_AGGR_ECO9_2015_dict_NLA[i])) # #NLA_points['HUC8'] = aux_list_HUC8_NLA #NLA_points['LAT_DD83'] = aux_list_LAT_DD83_NLA #NLA_points['LON_DD83'] = aux_list_LON_DD83_NLA #NLA_points['AGGR_ECO9_2015'] = aux_list_AGGR_ECO9_2015_NLA #NLA_points[['UID','HUC8','LAT_DD83','LON_DD83', 'AGGR_ECO9_2015']] #NLA_points = NLA_points.rename(columns={'HUC8':'HUC_8', 'PTL_RESULT':'PTL', 'AMMONIA_N_RESULT':'NH4'}) #Precomputed dataframe #NLA_points = pd.read_csv('NLA_points.csv', sep=",", header=0) #---------------------------------------------------------------------------------- #NLA_points_filt_P = NLA_points.dropna(subset=['PTL']) #remove NAN values of PTL #NLA_points_filt_N = NLA_points.dropna(subset=['NH4']) #remove NAN values of NH4 #NLA_group_aux_filt_P = NLA_points_filt_P.groupby(['HUC_8']).mean() #NLA_group_aux_filt_N = NLA_points_filt_N.groupby(['HUC_8']).mean() # #HUC8_NLA_P = HUC8.merge(NLA_group_aux_filt_P, on='HUC_8') #HUC8_NLA_N = HUC8.merge(NLA_group_aux_filt_N, on='HUC_8') # #proj = gcrs.AlbersEqualArea() #norm = colors.LogNorm() #gplt.choropleth(HUC8_NLA_P, hue='PTL', projection=proj, norm=norm, cmap='viridis', k=5, scheme='quantiles', legend=True, legend_kwargs={'loc': 'lower right'},figsize=(12, 12))#, linewidth=0.5, edgecolor='black',) #plt.savefig('NLA_TP.pdf') #plt.savefig("NLA_TP.png", bbox_inches='tight', pad_inches=0.1) # # #proj = gcrs.AlbersEqualArea() #norm = colors.LogNorm() #gplt.choropleth(HUC8_NLA_N, hue='NH4', projection=proj, norm=norm, cmap='viridis', k=5, scheme='quantiles', legend=True, legend_kwargs={'loc': 'lower right'},figsize=(12, 12))#, linewidth=0.5, edgecolor='black',) #plt.savefig('NLA_NH4.pdf') #plt.savefig("NLA_NH4.png", bbox_inches='tight', pad_inches=0.1) # ============================================================================= # NARS+NLA # ============================================================================= #NARS_NLA_points = pd.concat([NARS_points, NLA_points], ignore_index=True, sort=False) #NARS_NLA_points_filt_P = NARS_NLA_points.dropna(subset=['PTL']) #remove NAN values of PTL #NARS_NLA_points_filt_N = NARS_NLA_points.dropna(subset=['NH4']) #remove NAN values of PTL #NARS_NLA_group_aux_filt_P = NARS_NLA_points_filt_P.groupby(['HUC_8']).mean() #NARS_NLA_group_aux_filt_N = NARS_NLA_points_filt_N.groupby(['HUC_8']).mean() # #HUC8_NARS_NLA_P = HUC8.merge(NARS_NLA_group_aux_filt_P, on='HUC_8') #HUC8_NARS_NLA_N = HUC8.merge(NARS_NLA_group_aux_filt_N, on='HUC_8') #---------------------------------------------------------------------------------- #proj = gcrs.AlbersEqualArea() #norm = colors.LogNorm() #gplt.choropleth(HUC8_NARS_NLA_P, hue='PTL', projection=proj, norm=norm, cmap='viridis', k=5, scheme='quantiles', legend=True, legend_kwargs={'loc': 'lower right'},figsize=(12, 12))#, linewidth=0.5, edgecolor='black',) #plt.savefig('NARS_NLA_P.pdf') #plt.savefig("NARS_NLA_P.png", bbox_inches='tight', pad_inches=0.1) # #proj = gcrs.AlbersEqualArea() #norm = colors.LogNorm() #gplt.choropleth(HUC8_NARS_NLA_N, hue='NH4', projection=proj, norm=norm, cmap='viridis', k=5, scheme='quantiles', legend=True, legend_kwargs={'loc': 'lower right'},figsize=(12, 12))#, linewidth=0.5, edgecolor='black',) #plt.savefig('NARS_NLA_N.pdf') #plt.savefig("NARS_NLA_N.png", bbox_inches='tight', pad_inches=0.1) # ============================================================================= # Fill the gaps with the ecoregions average data # ============================================================================= #HUC8_in_NARS_NLA_list = list(HUC8_NARS_NLA_P['HUC_8']) #HUC8_total_list = list(HUC8['HUC_8']) #HUC8_diff_list = set(HUC8_total_list)-set(HUC8_in_NARS_NLA_list) # #HUC8_diff = HUC8[HUC8['HUC_8'].isin(HUC8_diff_list)] # #HUC8inEcoregions = sjoin(HUC8_diff, Ecoregions, how='inner', op='intersects') #HUC8inEcoregions = HUC8inEcoregions.drop_duplicates(subset=['HUC_8']) #HUC8inEcoregions_index = HUC8inEcoregions.set_index('HUC_8') # #HUC8_Ecoregion_dict = dict(zip(HUC8inEcoregions.HUC_8, HUC8inEcoregions.WSA_REGION)) # #NARS_NLA_AGGR_ECO9_2015 = NARS_NLA_points.groupby(['AGGR_ECO9_2015']).mean() #NARS_NLA_AGGR_ECO9_2015 = NARS_NLA_AGGR_ECO9_2015.rename(index={"['CPL']":'CPL', # "['NAP']":'NAP', # "['NPL']":'NPL', # "['SAP']":'SAP', # "['SPL']":'SPL', # "['TPL']":'TPL', # "['UMW']":'UMW', # "['WMT']":'WMT', # "['XER']":'XER', # }) # #HUC8_Ecoregion_PTL_dict = dict() #for l in HUC8_diff.HUC_8: # HUC8_Ecoregion_PTL_dict[l] = NARS_NLA_AGGR_ECO9_2015.loc[HUC8_Ecoregion_dict[l],'PTL'] # #HUC8_Ecoregion_NH4_dict = dict() #for l in HUC8_diff.HUC_8: # HUC8_Ecoregion_NH4_dict[l] = NARS_NLA_AGGR_ECO9_2015.loc[HUC8_Ecoregion_dict[l],'NH4'] #---------------------------------------------------------------------------------- #Average ecoregion value #HUC8_Ecoregion_PTL = pd.DataFrame(HUC8_Ecoregion_PTL_dict.items(), columns=['HUC_8', 'PTL']) #HUC8_Ecoregion_NH4 = pd.DataFrame(HUC8_Ecoregion_NH4_dict.items(), columns=['HUC_8', 'NH4']) #HUC8_Ecoregion = pd.merge(HUC8_Ecoregion_PTL, HUC8_Ecoregion_NH4, how='inner', on='HUC_8') #HUC8_Ecoregion = HUC8_Ecoregion.set_index('HUC_8', drop=True) # #NARS_NLA_FINAL = pd.concat([NARS_NLA_group_aux, HUC8_Ecoregion], ignore_index=False, sort=False) # #HUC8_NARS_NLA_FINAL = HUC8.merge(NARS_NLA_FINAL, on='HUC_8') #proj = gcrs.AlbersEqualArea() #norm = colors.LogNorm() #gplt.choropleth(HUC8_NARS_NLA_FINAL, hue='PTL', projection=proj, norm=norm, cmap='viridis', k=5, scheme='quantiles', legend=True, legend_kwargs={'loc': 'lower right'},figsize=(12, 12))#, linewidth=0.5, edgecolor='black',) #plt.savefig('prueba6.pdf') #plt.savefig("obesity.png", bbox_inches='tight', pad_inches=0.1) #Zeros #HUC8_Ecoregion_PTL = pd.DataFrame(HUC8_diff.HUC_8, columns=['HUC_8']) #HUC8_Ecoregion_PTL['PTL'] = 0 # #HUC8_Ecoregion_NH4 = pd.DataFrame(HUC8_diff.HUC_8, columns=['HUC_8']) #HUC8_Ecoregion_NH4['NH4'] = 0 # #HUC8_Ecoregion = pd.merge(HUC8_Ecoregion_PTL, HUC8_Ecoregion_NH4, how='inner', on='HUC_8') #HUC8_Ecoregion = HUC8_Ecoregion.set_index('HUC_8', drop=True) # #NARS_NLA_FINAL_P = pd.concat([NARS_NLA_group_aux_filt_P, HUC8_Ecoregion], ignore_index=False, sort=False) #NARS_NLA_FINAL_N = pd.concat([NARS_NLA_group_aux_filt_N, HUC8_Ecoregion], ignore_index=False, sort=False) # #HUC8_NARS_NLA_FINAL_P = HUC8.merge(NARS_NLA_FINAL_P, on='HUC_8') #HUC8_NARS_NLA_FINAL_N = HUC8.merge(NARS_NLA_FINAL_N, on='HUC_8') #Precomputed dataframe #HUC8_NARS_NLA_FINAL_P = pd.read_csv('HUC8_NARS_NLA_FINAL_P.csv', sep=",", header=0) #HUC8_NARS_NLA_FINAL_N = pd.read_csv('HUC8_NARS_NLA_FINAL_N.csv', sep=",", header=0) HUC8_NARS_NLA_FINAL_P = geopandas.GeoDataFrame.from_file( 'HUC8_NARS_NLA_FINAL_P/HUC8_NARS_NLA_FINAL_P.shp') HUC8_NARS_NLA_FINAL_N = geopandas.GeoDataFrame.from_file( 'HUC8_NARS_NLA_FINAL_N/HUC8_NARS_NLA_FINAL_N.shp') HUC8_NARS_NLA_FINAL_P.crs = {'init': 'epsg:4326'} HUC8_NARS_NLA_FINAL_N.crs = {'init': 'epsg:4326'} #---------------------------------------------------------------------------------- proj = gcrs.AlbersEqualArea() norm = colors.LogNorm() cmap = matplotlib.cm.viridis cmap.set_under('grey') ax = gplt.choropleth( HUC8_NARS_NLA_FINAL_P, hue='PTL', projection=proj, norm=norm, cmap=cmap, k=5, scheme='quantiles', legend=True, legend_kwargs={'loc': 'lower right'}, figsize=(12, 12), vmin=HUC8_NARS_NLA_FINAL_P[ HUC8_NARS_NLA_FINAL_P['PTL'] > 0]['PTL'].min(), zorder=-100 ) #, vmin=0.8, vmax=HUC8_NARS_NLA_FINAL['PTL'].max() , linewidth=0.5, edgecolor='black',) gplt.pointplot(point, ax=ax, projection=proj, s=300, color='none', edgecolors='r', linewidth='3') gplt.pointplot(point, ax=ax, projection=proj, color='red') # plt.savefig('NARS_NLA_FINAL_P.pdf') plt.savefig("./../static/images/NARS_NLA_FINAL_P.png", bbox_inches='tight', pad_inches=0.1) cmap = matplotlib.cm.viridis cmap.set_under('grey') ax = gplt.choropleth( HUC8_NARS_NLA_FINAL_N, hue='NH4', projection=proj, norm=norm, cmap=cmap, k=5, scheme='quantiles', legend=True, legend_kwargs={'loc': 'lower right'}, figsize=(12, 12), vmin=HUC8_NARS_NLA_FINAL_N[ HUC8_NARS_NLA_FINAL_N['NH4'] > 0]['NH4'].min(), zorder=-100 ) #, vmin=0.8, vmax=HUC8_NARS_NLA_FINAL['PTL'].max() , linewidth=0.5, edgecolor='black',) gplt.pointplot(point, ax=ax, projection=proj, s=300, color='none', edgecolors='r', linewidth='3') gplt.pointplot(point, ax=ax, projection=proj, color='red') # plt.savefig('NARS_NLA_FINAL_N.pdf') plt.savefig("./../static/images/NARS_NLA_FINAL_N.png", bbox_inches='tight', pad_inches=0.1) # ============================================================================= # GIS data retrieval # ============================================================================= # HUC8_NARS_NLA_FINAL_P_index = HUC8_NARS_NLA_FINAL_P.set_index('HUC_8', drop=False) # HUC8_NARS_NLA_FINAL_N_index = HUC8_NARS_NLA_FINAL_N.set_index('HUC_8', drop=False) # # TP_GIS = HUC8_NARS_NLA_FINAL_P_index.loc[HUC8ContPoint, 'PTL'] # NH4_GIS = HUC8_NARS_NLA_FINAL_N_index.loc[HUC8ContPoint, 'NH4'] return
The last demo shows a power scale. This is useful for data that follows a power law distribution of some kind. Again, this doesn't work too well in our case. """ import geopandas as gpd import geoplot as gplt import geoplot.crs as gcrs import numpy as np import matplotlib.pyplot as plt continental_usa_cities = gpd.read_file(gplt.datasets.get_path('usa_cities')) continental_usa_cities = continental_usa_cities.query( 'STATE not in ["AK", "HI", "PR"]') contiguous_usa = gpd.read_file(gplt.datasets.get_path('contiguous_usa')) proj = gcrs.AlbersEqualArea(central_longitude=-98, central_latitude=39.5) f, axarr = plt.subplots(2, 2, figsize=(12, 9), subplot_kw={'projection': proj}) polyplot_kwargs = {'facecolor': (0.9, 0.9, 0.9), 'linewidth': 0} pointplot_kwargs = { 'scale': 'ELEV_IN_FT', 'edgecolor': 'white', 'linewidth': 0.5, 'color': 'black' } gplt.polyplot(contiguous_usa.geometry, ax=axarr[0][0], **polyplot_kwargs) gplt.pointplot(continental_usa_cities.query("POP_2010 > 10000"), ax=axarr[0][0], limits=(0.1, 10), **pointplot_kwargs)
fatal_collisions = gpd.GeoDataFrame(fatal_collisions, geometry=fatal_collisions.apply(pointify, axis='columns')) fatal_collisions = fatal_collisions[fatal_collisions.geometry.map(lambda srs: not (srs.x == 0))] fatal_collisions = fatal_collisions[fatal_collisions['DATE'].map(lambda day: "2016" in day)] injurious_collisions = gpd.GeoDataFrame(injurious_collisions, geometry=injurious_collisions.apply(pointify, axis='columns')) injurious_collisions = injurious_collisions[injurious_collisions.geometry.map(lambda srs: not (srs.x == 0))] injurious_collisions = injurious_collisions[injurious_collisions['DATE'].map(lambda day: "2016" in day)] # Plot the data. fig = plt.figure(figsize=(10,5)) ax1 = plt.subplot(121, projection=gcrs.AlbersEqualArea(central_latitude=40.7128, central_longitude=-74.0059)) gplt.polyplot(boroughs, ax=ax1, projection=gcrs.AlbersEqualArea()) gplt.pointplot(fatal_collisions, projection=gcrs.AlbersEqualArea(), hue='BOROUGH', categorical=True, edgecolor='white', linewidth=0.5, zorder=10, scale='NUMBER OF PERSONS KILLED', limits=(2, 8), legend=True, legend_var='scale', legend_kwargs={'loc': 'upper left'}, legend_values=[2, 1], legend_labels=['2 Fatalities', '1 Fatality'], ax=ax1) plt.title("Fatal Crashes in New York City, 2016") ax2 = plt.subplot(122, projection=gcrs.AlbersEqualArea(central_latitude=40.7128, central_longitude=-74.0059)) gplt.polyplot(boroughs, ax=ax2, projection=gcrs.AlbersEqualArea()) gplt.pointplot(injurious_collisions, projection=gcrs.AlbersEqualArea(), hue='BOROUGH', categorical=True, edgecolor='white', linewidth=0.5, zorder=10,
@pytest.mark.mpl_image_compare @pytest.mark.parametrize("proj", [ gcrs.PlateCarree(), gcrs.LambertCylindrical(), gcrs.Mercator(), gcrs.Miller(), gcrs.Mollweide(), gcrs.Robinson(), gcrs.Sinusoidal(), pytest.param(gcrs.InterruptedGoodeHomolosine(), marks=pytest.mark.xfail), pytest.param(gcrs.Geostationary(), marks=pytest.mark.xfail), gcrs.NorthPolarStereo(), gcrs.SouthPolarStereo(), gcrs.Gnomonic(), gcrs.AlbersEqualArea(), gcrs.AzimuthalEquidistant(), gcrs.LambertConformal(), gcrs.Orthographic(), gcrs.Stereographic(), pytest.param(gcrs.TransverseMercator(), marks=pytest.mark.xfail), gcrs.LambertAzimuthalEqualArea(), gcrs.WebMercator() ]) def test_basic_global_projections(proj, countries): gplt.polyplot(countries, proj) ax = plt.gca() ax.set_global() return plt.gcf()
# %% [markdown] Collapsed="false" # Maps with color-coding based on value in table # # + scheme=None—A continuous colormap. # + scheme=”Quantiles”—Bins the data such that the bins contain equal numbers of samples. # + scheme=”EqualInterval”—Bins the data such that bins are of equal length. # + scheme=”FisherJenks”—Bins the data using the Fisher natural breaks optimization procedure. # # (Example from geoplots gallery) # %% Collapsed="false" cali = gpd.read_file(gplt.datasets.get_path('california_congressional_districts')) cali['area'] =cali.geometry.area proj=gcrs.AlbersEqualArea(central_latitude=37.16611, central_longitude=-119.44944) fig, axarr = plt.subplots(2, 2, figsize=(12, 12), subplot_kw={'projection': proj}) gplt.choropleth( cali, hue='area', linewidth=0, scheme=None, ax=axarr[0][0] ) axarr[0][0].set_title('scheme=None', fontsize=18) scheme = mc.Quantiles(cali.area, k=5) gplt.choropleth( cali, hue='area', linewidth=0, scheme=scheme, ax=axarr[0][1] ) axarr[0][1].set_title('scheme="Quantiles"', fontsize=18) scheme = mc.EqualInterval(cali.area, k=5) gplt.choropleth(
import numpy as np import matplotlib.pyplot as plt import geoplot as gplt import geoplot.crs as gcrs import geopandas as gpd import scipy.stats as stats import descartes from shapely.geometry import Point, Polygon gun_df = pd.read_csv('./data/stage3.csv') clean_lat_long = gun_df[['n_killed', 'n_injured', 'longitude', 'latitude']].dropna() geometry = [ Point(xy) for xy in zip(clean_lat_long['longitude'], clean_lat_long['latitude']) ] crs = {'init': 'epsg:4326'} geo_df = gpd.GeoDataFrame(clean_lat_long, crs=crs, geometry=geometry) contiguous_usa = gpd.read_file(gplt.datasets.get_path('contiguous_usa')) ax = gplt.polyplot(contiguous_usa, projection=gcrs.AlbersEqualArea()) gplt.kdeplot(geo_df[geo_df['n_killed'] > 0], cmap='Reds', shade=True, thresh=0, clip=contiguous_usa, ax=ax) plt.show()
""" Sankey of traffic volumes in Washington DC ========================================== This example plots `annual average daily traffic volume <https://en.wikipedia.org/wiki/Annual_average_daily_traffic>`_ in Washington DC. """ import geopandas as gpd import geoplot as gplt import geoplot.crs as gcrs import matplotlib.pyplot as plt dc_roads = gpd.read_file(gplt.datasets.get_path('dc_roads')) gplt.sankey( dc_roads, projection=gcrs.AlbersEqualArea(), scale='aadt', limits=(0.1, 10), color='black' ) plt.title("Streets in Washington DC by Average Daily Traffic, 2015")
cities = gpd.read_file(geoplot_data.usa_cities()) contiguous_usa = gpd.read_file(geoplot_data.contiguous_usa()) # Plot the data. import geoplot as gplt import geoplot.crs as gcrs import numpy as np import matplotlib.pyplot as plt f, axarr = plt.subplots(2, 2, figsize=(12, 8), subplot_kw={ 'projection': gcrs.AlbersEqualArea(central_longitude=-98, central_latitude=39.5) }) polyplot_kwargs = { 'projection': gcrs.AlbersEqualArea(), 'facecolor': (0.9, 0.9, 0.9), 'zorder': -100, 'linewidth': 0 } pointplot_kwargs = { 'projection': gcrs.AlbersEqualArea(), 'scale': 'ELEV_IN_FT', 'edgecolor': 'white', 'linewidth': 0.5, 'color': 'black' }
def geospatial_viz(geo_data_url, point_data_url=None, att_var=None, map_type=None): ''' function to visualize the attribute information in map. (eg, population in states) geo_att_data: geodataframe that contains both geometry and attributes info att_var: the attributes to be visualized in the map map_type: string, the type of map to be viz. pointplot, choropleth, voronoi if point_data = None, att_var must be from geo_data ''' geo_data = gpd.read_file(geo_data_url) print(geo_data.head()) if point_data_url == 'No point attribute data': if att_var is None: ax = gplt.polyplot(geo_data, figsize=(10, 5)) ax.set_title('plain map of continental USA', fontsize=16) else: if map_type == 'choropleth': scheme = mc.FisherJenks(geo_data[att_var], k=5) labels = scheme.get_legend_classes() ax = gplt.polyplot(geo_data, projection=gcrs.AlbersEqualArea()) gplt.choropleth(geo_data, hue=att_var, edgecolor='white', linewidth=1, cmap='Blues', legend=True, scheme=scheme, legend_labels=labels, ax=ax) ax.set_title('{} in the continental US'.format(att_var), fontsize=16) if map_type == "cartogram": gplt.cartogram(geo_data, scale=att_var, edgecolor='black', projection=gcrs.AlbersEqualArea()) else: point_data = gpd.read_file(point_data_url) scheme = mc.Quantiles(point_data[att_var], k=5) labels = scheme.get_legend_classes() if map_type == 'pointplot': if isinstance(point_data.geometry[0], shapely.geometry.point.Point): ax = gplt.polyplot(geo_data, edgecolor='white', facecolor='lightgray', figsize=(12, 8) #projection = gcrs.AlbersEqualArea() ) gplt.pointplot(point_data, ax=ax, hue=att_var, cmap='Blues', scheme=scheme, scale=att_var, legend=True, legend_var='scale', legend_kwargs={"loc": 'lower right'}, legend_labels=labels) ax.set_title( 'Cities in the continental US, by population 2010', fontsize=16) else: print('Geometry data type not valid') if map_type == "voronoi": # check uniqueness of coordinates duplicates = point_data.geometry.duplicated() point_data_unique = point_data[-duplicates] proj = gplt.crs.AlbersEqualArea(central_longitude=-98, central_latitude=39.5) ax = gplt.voronoi(point_data_unique, hue=att_var, clip=geo_data, projection=proj, cmap='Blues', legend=True, edgecolor="white", linewidth=0.01) gplt.polyplot(geo_data, ax=ax, extent=geo_data.total_bounds, edgecolor="black", linewidth=1, zorder=1) plt.title("{} in US cities".format(att_var), fontsize=16)
test = test_precincts.join(df, how='left') test_precincts = test_precincts.sort_index() test_precincts['count'] = test['count'] test_precincts['Educated'] = test['Educated'] test_precincts['Income'] = test['Income'] test_precincts['Population'] = test['Population'] test_precincts['Median age'] = test['Median age'] test_precincts['Sex ratio (males per 100 females)'] = test[ 'Sex ratio (males per 100 females)'] test_precincts = test_precincts.fillna(method='bfill') f, axarr = plt.subplots(1, 2, figsize=(20, 10), subplot_kw={ 'projection': gcrs.AlbersEqualArea(central_latitude=40.7128, central_longitude=-74.0059) }) # Educated f.suptitle('Comparison between Educated level and number of crimes', fontsize=16) f.subplots_adjust(top=0.95) gplt.choropleth(test_precincts, hue='Educated', projection=gcrs.AlbersEqualArea(central_latitude=40.7128, central_longitude=-74.0059), linewidth=0, figsize=(12, 12), scheme='Fisher_Jenks', cmap='Reds', legend=True,
def plotting_heat_map(file_name='toronto_data.csv',\ json_name='https://raw.githubusercontent.com/jasonicarter/toronto-geojson/master/toronto_crs84.geojson',\ max_distance=15,\ min_bedrooms=1,\ max_bedrooms=1,\ max_rent=5000): """ Plots a heat map based on the DataFrame supplied to the function. The function plots rents by number of bedrooms on a map Args: file_name: file name to be called (default='toronto_data.csv') json_name: file address for the map of the city on which data is superimposed (defualt='https://raw.githubusercontent.com/jasonicarter/toronto-geojson/master/toronto_crs84.geojson') max_distance: exclude renatal that are more than max_distance away from the center (default=5) min_bedrooms: excludes rentals with less than min_bedrooms bedrooms (default=0) max_bedrooms: excludes rentals with more than max_bedrooms bedrooms (default=0) max_rent: excludes rentals that cost more than max_rent (default=5000) """ # imports the .csv file and tells the data from that coordaintes are a list df = pd.read_csv(file_name, converters={'coordinates': literal_eval}) # breaks down the coordinates to lat and long df[['lat', 'long']] = pd.DataFrame(df.coordinates.values.tolist(), index=df.index) # drops all the lat and long na observations df = df.dropna(subset=['lat']) df = df[df['distance'] <= max_distance] df = df[df['rent'] <= max_rent] # drops all rentals with more than max_bedrooms and less than min_bedrooms df = df[df['bedroom'] >= min_bedrooms] df = df[df['bedroom'] <= max_bedrooms] # creates a new column that keeps both lat and long gdf = geopd.GeoDataFrame(df, geometry=geopd.points_from_xy(df.long, df.lat)) # create backgroup map with city geojson data city_json = 'https://raw.githubusercontent.com/jasonicarter/toronto-geojson/master/toronto_crs84.geojson' # loading the map for the city city_map = geopd.read_file(city_json) # creating the map ax = gplt.webmap(city_map, projection=gcrs.WebMercator()) # plots rents by color on the map of the city gplt.pointplot(gdf, ax=ax, hue='rent', legend=True, projection=gcrs.AlbersEqualArea()) # saves the plot as a .png file plt.savefig('heat_map_' + file_name.replace('.csv', '.png'))