The examples are shown in the above link all about USA level.
Is there any example that only focuses on one state and with all counties on it by Geojson?
Hi @czhao95,
Welome to Plotly forum!!
Here are examples of plotting choropleths for other country than USA
Hey thanks for replying!
I am looking for the county level choropleth maps. i.e. the county map of Illinois . Instead show the whole US map, I only want the Illinois’s map.
I have a geojson file for Illinois counties since I analized the presidential elections.
You can download it : https://github.com/empet/Datasets/blob/master/illinois-election.geojson
or just read it from url.
Its contents:
jdata['features'][0]
{'id': 0,
'type': 'Feature',
'properties': {'county_name': 'Marshall',
'center_lon_lat': [-89.34355051659558, 41.03517190637024],
'party': 1,
'Clinton_Trump': [1789.0, 3785.0]},
'geometry': {'type': 'Polygon',
'coordinates': [[[-89.63683115774177, 41.148569234450726],
[-89.46722307611506, 41.148569234450726],
[-89.32941650979336, 41.148569234450726],
[-89.35945127424809, 41.10370875301229],
[-89.16334192986722, 41.10495487749669],
[-89.04850312459912, 41.10495487749669],
[-89.04850312459912, 40.92551295174295],
[-89.16157517901694, 40.926759076227356],
[-89.4725233286659, 40.92177457828975],
[-89.447788816762, 40.97286568215019],
[-89.63859790859205, 40.97411180663459],
[-89.63683115774177, 41.14109248754432],
[-89.63683115774177, 41.148569234450726]]]}}
import numpy as np
import json
import plotly.graph_objs as go
import urllib.request
def read_geojson(url):
with urllib.request.urlopen(url) as url:
jdata = json.loads(url.read().decode())
if 'id' not in jdata['features'][0].keys():
if 'properties' in jdata['features'][0].keys():
if 'id' in jdata['features'][0]['properties'] and jdata['features'][0]['properties']['id'] is not None:
for k, feat in enumerate(jdata['features']):
jdata['features'][k]['id'] = feat['properties']['id']
else:
for k in range(len(jdata['features'])):
jdata['features'][k]['id'] = k
return jdata
url = ‘https://raw.githubusercontent.com/empet/Datasets/master/illinois-election.geojson’
jdata = read_geojson(url)
center = jdata['features'][79]['properties']['center_lon_lat'] #Macon County center
L = len(jdata['features'])
#Define some synthetic data for z:
z= 10+15*np.random.rand(L)
locations = [feat['id'] for feat in jdata['features']]
text = [feat['properties']['county_name'] for feat in jdata['features']]
mapboxt = open(".mapbox_token").read().rstrip() #my mapbox_access_token
trace = go.Choroplethmapbox(z=z,
locations=locations,
coloraxis='coloraxis',
geojson=jdata,
text=text,
hoverinfo='all',
marker_line_width=0.1, marker_opacity=0.7)
layout = go.Layout(title_text= 'Illinois County Choroplethmapbox',
coloraxis=dict(colorscale='Oranges_r',
colorbar=dict(thickness=20, ticklen=3),
showscale=True),
title_x=0.5, width = 700, height=700,
mapbox = dict(
accesstoken= mapboxt,
bearing=0,
center=dict(
lat=center[1], #Macon County center
lon=center[0]),
pitch=0,
zoom=5.61,
style='basic'
))
fig = go.Figure(data=[trace], layout =layout)
Going to give a try. Thanks a lot!!!
Do you have any start source of the mapbox? I am a new user and actually have no idea how to do it.
Appreciated!!
@czhao95 In a file with the name .mapbox_token
(no extension) save your mapbox token
,
a long string of the form
pk.eyJ2la…
If you don’t have a token yet, you have to register for a free account at https://mapbox.com/ and obtain a Mapbox Access token.
The token can be pasted in your code too, instead of reading it from a file, as I did above:
mapbox_t ="pk.eyJ2la.... "
Thanks a lot! it finally came up.
The hover letter is different than your demo’s, I am wondering if it is because the code is different or I need to install more package to solve it.
I didn’t set the go.Layout.font
, hence it is the default one.
If you don’t like the font used for displaying text in your plot
you can try set it as follows:
fig.update_layout(font_family='font_name', font_size=12)
where the font_family
can be selected from the ones listed below:
help(go.Layout.font)
The 'font' property is an instance of Font
that may be specified as:
- An instance of plotly.graph_objs.layout.Font
- A dict of string/value properties that will be passed
to the Font constructor
Supported dict properties:
color
family
HTML font family - the typeface that will be
applied by the web browser. The web browser
will only be able to apply a font if it is
available on the system which it operates.
Provide multiple font families, separated by
commas, to indicate the preference in which to
apply fonts if they aren't available on the
system. The plotly service (at https://plot.ly
or on-premise) generates images on a server,
where only a select number of fonts are
installed and supported. These include "Arial",
"Balto", "Courier New", "Droid Sans",, "Droid
Serif", "Droid Sans Mono", "Gravitas One", "Old
Standard TT", "Open Sans", "Overpass", "PT Sans
Narrow", "Raleway", "Times New Roman".
size
But as we can read above The web browser will only be able to apply a font if it is available on the system which it operates. Hence you see adifferent font because your browser doesn’t display the plot with the same font as my browser does.
I am also trying to figure out how to get single state counties in a Geojson object. I followed the very basic ‘all counties in USA’ choropleth tutorial on here which was exciting but I was wondering if anyone knew how to filter the main counties JSON shape file used in that tutorial to only give you the states or counties you wanted. I’ve been trying to figure it out with some dictionary and list comprehension and filtering code but have failed. I am new to Python so there might be something absurdly simple to accomplish this for any county or state my heart desires to extract from the main counties json object but even so, any help would be greatly appreciated!