In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('white')
%matplotlib inline
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 15, 6
In [2]:
data = pd.read_csv('listings_tot.csv')
In [3]:
num_hogares = pd.read_csv("../datasets/num_hogares/NumHogaresYFamilias2011.csv", sep=";")
In [4]:
num_hogares.head()
Out[4]:
In [5]:
# strip space in the begining of column name
num_hogares.columns = num_hogares.columns.str.lstrip()
In [6]:
# ugly number cleaning
def drop_digits(in_str):
digit_list = "1234567890"
for char in digit_list:
in_str = in_str.str.replace(char, "")
return in_str
In [7]:
num_hogares.Barrio = drop_digits(num_hogares.Barrio)
num_hogares.Barrio = num_hogares.Barrio.str.replace(".", "")
n_airbnbs_barri= data.neighbourhood_cleansed.value_counts()
n_airbnbs_barri = n_airbnbs_barri[n_airbnbs_barri.index.isin(num_hogares.Barrio)]
num_hogares = num_hogares[num_hogares.Barrio.isin(n_airbnbs_barri.index)]
num_hogares.index = num_hogares.Barrio
num_hogares.drop("Barrio", axis=1)[:2]
Out[7]:
In [8]:
num_hogares = num_hogares.NumHogares
#num_hogares.drop(["Can Peguera", "Baró de Viver", "Torre Baró", "Vallbona"], inplace=True)#outliers
num_hogares=num_hogares * 1000 #ugly hack to get rid of the "." decimal
density = n_airbnbs_barri/num_hogares
In [9]:
density.sort_values(ascending=False).plot(kind ="bar",)
plt.ylabel("Listings / Hogares ")
plt.savefig("density.png", format="png")
In [10]:
density.sort_values(ascending=False).head(10).plot(kind="bar")
plt.savefig("density10.png")
In [11]:
barri_json_path = r"../datasets/divisiones_administrativas/barris/barris_geo.json"
In [12]:
from bokeh.io import output_notebook, show
output_notebook()
In [13]:
from bokeh.models import (
GeoJSONDataSource,
HoverTool,
LinearColorMapper
)
from bokeh.plotting import figure
from bokeh.palettes import Viridis6
with open(barri_json_path, 'r') as f:
geo_source = GeoJSONDataSource(geojson=f.read())
color_mapper = LinearColorMapper(palette=Viridis6)
TOOLS = "pan,wheel_zoom,box_zoom,reset,hover,save"
p = figure(title="barris", tools=TOOLS, x_axis_location=None, y_axis_location=None, width=800, height=800)
p.grid.grid_line_color = None
p.patches('xs', 'ys', fill_alpha=0.7,
line_color='white', line_width=0.5, source=geo_source)
hover = p.select_one(HoverTool)
hover.point_policy = "follow_mouse"
hover.tooltips = [("Provincia:", "$properties""$N_Barri")]
#output_file("PBIar.html", title="Testing islands in bokeh")
show(p)
In [14]:
import xarray as xr
import numpy as np
import pandas as pd
import holoviews as hv
import geoviews as gv
import geoviews.feature as gf
import cartopy
from cartopy import crs as ccrs
from bokeh.tile_providers import STAMEN_TONER
from bokeh.models import WMTSTileSource
hv.notebook_extension('bokeh')
In [15]:
density = pd.DataFrame({"N_Barri":density.index, "value":density.values})
In [16]:
shapefile = "../datasets/divisiones_administrativas/barris/shape/barris_geo.shp"
shapes = cartopy.io.shapereader.Reader(shapefile)
density_hv = hv.Dataset(density)
In [35]:
density_hv.data.dropna(inplace=True)
In [22]:
%%opts Overlay [width=800 height=800 xaxis=None yaxis=None]
%%opts Shape (cmap='Blues') [tools=['hover'] width=800 height=800 colorbar=True toolbar='above' xaxis=None yaxis=None]
#%%output filename="holoviews"
gv.Shape.from_records(shapes.records(), density_hv, on='N_Barri', value='value',
index='N_Barri', crs=ccrs.PlateCarree(), group="Densitat Airbnb Barcelona per nombre d'hogars",
drop_missing=False).redim(name='County', leaveVoteshare='Leave Vote %')
#gv.WMTS(WMTSTileSource(url='https://maps.wikimedia.org/osm-intl/{Z}/{X}/{Y}@2x.png'))
Out[22]:
In [ ]: