Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Plotly_Nashvile_Scattermapbox
import pandas as pd
import plotly.plotly as py
from plotly.graph_objs import *
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import matplotlib.pyplot as plt
mapbox_access_token = ###
df = pd.read_csv('https://data.nashville.gov/api/views/j7b8-4fv6/rows.csv?accessType=DOWNLOAD')
df['total_students'] = df.iloc[:, 6:21].sum(axis=1)
df['Percent Disabled'] = df['Disability'] / df['total_students']
df['Percent Limited English Proficiency'] = df['Limited English Proficiency'] / df['total_students']
df['Percent Male'] = df['Male'] / df['total_students']
df['Percent Black or African American'] = df['Black or African American'] / df['total_students']
df['Percent White'] = df['White'] / df['total_students']
df['Percent Economically Disadvantaged'] = df['Economically Disadvantaged'] / df['total_students']
df['Percent Hispanic/Latino'] = df['Hispanic/Latino'] / df['total_students']
df['Percent Asian'] = df['Asian'] / df['total_students']
scl = [[0,"rgb(40,181,51)"], [0.25,"rgb(153,193,64)"], [0.5,"rgb(231,180,22)"],[0.75,"rgb(219,123,43)"],[1,"rgb(204,50,50)"]]
##df.iloc.fillna(0, inplace=True)
cs = scl
ones = ['A']
for i in range(21):
ones.append(1)
ones.append(100)
ones.append(100)
for i in range(20):
ones.append(1)
ones[3]='a'
df.loc[171] = ones
zeros = ['A']
for i in range(21):
zeros.append(1)
zeros.append(100)
zeros.append(100)
for i in range(20):
zeros.append(1)
zeros[3]='a'
df.loc[172] = zeros
ticks = ['Percent Disabled', 'Percent Limited English Proficiency', 'Percent Male', 'Percent White', 'Percent Black or African American', 'Percent Hispanic/Latino', 'Percent Asian']
allTicks =['Percent Economically Disadvantaged']
for i in ticks:
allTicks.append(i)
fTicks = []
for i in range(len(allTicks)):
a = allTicks[i].split(" ")
b = " ".join(a[1:])
fTicks.append(b)
data = Data([
Scattermapbox(
lat=df['Latitude'],
lon=df['Longitude'],
mode='markers',
text=df['School Name'] + "<br>" + 'Percent Economically Disadvantaged: ' + (df['Percent Economically Disadvantaged'].fillna(value=0)*100).astype(dtype='int').astype(dtype='str'),
marker = dict(
size = 13.,
opacity = 0.9,
reversescale = True,
autocolorscale = False,
symbol = 'circle',
colorscale = cs,
cmin = 0,
color = df['Percent Economically Disadvantaged'],
colorbar=dict(
title="Percent Economically Disadvantaged",
titleside = 'right',
tickmode = 'array',
titlefont =dict(size=20),
tickvals = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
ticktext = ['10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%'],
tickangle = 45
)
)
)
])
for tick in ticks:
data.append(data[0].copy())
data[-1]['marker'] = marker = dict(
size = 13.,
opacity = 0.9,
reversescale = True,
autocolorscale = False,
symbol = 'circle',
colorscale = cs,
color = df[tick],
cmin=0,
colorbar=dict(
title= str(tick),
titleside = 'right',
tickmode = 'array',
titlefont =dict(size=20),
tickvals = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
ticktext = ['10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%'],
tickangle = 45
))
data[-1]['text'] = df['School Name'] + "<br>" + str(tick) + ': ' + (df[tick].fillna(value=0)*100).astype(dtype='int').astype(dtype='str')
##let's create the steps for the slider
steps = []
for i in range(len(data)):
step = dict(method='restyle',
args=['visible', [False] * len(data)],
label=fTicks[i])
step['args'][1][i] = True
steps.append(step)
sliders = [dict(active=0,
pad={"t": 1},
steps=steps, font=dict(size=10)) ]
##Set Layout
layout = Layout(
autosize=True,
hovermode='closest',
mapbox=dict(
accesstoken=mapbox_access_token,
bearing=0,
center=dict(
lat=36.1627,
lon=-86.7816
),
pitch=0,
zoom=10.035
),
sliders=sliders,
title="Nashville Public School Demographics<br>As a percentage of school poptulation")
##Graph
fig = dict(data=data, layout=layout)
plot(fig, filename='Multiple Mapbox.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.