示例#1
0
文件: viz.py 项目: beaulima/seotbx
def plot_footprints(products_gdf):
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
    print(gpd.datasets.available)
    ax = gplt.polyplot(world, projection=gcrs.WebMercator())
    gplt.polyplot(products_gdf,
                  projection=gcrs.WebMercator(),
                  edgecolor='r',
                  ax=ax)

    plt.show()
    toto = 0
示例#2
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)
示例#3
0
    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()


@pytest.mark.mpl_image_compare
@pytest.mark.parametrize("proj", [
    gcrs.EuroPP(),
    gcrs.OSGB(),
])
def test_basic_non_global_projections(proj, countries):
    with warnings.catch_warnings():
# 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]:
示例#5
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)
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
示例#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'))