# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import numpy as np
import pandas as pd
import geopandas as gpd
from lets_plot import *

LetsPlot.setup_html()
# -

# ## Prepare the dataset

# +
from sklearn.datasets import fetch_california_housing

california_housing_bunch = fetch_california_housing()
data = pd.DataFrame(california_housing_bunch.data, columns=california_housing_bunch.feature_names)

# Add $-value field to the dataframe.
# dataset.target: numpy array of shape (20640,)
# Each value corresponds to the average house value in units of 100,000.
data['Value($)'] = california_housing_bunch.target * 100000
data.head()

# +
# Draw a random sample from the data set.
# data = data.sample(n=1000)
# -

# ### Use `geopandas` to read a shape file to GeoDataFrame

CA = gpd.read_file("./ca-state-boundary/CA_State_TIGER2016.shp")
CA.head()

# Keeping in mind that our target is the housing value, fill the choropleth over the state contours using `geom_map()`function

# ### Make a plot out of polygon and points
#
# The color of the points will reflect the house age and
# the size of the points will reflect the value of the house.

# +
# The plot base 
p = ggplot(data=data) + scale_color_gradient(name='House Age', low='red', high='green')

# The points layer
points = geom_point(aes(x='Longitude',
                        y='Latitude',
                        size='Value($)',
                        color='HouseAge'), 
#                     data=data,
                    alpha=0.8)

# The map
p + geom_polygon(map=CA, fill='#F8F4F0', color='#B71234')\
  + points\
  + theme(axis_title='blank', axis_text='blank', axis_ticks='blank', axis_line='blank', axis_tooltip='blank')\
  + ggsize(600, 500)
# -

LetsPlot.set(maptiles_zxy(url='https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}@2x.png'))

# ### Make a plot similar to the one above but interactive

p + geom_livemap()\
  + geom_polygon(map=CA, fill='white', color='#B71234', alpha=0.5)\
  + points



StarcoderdataJupyterScriptsDedupFiltered