Lecture 28 – Maps

Data 94, Spring 2021

In [1]:
from datascience import *
import numpy as np

Table.interactive_plots()

Review: scatter plots and line plots

In [2]:
wm = Table.read_table('data/walmart.csv').select('STREETADDR', 'STRCITY', 'STRSTATE', 'type_store', 'LAT', 'LON', 'YEAR')
wm
Out[2]:
STREETADDR STRCITY STRSTATE type_store LAT LON YEAR
2110 WEST WALNUT Rogers AR Supercenter 36.3422 -94.0714 1962
1417 HWY 62/65 N Harrison AR Supercenter 36.237 -93.0935 1964
2901 HWY 412 EAST Siloam Springs AR Supercenter 36.1799 -94.5021 1965
1621 NORTH BUSINESS 9 Morrilton AR Supercenter 35.1565 -92.7586 1967
3801 CAMP ROBINSON RD. North Little Rock AR Wal-Mart 34.8133 -92.3023 1967
2020 SOUTH MUSKOGEE Tahlequah OK Supercenter 35.9237 -94.9719 1968
2705 GRAND AVE Carthage MO Supercenter 37.169 -94.3116 1968
1500 LYNN RIGGS BLVD Claremore OK Supercenter 36.3271 -95.6119 1968
65 WAL-MART DRIVE Mountain Home AR Supercenter 36.329 -92.3578 1968
1303 SOUTH MAIN Sikeston MO Supercenter 36.8912 -89.5836 1968

... (2982 rows omitted)

In [3]:
wm.group('YEAR')
Out[3]:
YEAR count
1962 1
1964 1
1965 1
1967 2
1968 5
1969 5
1970 5
1971 15
1972 17
1973 19

... (33 rows omitted)

In [4]:
wm.group('YEAR').plot('YEAR',
                     title = 'Number of Walmarts Opened Per Year')
In [5]:
wm_per_year = wm.group('YEAR')
wm_per_year = wm_per_year.with_columns(
    'total', np.cumsum(wm_per_year.column('count'))
)
wm_per_year
Out[5]:
YEAR count total
1962 1 1
1964 1 2
1965 1 3
1967 2 5
1968 5 10
1969 5 15
1970 5 20
1971 15 35
1972 17 52
1973 19 71

... (33 rows omitted)

In [6]:
wm_per_year.plot('YEAR', 'total',
                title = 'Total Number of Walmarts Over Time')

Maps with circles

In [7]:
wm_ca = wm.where('STRSTATE', 'CA')
wm_ca
Out[7]:
STREETADDR STRCITY STRSTATE type_store LAT LON YEAR
44765 VALLEY CENTRAL WAY Lancaster CA Wal-Mart 34.7289 -118.327 1990
2150 NO. WATERMAN AVENUE El Centro CA Supercenter 32.7834 -115.577 1990
2225 PLAZA PARKWAY Modesto CA Wal-Mart 37.6718 -121.012 1990
3223 EAST HAMMER LANE Stockton CA Supercenter 38.0389 -121.242 1991
2300 WHITE LANE Bakersfield CA Wal-Mart 35.3442 -119.022 1991
355 ORO DAM BLVD Oroville CA Wal-Mart 39.5586 -121.589 1991
1977 WEST CLEVELAND AVE Madera CA Wal-Mart 36.9148 -120.158 1991
15272 BEAR VALLEY ROAD Victorville CA Wal-Mart 34.4895 -117.353 1991
911 SOUTH CHINA LAKE BLVD Ridgecrest CA Wal-Mart 35.5992 -117.681 1991
1025 SOUTH MAIN Red Bluff CA Wal-Mart 40.1838 -122.241 1991

... (149 rows omitted)

In [8]:
wm_ca.select('LAT', 'LON')
Out[8]:
LAT LON
34.7289 -118.327
32.7834 -115.577
37.6718 -121.012
38.0389 -121.242
35.3442 -119.022
39.5586 -121.589
36.9148 -120.158
34.4895 -117.353
35.5992 -117.681
40.1838 -122.241

... (149 rows omitted)

In [9]:
Circle.map_table(wm_ca.select('LAT', 'LON'))
Out[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Modifying circle appearance

In [10]:
Circle.map_table(wm_ca.select('LAT', 'LON'),
                 area = 200,
                 weight = 1.5, 
                 line_color = 'gold',
                 color = 'purple', 
                 fill_opacity = 0.8
                 )
Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook

labels

In [11]:
wm_ca.select('LAT', 'LON', 'STREETADDR')
Out[11]:
LAT LON STREETADDR
34.7289 -118.327 44765 VALLEY CENTRAL WAY
32.7834 -115.577 2150 NO. WATERMAN AVENUE
37.6718 -121.012 2225 PLAZA PARKWAY
38.0389 -121.242 3223 EAST HAMMER LANE
35.3442 -119.022 2300 WHITE LANE
39.5586 -121.589 355 ORO DAM BLVD
36.9148 -120.158 1977 WEST CLEVELAND AVE
34.4895 -117.353 15272 BEAR VALLEY ROAD
35.5992 -117.681 911 SOUTH CHINA LAKE BLVD
40.1838 -122.241 1025 SOUTH MAIN

... (149 rows omitted)

In [12]:
wm_ca_labeled = wm_ca.select('LAT', 'LON', 'STREETADDR').relabeled('STREETADDR', 'labels')
wm_ca_labeled
Out[12]:
LAT LON labels
34.7289 -118.327 44765 VALLEY CENTRAL WAY
32.7834 -115.577 2150 NO. WATERMAN AVENUE
37.6718 -121.012 2225 PLAZA PARKWAY
38.0389 -121.242 3223 EAST HAMMER LANE
35.3442 -119.022 2300 WHITE LANE
39.5586 -121.589 355 ORO DAM BLVD
36.9148 -120.158 1977 WEST CLEVELAND AVE
34.4895 -117.353 15272 BEAR VALLEY ROAD
35.5992 -117.681 911 SOUTH CHINA LAKE BLVD
40.1838 -122.241 1025 SOUTH MAIN

... (149 rows omitted)

In [13]:
Circle.map_table(wm_ca_labeled)
Out[13]:
Make this Notebook Trusted to load map: File -> Trust Notebook

color_scale

In [14]:
wm_ca_scales = wm_ca.select('LAT', 'LON', 'STRCITY', 'YEAR') \
                    .relabeled(['STRCITY', 'YEAR'], ['labels', 'color_scale'])

wm_ca_scales
Out[14]:
LAT LON labels color_scale
34.7289 -118.327 Lancaster 1990
32.7834 -115.577 El Centro 1990
37.6718 -121.012 Modesto 1990
38.0389 -121.242 Stockton 1991
35.3442 -119.022 Bakersfield 1991
39.5586 -121.589 Oroville 1991
36.9148 -120.158 Madera 1991
34.4895 -117.353 Victorville 1991
35.5992 -117.681 Ridgecrest 1991
40.1838 -122.241 Red Bluff 1991

... (149 rows omitted)

In [15]:
Circle.map_table(wm_ca_scales,
                fill_opacity = 0.8,
                line_color = None,
                area = 200)
Out[15]:
Make this Notebook Trusted to load map: File -> Trust Notebook

The map above confirms the claims of this LA Times article from 1990, which says:

The company plans to open 10 stores in California in 1990 and 1991, with most to be located in the interior sections of the state. This year, it will open stores in Lancaster, Victorville, El Centro, Madera, Modesto, Ridgecrest and Stockton. In 1991, it plans stores in Elk Grove, Hanford and Bakersfield.

colors

In [16]:
wm_ca
Out[16]:
STREETADDR STRCITY STRSTATE type_store LAT LON YEAR
44765 VALLEY CENTRAL WAY Lancaster CA Wal-Mart 34.7289 -118.327 1990
2150 NO. WATERMAN AVENUE El Centro CA Supercenter 32.7834 -115.577 1990
2225 PLAZA PARKWAY Modesto CA Wal-Mart 37.6718 -121.012 1990
3223 EAST HAMMER LANE Stockton CA Supercenter 38.0389 -121.242 1991
2300 WHITE LANE Bakersfield CA Wal-Mart 35.3442 -119.022 1991
355 ORO DAM BLVD Oroville CA Wal-Mart 39.5586 -121.589 1991
1977 WEST CLEVELAND AVE Madera CA Wal-Mart 36.9148 -120.158 1991
15272 BEAR VALLEY ROAD Victorville CA Wal-Mart 34.4895 -117.353 1991
911 SOUTH CHINA LAKE BLVD Ridgecrest CA Wal-Mart 35.5992 -117.681 1991
1025 SOUTH MAIN Red Bluff CA Wal-Mart 40.1838 -122.241 1991

... (149 rows omitted)

In [17]:
def color_from_type(type_store):
    if type_store == 'Wal-Mart':
        return 'blue'
    else:
        return 'red'
In [18]:
wm_ca = wm_ca.with_columns(
    'colors', wm_ca.apply(color_from_type, 'type_store')
)

wm_ca
Out[18]:
STREETADDR STRCITY STRSTATE type_store LAT LON YEAR colors
44765 VALLEY CENTRAL WAY Lancaster CA Wal-Mart 34.7289 -118.327 1990 blue
2150 NO. WATERMAN AVENUE El Centro CA Supercenter 32.7834 -115.577 1990 red
2225 PLAZA PARKWAY Modesto CA Wal-Mart 37.6718 -121.012 1990 blue
3223 EAST HAMMER LANE Stockton CA Supercenter 38.0389 -121.242 1991 red
2300 WHITE LANE Bakersfield CA Wal-Mart 35.3442 -119.022 1991 blue
355 ORO DAM BLVD Oroville CA Wal-Mart 39.5586 -121.589 1991 blue
1977 WEST CLEVELAND AVE Madera CA Wal-Mart 36.9148 -120.158 1991 blue
15272 BEAR VALLEY ROAD Victorville CA Wal-Mart 34.4895 -117.353 1991 blue
911 SOUTH CHINA LAKE BLVD Ridgecrest CA Wal-Mart 35.5992 -117.681 1991 blue
1025 SOUTH MAIN Red Bluff CA Wal-Mart 40.1838 -122.241 1991 blue

... (149 rows omitted)

In [19]:
wm_ca.select('LAT', 'LON', 'colors')
Out[19]:
LAT LON colors
34.7289 -118.327 blue
32.7834 -115.577 red
37.6718 -121.012 blue
38.0389 -121.242 red
35.3442 -119.022 blue
39.5586 -121.589 blue
36.9148 -120.158 blue
34.4895 -117.353 blue
35.5992 -117.681 blue
40.1838 -122.241 blue

... (149 rows omitted)

In [20]:
Circle.map_table(wm_ca.select('LAT', 'LON', 'colors'),
                fill_opacity = 0.6,
                line_color = None,
                area = 200)
Out[20]:
Make this Notebook Trusted to load map: File -> Trust Notebook

It seems like most Walmarts in California are standard locations and only a few are Supercenters.

What about in the rest of the country?

In [21]:
wm = wm.with_columns(
    'colors', wm.apply(color_from_type, 'type_store')
)

Circle.map_table(wm.select('LAT', 'LON', 'colors'),
                fill_opacity = 0.8,
                line_color = None,
                area = 20)
Out[21]:
Make this Notebook Trusted to load map: File -> Trust Notebook