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â)