In [128]:
# Required imports
import datashader as ds
import pandas as pd
import json
from pprint import pprint
import numpy as np
# Trying to visualize the data
import datashader as ds
import datashader.transfer_functions as tf
import holoviews as hv
import geoviews as gv
import holoviews.plotting.mpl
from holoviews.operation.datashader import datashade
from bokeh.plotting import figure, output_notebook, show
from IPython.core.display import HTML, display
import datashader as ds
from datashader.bokeh_ext import InteractiveImage
from datashader.colors import Hot
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline
from functools import partial
import datashader as ds
import datashader.transfer_functions as tf
from datashader.utils import export_image
from datashader.colors import viridis, colormap_select
from colorcet import fire
from shapely.geometry import shape, Point
from sklearn import metrics
from sklearn.cluster import KMeans
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.preprocessing import scale
In [67]:
# Importing data
foursquare_categories_dict = {
"food":0,
"shops":1,
"parks_outdoors":2,
"nightlife":3,
"travel":4,
"arts_entertainment":5,
"building":6,
"education":7}
with open('../datasets/foursquare/venuesBcn.json') as data_file:
data = json.load(data_file)
df = pd.DataFrame([], columns=['category', 'category_id','name', 'lat', 'lng'])
for i in range (len(data)):
# print data[i]
cat = data[i]['categories'][0]['icon']['prefix'].split("/")[5]
cat_id = foursquare_categories_dict[cat]
name = data[i]['name']
lat = data[i]['location']['lat']
lng = data[i]['location']['lng']
df.loc[i] = [cat, cat_id, name, lat, lng]
In [68]:
df.head()
Out[68]:
In [69]:
# Total of venues
df.size
Out[69]:
In [70]:
# Plots options
background = "black"
plot_width = int(900)
plot_height = int(plot_width*7.0/12)
In [72]:
# Creating first datshader
tf.spread(tf.interpolate(ds.Canvas().points(df,'lng', 'lat')), px=3)
Out[72]:
In [73]:
# Importing and configuring bokeh
output_notebook()
x_range=(-8250000,-8210000)
y_range=(4965000,4990000)
def base_plot(tools='pan,wheel_zoom,reset',plot_width=900, plot_height=600, **plot_args):
p = figure(tools=tools, plot_width=plot_width, plot_height=plot_height,
x_range=x_range, y_range=y_range,
outline_line_color=None,
min_border=0, min_border_left=0, min_border_right=0,
min_border_top=0, min_border_bottom=0, **plot_args)
p.axis.visible = False
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
return p
options = dict(line_color=None, fill_color='blue', size=5)
display(HTML("<style>.container { width:90% !important; }</style>"))
In [74]:
# Plotting new datashader of venues. The grid of Eixample is visible!!!
cvs = ds.Canvas(plot_width=800, plot_height=500)
agg = cvs.points(df, 'lng', 'lat')
img = tf.interpolate(agg)
In [75]:
img
Out[75]:
In [76]:
# Testing another datashader plot
def create_image(x_range, y_range, w, h):
cvs = ds.Canvas(plot_width=w, plot_height=h, )
agg = cvs.points(df, 'lng', 'lat')
img = tf.interpolate(agg, cmap=Hot)
return tf.dynspread(img, threshold=0.5, max_px=4)
p = base_plot(background_fill_color="black",responsive=True, plot_width=int(900*1.5), plot_height=int(600*1.5))
InteractiveImage(p, create_image)
Out[76]: