Reputation: 1
I saw on: https://plotly.com/python/county-choropleth/ the following Example code and adjusted to my Jupiter Notebook and it worked, but now something went wrong and the error I recieve indicate missing packages/ modules, and I am kinda new to python and tried many things that didn't work please help me what packages/ modules do I miss?
!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
import plotly.figure_factory as ff
import numpy as np 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()
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={'color': 'rgb(255,255,255)', 'width': 0.5}, exponent_format=True, ) fig.layout.template = None fig.show()
Upvotes: 0
Views: 42
Reputation: 35090
As far as I can tell, there has been a similar question in the plotly community, but no solution. This is not recommended as it uses the previous map creation functionality; I suggest an alternative solution using a geojson file. Take the US geojson file from the reference and combine it with the Florida data. Use this combined geodata frame to visualize the population data. Change the target elements to the data that suits your purposes.
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()
Upvotes: 0