pandas 错误安装正确的包/模块以创建美国爱荷华州县的Map

6bc51xsx  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(73)

我在:https://plotly.com/python/county-choropleth/上看到了下面的示例代码,并根据我的Jupiter Notebook进行了调整,它工作了,但现在出了问题,我收到的错误指示缺少包/模块,我对python有点陌生,尝试了许多不起作用的东西,请帮助我,我错过了什么包/模块?
! pip install plotly-geo == 1.0.0! pip install geopandas == 0.3.0! pip install pyshp == 1.2.10! pip install shapely == 1.6.3
将plotly. figure_factory导入为ff
import numpy as np import pandas pd
© 2019 www.githubusercontent.com.cn版权所有并保留所有权利pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv') df_sample_r = df_sample[df_sample['STNAME'] == 'Florida']
values = df_sample_r ['TOT_POP']. tolist()fips = df_sample_r ['FIPS ']. tolist()
endpts = list(np. mgrid [min(values):max(values):4j])colorscale =["#030512","#1d1d3b","#323268","#3d4b94","#3e6ab0","#4989bc","#60a7c7","#85c5d3","#b7e0e4","#eafcfd"] fig = ff. create_choropleth(fips = fips,values = values,scope = ['Florida '],show_state_data = True,colorscale = colorscale,binning_endpoints = endpts,round_legend_values = True,plot_bgcolor ='rgb(229,229,229)',paper_bgcolor ='rgb(229,229,229)',legend_title ='Population by County',county_outline ='rgb(255,255,255)','width':0.5},exponent_format = True,)fig. layout. template = None www.example.com()fig.show()

mnemlml8

mnemlml81#

据我所知,在plotly community中也有类似的问题,但没有解决方案。我建议使用geojson文件的替代解决方案。从参考中获取US geojson文件,并将其与佛罗里达的数据相结合。使用此组合的地理数据框架来可视化人口数据。将目标元素更改为适合您目的的数据。

import pandas as pd

df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv')
df_sample_r = df_sample[df_sample['STNAME'] == 'Florida']
values = df_sample_r['TOT_POP'].tolist()
fips = df_sample_r['FIPS'].tolist()

df_sample_r['FIPS'] = df_sample_r['FIPS'].astype('str') 

import geopandas as gpd

url = 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
geo_counties = gpd.read_file(url)
geo_df = geo_counties.merge(df_sample_r, left_on='id', right_on='FIPS')
geo_df = geo_df.set_index('id')

import plotly.graph_objects as go

fig = go.Figure(go.Choroplethmapbox(geojson=geo_df.__geo_interface__,#json.loads(geo_df.to_json()), 
                                    locations=geo_df.index,
                                    z=geo_df['TOT_POP'],
                                    colorscale="Viridis",
                                    marker_opacity=0.5,
                                    marker_line_width=0.5
                                   ))

fig.update_layout(mapbox_style="carto-positron",
                  mapbox_zoom=5,
                  mapbox_center = {"lat": 27.6648, "lon": -81.5157})
fig.update_layout(height=600, margin={"r":0,"t":20,"l":0,"b":0})
fig.show()

相关问题