In [1]:
from __future__ import print_function
from ipyleaflet import (
    Map,
    Marker,
    TileLayer, ImageOverlay,
    Polyline, Polygon, Rectangle, Circle, CircleMarker,
    GeoJSON,
    DrawControl
)

In [2]:
center = [34.6252978589571, -77.34580993652344]
zoom = 10

In [3]:
m = Map(center=center, zoom=zoom)
m



In [4]:
import matplotlib as mpl
import matplotlib.cm
import matplotlib.colors
import numpy as np

def n_colors(n, colormap=mpl.cm.Blues):
    data = np.linspace(0.0,1.0,n)
    c = [mpl.colors.rgb2hex(d[0:3]) for d in colormap(data)]
    return c

def data_to_colors(data, colormap=mpl.cm.Blues):
    c = [mpl.colors.rgb2hex(d[0:3]) for d in colormap(mpl.colors.Normalize()(data))]
    return c

In [5]:
import json
with open('demo.json') as f:
    data = json.load(f)

In [6]:
n_features = len(data['features'])
colors = n_colors(n_features)
print(n_features)


20

In [7]:
colors


Out[7]:
[u'#f7fbff',
 u'#edf4fc',
 u'#e3eef8',
 u'#d8e7f5',
 u'#cee0f2',
 u'#c2d9ee',
 u'#b2d2e8',
 u'#a0cbe2',
 u'#8cc0dd',
 u'#75b4d8',
 u'#63a8d3',
 u'#519ccc',
 u'#4090c5',
 u'#3282be',
 u'#2474b7',
 u'#1966ad',
 u'#0e59a2',
 u'#084b93',
 u'#083d7f',
 u'#08306b']

In [8]:
for feature, color in zip(data['features'], colors):
    feature['properties']['style'] = {'color':color, 'weight': 1, 'fillColor':color, 'fillOpacity':0.5}

In [9]:
data['features'][0]


Out[9]:
{u'geometry': {u'coordinates': [[[[-77.39940577291571, 34.5780540544853],
     [-77.39940610066606, 34.57805427015046],
     [-77.39940640189788, 34.57805446836596],
     [-77.39940680648922, 34.57805473459376],
     [-77.39940687019617, 34.57805477651398],
     [-77.39940689381065, 34.57805472199389],
     [-77.3994073308358, 34.57805432604519],
     [-77.39940731720131, 34.578054179242265],
     [-77.39940728534575, 34.578053836252096],
     [-77.39940728147378, 34.57805380828763],
     [-77.39940726378504, 34.57805363205166],
     [-77.39940725501071, 34.57805361742678],
     [-77.39940719593868, 34.57805349818902],
     [-77.39940711258689, 34.57805350039165],
     [-77.39940706262787, 34.57805349996589],
     [-77.3994070238417, 34.578053501157314],
     [-77.39940689549977, 34.57805350509971],
     [-77.3994068702403, 34.5780535173504],
     [-77.39940683257294, 34.57805352755014],
     [-77.39940665584797, 34.57805351246129],
     [-77.39940650171212, 34.578053517196004],
     [-77.39940646539253, 34.578053518311656],
     [-77.39940610067742, 34.578053945657025]]]],
  u'type': u'MultiPolygon'},
 u'id': u'demo0',
 u'properties': {'style': {'color': u'#f7fbff',
   'fillColor': u'#f7fbff',
   'fillOpacity': 0.5,
   'weight': 1}},
 u'type': u'Feature'}

In [10]:
g = GeoJSON(data=data)

In [11]:
m.add_layer(g)

In [ ]:
# m.remove_layer(g)

In [ ]:
# g.close()