コード例 #1
0
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)
コード例 #2
0
# read shape files
q1 = gp.read_file('./Ookla shape data/2020q1')
q2 = gp.read_file('./Ookla shape data/2020q2')
q3 = gp.read_file('./Ookla shape data/2020q3')
q4 = gp.read_file('./Ookla shape data/2020q4')
ma = gp.read_file('./data/export-gisdata.mapc.ma_municipalities').to_crs(
    epsg=4326)

# In[57]:

data_2020 = pd.concat([q1, q2, q3, q4])

# In[59]:

ax = gplt.webmap(data_2020, projection=gcrs.WebMercator())
gplt.choropleth(data_2020,
                hue='avg_d_kbps',
                projection=gcrs.AlbersEqualArea(),
                cmap='Greens',
                legend=True,
                ax=ax)
plt.show()

# In[43]:

# use the location of the centroid of each polygon
data_2020['geometry'] = data_2020['geometry'].centroid

# In[56]:
コード例 #3
0
"""
KDEPlot of Boston AirBnB Locations
==================================

This example demonstrates a combined application of ``kdeplot`` and ``pointplot`` to a
dataset of AirBnB locations in Boston. The result is outputted to a webmap using the nifty
``mplleaflet`` library. We sample just 1000 points, which captures the overall trend without
overwhelming the renderer.

`Click here to see this plot as an interactive webmap.
<https://bl.ocks.org/ResidentMario/868ac097d671df1ed5ec83eed048560c>`_
"""

import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt

boston_airbnb_listings = gpd.read_file(
    gplt.datasets.get_path('boston_airbnb_listings'))

ax = gplt.kdeplot(boston_airbnb_listings,
                  cmap='viridis',
                  projection=gcrs.WebMercator(),
                  figsize=(12, 12),
                  shade=True)
gplt.pointplot(boston_airbnb_listings, s=1, color='black', ax=ax)
gplt.webmap(boston_airbnb_listings, ax=ax)
plt.title('Boston AirBnB Locations, 2016', fontsize=18)
コード例 #4
0
# %% [markdown] Collapsed="false"
# ## Making Maps

# %% Collapsed="false"
f, ax = plt.subplots(dpi = 200)
countries.plot(edgecolor = 'k', facecolor = 'None', linewidth = 0.6, ax = ax)
cities.plot(markersize = 0.5, facecolor = 'red', ax = ax)
lat_am_capitals.plot(markersize = 0.5, facecolor = 'y', ax = ax)
ax.set_title('World Map')
ax.set_axis_off()

# %% [markdown] Collapsed="false"
# ## Static Webmaps

# %% Collapsed="false"
ax = gplt.webmap(countries, projection=gplt.crs.WebMercator(), figsize = (16, 12))
gplt.pointplot(cities, ax=ax, hue = 'POP2015')

# %% [markdown] Collapsed="false"
# ## Aside on Projections

# %% [markdown] Collapsed="false"
# Map projections flatten a globe's surface onto a 2D plane. This necessarily distorts the surface (one of Gauss' lesser known results), so one must choose specific form of 'acceptable' distortion.
#
# By convention, the standard projection in GIS is World Geodesic System(lat/lon - `WGS84`). This is a cylindrical projection, which stretches distances east-west and *results in incorrect distance and areal calculations*. For accurate distance and area calculations, try to use UTM (which divides map into zones). See [epsg.io](epsg.io)

# %% Collapsed="false"
countries.crs

# %% Collapsed="false"
countries_2 = countries.copy()
コード例 #5
0
def plotme(week=weeks[-1][0],
           startweek=weeks[-1][0],
           webmap=False,
           setnorm=False,
           stddev=1,
           savename='RecentMap.jpg',
           closefig=False,
           figsize=(25, 14)):
    projectioncode = 'EPSG:3395'

    fig = plt.figure(figsize=figsize)
    spec = gridspec.GridSpec(ncols=2, nrows=1, width_ratios=[1, 10])

    colname = week.strftime('%Y-%m-%d') + '_chg'
    validmask = np.logical_not(
        np.logical_or(np.isnan(df[colname]), np.isinf(df[colname])))

    normmin, normmax = (df[validmask][colname].min(),
                        df[validmask][colname].max())
    print("DataMin=%d\tDataMax=%d" % (normmin, normmax))

    if webmap:
        ax1 = fig.add_subplot(spec[1], projection=gcrs.WebMercator())
        gplt.webmap(df, ax=ax1)
        projectioncode = 'EPSG:3785'

    else:
        ax1 = fig.add_subplot(spec[1], projection=gcrs.Mercator())

    if setnorm:
        norm = Normalize(vmin=setnorm[0], vmax=setnorm[1], clip=True)
    else:
        #maxmin = np.max(np.abs([df[colname].min(),df[colname].max()]))
        #norm = Normalize(vmin=-maxmin,vmax=maxmin)
        norm = Normalize(vmin=normmin, vmax=normmax.max())

    gplt.choropleth(df[validmask],
                    zorder=10,
                    hue=colname,
                    norm=norm,
                    legend=True,
                    ax=ax1,
                    extent=extent,
                    edgecolor='black',
                    cmap='RdYlGn_r')

    #color NaNs and inf
    gplt.polyplot(df[np.logical_not(validmask)],
                  facecolor='#0000FF',
                  zorder=20,
                  ax=ax1,
                  extent=extent)

    fig.suptitle(
        "Covid-19 Week to Week %% Change in New Cases by Parish for %s" %
        week.strftime('%b %d, %Y').ljust(14))

    ax2 = fig.add_subplot(spec[0])
    ax2.set_title('Weekly New Cases for Louisiana')
    ax2.barh(la_series.index,
             la_series,
             color='y',
             height=6.5,
             label='Weekly New Cases')
    ax2.axhline(week, color='red', label='Current Week')

    ax2.yaxis.set_major_formatter(DateFormatter('%b %d'))

    for tick in ax2.get_yticklabels():
        tick.set_rotation(90)

    ax2.legend()

    fig.text(0.78,
             0.93,
             week.strftime('%b %d, %Y').rjust(12),
             fontsize=18,
             fontfamily='monospace',
             bbox=dict(boxstyle='round', facecolor='#ffffa8', alpha=0.7))

    #get outliers
    mean = df[validmask][colname].mean()
    outdev = df[validmask][colname].std() * stddev
    print("outdev=%d" % outdev)
    label_outliers = df[np.abs(df[colname] - mean) > outdev]

    labels = label_outliers.append(label_places).drop_duplicates()
    centroids = labels['geometry'].to_crs(projectioncode).centroid
    for x, y, name in zip(centroids.x, centroids.y, labels['NAME']):
        ax1.annotate(name.replace(' ', "\n"),
                     xy=(x, y),
                     xytext=(0, -6),
                     textcoords="offset points",
                     zorder=50,
                     ha='center',
                     va='top')

    centroids = df['geometry'].to_crs(projectioncode).centroid
    for x, y, value in zip(centroids.x, centroids.y, df[colname]):
        ax1.annotate(labelgen(value),
                     xy=(x, y),
                     xytext=(0, 6),
                     textcoords="offset points",
                     zorder=50,
                     ha='center',
                     va='center',
                     fontsize=15,
                     bbox=dict(boxstyle='round',
                               facecolor=getfacecolor(value),
                               alpha=0.4))

    fig.tight_layout()

    if savename != None:
        fig.savefig(savename)

    if closefig:
        plt.close()

    return norm
コード例 #6
0
gdf = geopandas.GeoDataFrame(new_data_reduced, geometry='Coordinates')
if DEBUG==1: print(" Sample of the geospatial dataframe: \n", gdf.head())




# Convert from pandas data frame to a geopandas dataframe
if DEBUG==1: print(" Boroughs sample :\n\n", boroughs.head() , "\n\n Collisions sample:\n\n" , collisions.head())
new_data_reduced.rename(columns={'Coordinates':'geometry'}, inplace='True')
final_df = geopandas.GeoDataFrame(new_data_reduced)
if DEBUG==0: print ('\n\nType of new data reduced: ',type(final_df)\
     ,'\n\n Newly renamed columns of new_data_reduced :\n\n', final_df.head())
# Now making the geoplot of the plots with the 

#making the heatmap
boroughs = geopandas.read_file(geoplot.datasets.get_path('nyc_boroughs'))
ax = geoplot.kdeplot(
    final_df,# clip=boroughs.geometry,
    shade=True, cmap='Reds', 
    projection=geoplot.crs.WebMercator())
geoplot.polyplot(boroughs, ax=ax, zorder=1)
geoplot.webmap(boroughs,ax=ax,zoom=12)
plt.show()


# Trying to plot as point data over webmap instead of a heat map over a webmap

ax = geoplot.webmap(boroughs, projection=geoplot.crs.WebMercator(), zoom=12)
geoplot.pointplot(final_df[final_df['Asset_Area'] > 10.0 ] , ax=ax)
geoplot.polyplot(boroughs, ax=ax, zorder=1)
plt.show()
コード例 #7
0
                          stopwords=STOPWORDS).generate(' '.join(
                              incidents_gdf['cat_calc'].values))
    fig = plt.figure(figsize=(10, 8), facecolor='k', edgecolor='k')
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis('off')
    plt.tight_layout(pad=1)
    plt.show()

    ############################################
    # Which incidents are most common, in pie-chart form?
    df['cat_calc'].value_counts().plot.pie(legend=False, figsize=(15, 6))

    ############################################
    # Where are each incident in Arlington County, plotted as points on a web map?
    # as simple points
    ax = gplt.webmap(both_df, projection=gcrs.WebMercator())
    gplt.pointplot(both_df, ax=ax, marker=".",
                   alpha=0.80)  # marker could be o or .
    _ = ax.axis('off')
    ax.set_title("Police incidents in Arlington County, VA")
    plt.show()

    # give a 1-to-n (0 to 1) number for each category and assign to 'arbitrary_hue' value based on 'cat_calc' value
    categories_split_str = [' '.join(c) for c in categories_split] + ['OTHER']
    both_df['arbitrary_hue'] = both_df['cat_calc'].apply(lambda c: float(
        categories_split_str.index(c)) / (len(categories_split_str) - 1))
    ax = gplt.webmap(both_df, projection=gcrs.WebMercator())
    gplt.pointplot(both_df, ax=ax, marker=".", hue='arbitrary_hue',
                   alpha=0.80)  # marker could be o or .
    _ = ax.axis('off')
    ax.set_title("Police incidents in Arlington County, VA")
コード例 #8
0
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'))