Choropleth graphs are maps with coloured areas to display the values to each geo location. Plotly's Choropleth Graph supports various maps, including the world and the US. In this folder, we will go over how to create chropleth graphs with Python and Plotly.
The following scripts are used in this chapter:
- Example_Basic_ChoroplethGraph.py
This chapter requires the following packages for the scripts used:
- Pandas
- Plotly
This chapter may use the following data from the Data folder:
- county_avg_rent.csv
- countyfips.csv
Plotly has a feature to draw borders based on FIPS (Federal Information Processing Standards) defined by the US government, so Plotly could draw the border automatically if you have connected the data with FIPS. countyfips.csv is the table for all FIPS number for all counties in the US.
figure_factory.create_choropleth() is a easy way to create choropleth graphs in the United States, especially only displaying individual state. Different with the go.Figure(), figure_factory.create_choropleth() takes all data parameters and layout parameters.
figure_factory.create_choropleth() has the following parameters:
- fips: FIPS (Federal Information Processing Standards) defined by the US government, an attribute to locate the counties in United States
- values: Values
- colorscale: The set of colours to display the value of each area, see plotly.colors.sequential for colour sets
- show_state_data: (Coming Soon...)
- scope: The map used in the visualization
- binning_endpoints: To map the range of values to colorscale
- county_outline (Dictionary): Define the lines on county borders
- color: Color of county borders, RGB or Hex in string
- width: The width of borderline in px, integer
- state_outline (Dictionary): Define the lines on state borders
- color: Color of county borders, RGB or Hex in string
- width: The width of borderline in px, integer
Choropleth Graph Exclusive parameters:
- fips: FIPS (Federal Information Processing Standards) defined by the US government, an attribute to locate the counties in United States
- show_state_data: (Coming Soon...)
- scope: The map used in the visualization
- binning_endpoints: To map the range of values to colorscale
- county_outline (Dictionary): Define the lines on county borders
- color: Color of county borders, RGB or Hex in string
- width: The width of borderline in px, integer
- state_outline (Dictionary): Define the lines on state borders
- color: Color of county borders, RGB or Hex in string
- width: The width of borderline in px, integer
# Define the endpoints
endpts = list(range(1,6))
fig = ff.create_choropleth(
fips = df['fips'], values = df['median_sqft_price'],
colorscale = plotly.colors.sequential.Oranges,
show_state_data = True, scope = ['CA'],
binning_endpoints=endpts,
county_outline={'color': 'rgb(15,15,55)', 'width': 1},
state_outline={'color': 'rgb(15,15,55)', 'width': 1},
legend_title='Median Rent')
Plotly Documentation Choropleth Graph
