Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! ✅Check out the new docs.

Choropleth code not displaying the color for distribution across USA map

Hey guys,
This is my first time using Plotly and I am trying to use Plotly to display the data on the map for USA for the following data sets.

I created a new data frame to isolate the ibu of each brewery in the state, imputed the missing data sets for ibu, calculated the mean for breweries in each state and now i want to display it for each state using the map, Below is my code, but i can’t get the map to display the distribution as a color map. Any help would be greatly appreciated.

The map that is created is empty and displays no information.

import pandas as pd

Import the data

data_beers = pd.read_csv(’…/beers.csv’)
data_brew = pd.read_csv(’…/breweries.csv’)

The first columns of breweries doesn’t have a label, so add the label brewery_id to it.

data_brew.columns = [‘brewery_id’, ‘name’, ‘city’, ‘state’]

merge_data = pd.merge(data_beers, data_brew, on=[‘brewery_id’])

#print(merge_data[0:3]) # to make sure that the tables merged.

from sklearn.preprocessing import Imputer
imputer = Imputer()
##Get state name
df_state = pd.DataFrame(merge_data[‘state’])
##Data frame for ibu, mean is 42.713
values = merge_data[‘ibu’].values
values= values.reshape(-1,1)
ibu = imputer.fit_transform(values)

df_ibu = pd.DataFrame(ibu)

#concatenate State and ibu
frames= [df_state, df_ibu]
state_ibu = pd.concat(frames, axis = 1)
state_ibu.columns = [‘state’, ‘ibu’]

Group the values by states and take the mean of the values

state_mean = state_ibu.groupby(‘state’)[‘ibu’].agg([‘count’,‘mean’]).reset_index()

MAKE MAPS

import plotly
import plotly.plotly as py

plotly.tools.set_credentials_file(username=‘username’, api_key=‘API_KEY’)

#for col in state_mean.columns:

state_mean[col] = state_mean[col].astype(str)

scl = [[0.0, ‘rgb(242,240,247)’],[0.2, ‘rgb(218,218,235)’],[0.4, ‘rgb(188,189,220)’],
[0.6, ‘rgb(158,154,200)’],[0.8, ‘rgb(117,107,177)’],[1.0, ‘rgb(84,39,143)’]]
state_mean[‘text’]= ‘Breweries =’+ state_mean[‘count’].astype(str)+’
’+
‘Mean IBU=’+state_mean[‘mean’].astype(str)

data= [dict(
type=‘choropleth’,
colorscale = scl,
autocolorscale = False,
locations = state_mean[‘state’],
z = state_mean[‘mean’].astype(float),
locationmode = ‘USA-states’,
text = state_mean[‘text’],
marker = dict(
line = dict(
color = ‘rgb(255,255,255)’,
width = 2
)),
colorbar = dict(title =‘ibu spread’)
)]

layout= dict(
title =‘How strong the beer of your state’,
geo=dict(
scope=‘usa’,
projection=dict( type=‘albers usa’),
showlakes = True,
lakecolor = ‘rgb(255, 255, 255)’),
)

fig = dict( data=data, layout=layout )
py.iplot(fig, filename=‘d3-cloropleth-map’)