在python中创建美国国家啤酒厂位置热图

时间:2017-07-02 13:56:08

标签: python choropleth

我正在尝试在python中为美国啤酒厂的位置创建一个热图。我还希望利用这些代码来映射啤酒风格和地区的abv密度。

我的数据集来自Kaggle https://www.kaggle.com/nickhould/craft-cans

我将两个数据集操作为一个,如下所示:

    #Loading the data set
df_beer = pd.read_csv("C:/Users/phillip.murphy/Desktop/beers.csv")
df_brew = pd.read_csv("C:/Users/phillip.murphy/Desktop/breweries.csv")
df_brew['brewery_id'] = df_brew.index
df = df_beer.merge(df_brew, on="brewery_id")
print(df.head())
df = df.rename(index=str, columns={"name_x":"beer_name", "name_y":"brewery_name"})
## these 2 columns are just the index as well as the brewery ID repeated
df = df.drop(['Unnamed: 0_x', 'Unnamed: 0_y'], axis=1)
## Make ABV a percentage for readability in the plots
df['abv'] = df['abv']*100
print(df.head())

#get rid of NaN
df = df.replace(np.nan, 0)
print(df.head(25))
#convert abv to integer
df['abv'] = df['abv'].astype('float64')

头部的印刷看起来像这样:

   abv  ibu    id            beer_name                           style  \
0  5.0  NaN  1436             Pub Beer             American Pale Lager   
1  6.6  NaN  2265          Devil's Cup         American Pale Ale (APA)   
2  7.1  NaN  2264  Rise of the Phoenix                    American IPA   
3  9.0  NaN  2263             Sinister  American Double / Imperial IPA   
4  7.5  NaN  2262        Sex and Candy                    American IPA   

   brewery_id  ounces               brewery_name  city state  
0         408    12.0  10 Barrel Brewing Company  Bend    OR  
1         177    12.0        18th Street Brewery  Gary    IN  
2         177    12.0        18th Street Brewery  Gary    IN  
3         177    12.0        18th Street Brewery  Gary    IN  
4         177    12.0        18th Street Brewery  Gary    IN  

我正在努力使热图工作,我尝试了几个变化的等值线,我似乎无法让这个东西出于某种原因而工作,非常感谢任何帮助!

  #heatmap
state_number = df['state'].value_counts()
data = [dict(
    type='choropleth',
    colorscale=scl,
    autocolorscale=False,
    showscale=False,
    locations=location,
    z=state_number,
    locationmode='USA-states',
    marker=dict(
        line=dict(
            color='rgb(255, 255, 255)',
            width=2
        )),
)]

layout = dict(
    title='Breweries in the US',
    geo=dict(
        scope='usa',
        projection=dict(type='albers usa'),
        showlakes=True,
        lakecolor='rgb(255, 255, 255)',
        countrycolor='rgb(255, 255, 255)')
)

figure = dict(data=data, layout=layout)

py.plot(figure, filename= 'test2')

0 个答案:

没有答案