Visualizing Data with the Spatially Enabled DataFrame

The Spatially Enabled Dataframe has a plot() method that uses a syntax and symbology similar to matplotlib for visualizing features on a map. With this functionality, you can easily visualize aspects of your data both on a map and on a matplotlib chart using the same symbology!

Some unique characteristics of working with the visualization capabalities on the SDF:

  • Uses Pythonic syntax
  • Uses the same syntax as visualizing charts on Pandas DataFrames
  • Uses symbology familiar to users of matplotlib
  • Works on features and attributes simultaneously, eliminating to a great extent the need to iterate over all features (rows)
  • Handles reading and writing to multiple formats aiding data conversion

Checkout the Introduction to Spatially Enabled DataFrame guide to learn how to create a Spatially Enabled DataFrame.

Quickstart

Let us read a census data on major cities and load that into a Spatially Enabled DataFrame

from arcgis import GIS
gis = GIS()
# create an anonymous connection to ArcGIS Online and get a public item
item = gis.content.get("85d0ca4ea1ca4b9abf0c51b9bd34de2e")
flayer = item.layers[0]

# Specify a SQL query and get a sub-set of the original data as a DataFrame
df = flayer.query(where="AGE_45_54 < 1500").sdf

# Visualize the top 5 records
df.head()
FIDNAMECLASSSTSTFIPSPLACEFIPSCAPITALPOP_CLASSPOPULATIONPOP2010...MARHH_NO_CMHH_CHILDFHH_CHILDFAMILIESAVE_FAM_SZHSE_UNITSVACANTOWNER_OCCRENTER_OCCSHAPE
01AmmoncityID16160199061518113816...113110633533523.61474727132051271{"x": -12462673.723706165, "y": 5384674.994080...
12BlackfootcityID16160784061194611899...108117438129583.31454731827881441{"x": -12506251.313993266, "y": 5341537.793529...
24BurleycityID16161126061072710345...86113935824993.37388524121831461{"x": -12667411.402393516, "y": 5241722.820606...
36ChubbuckcityID16161468061465513922...128117237035863.40496122933241408{"x": -12520053.904151963, "y": 5300220.333409...
412JeromecityID16164132061140310890...77921038526403.44398529222191474{"x": -12747828.64784961, "y": 5269214.8197742...

5 rows × 50 columns

Note: If the above cell looks new to you, please checkout the Introduction to Spatially Enabled DataFrame guide.

Let us create a map of the United States

m1 = GIS().map("United States")
m1
m1.zoom = 4
m1.center = [39,-98]

Plotting the DataFrame

You can quickly visualize the points by calling the plot() method off the DataFrame's spatial accessor and passing the map you created above.

df.spatial.plot(map_widget= m1)
True

You can customize the symbols, their color, shape, border etc. like you would in a typical matplotlib plot. The rest of this guide talks about such customizations and suggestions to visualize your spatial and non-spatial data.

The code below plots the same set of points on a new map using a common structure used amongst many different Python packages for defining symbology. It is built off of the matplotlib libraries for simple, straightforward plotting. We'll explain some of the parameters below, and the plot() API Reference outlines more options.

m2= GIS().map('United States', zoomlevel=4)
m2
m2.center=[39,-98]
df.spatial.plot(map_widget=m2,
                renderer_type='s',
                symbol_type='simple',
                symbol_style='d', # d - for diamonds
                colors='Reds_r',
                cstep=50,
                outline_color='Blues',
                marker_size=10)
True

If this type of plotting is new to you, this is a good time to checkout matplotlib.pyplot documentation and Pandas dataframe plot documentation to get an idea.

Understanding renderers

In the snippets above, you visualized all the features with the same symbology. While this helps understand the geographic distribution of your data, your Spatially Enabled DataFrame object can do much more. For instance, you can group the features based on certain attributes that you choose and symbolize each group by a certain color or size or both. To help with this, let us understand the concept of renderers.

Renderers define how to visually represent a feature layer by defining symbols to represent individual features. The SeDF provides you with functionality to control the way features appear by choosing the symbol the renderer uses.

Previous versions of the ArcGIS API for Python provided a method to specify a renderer manually, but you had to know details about the renderer before you drew your data. The map.add_layer() method did not provide access to all options avialble for rendering datasets. The new visualization capabilities provided by the SEDF allow you to draw spatial data quickly and easily with access to more rendering options.

Supported renderers

The renderering options below are documented in further detail in the DatFrame Reference. At a high level, you have the following renderers:

RendererSyntaxExplanation
Simple's'renders using one symbol only (the examples in quickstart above)
Unique'u'renders each unique value with a different symbol. Suitable for categorical columns
Unique'u-a'renders each unique value with a different symbol using arcade expressions. Suitable for categorical columns
Class breaks'c'renders each group of values with a different color or size. Suitable for numerical columns
Heatmap'h'renders density of point data as raster of varying colors

Visualizing unique values

Let us explore the major cities census data further and explore if there are any categorical columns

df.columns
Index(['FID', 'NAME', 'CLASS', 'ST', 'STFIPS', 'PLACEFIPS', 'CAPITAL',
       'POP_CLASS', 'POPULATION', 'POP2010', 'WHITE', 'BLACK', 'AMERI_ES',
       'ASIAN', 'HAWN_PI', 'HISPANIC', 'OTHER', 'MULT_RACE', 'MALES',
       'FEMALES', 'AGE_UNDER5', 'AGE_5_9', 'AGE_10_14', 'AGE_15_19',
       'AGE_20_24', 'AGE_25_34', 'AGE_35_44', 'AGE_45_54', 'AGE_55_64',
       'AGE_65_74', 'AGE_75_84', 'AGE_85_UP', 'MED_AGE', 'MED_AGE_M',
       'MED_AGE_F', 'HOUSEHOLDS', 'AVE_HH_SZ', 'HSEHLD_1_M', 'HSEHLD_1_F',
       'MARHH_CHD', 'MARHH_NO_C', 'MHH_CHILD', 'FHH_CHILD', 'FAMILIES',
       'AVE_FAM_SZ', 'HSE_UNITS', 'VACANT', 'OWNER_OCC', 'RENTER_OCC',
       'SHAPE'],
      dtype='object')

The ST column contains state names in abbreviation is a good candidate. We could symbolize the points such that all which fall under the same state get the same symbol:

df[['ST', 'NAME']].head()
STNAME
0IDAmmon
1IDBlackfoot
2IDBurley
3IDChubbuck
4IDJerome
gis = GIS()
m3 = gis.map("United States", zoomlevel=4)
m3
df.spatial.plot(map_widget = m3,
                renderer_type='u', # specify the unique value renderer using its notation 'u'
                col='ST'  # column to get unique values from
               )
True

By default, the API picks a unique color from the jet colormap for each category (US State names in this case) and assigns it. You can override this by choosing your own colormap (prism for instance) and customizing the symbol style, symbol type, outline color, thickness, transparency etc. Keep reading to learn more about these customizations.

It is a good cartographic practice to not have too many unique values in your legend. Human eyes can only preceive so many colors and there are only a limited number of colors to choose from in a given color map before the colors become indistinguishable.

As a safe practice, the Python API caps the maximum number of unique values to 256, the remaining are generalized and plotted with a default color. If you have several unique values, you should consider picking a different column that better represents your data or consider using a different renderer.

Visualizing Unique Values with Arcade Expressions

Arcade is an expression language that can be used across the ArcGIS Platform. Whether writing simple scripts to control how features are rendered, or expressions to control label text, Arcade provides a simple scripting syntax to deliver these capabilities.

In the sense of visualization, Arcade expressions are used to create rich and dynamic symbology. This example will follow the JavaScript guide.

Obtain the Data

Access the enterprise to gain access to the FeatureLayer information and query it into a Spatially Enabled DataFrame.

item = gis.content.get("8444e275037549c1acab02d2626daaee")
flayer = item.layers[0]
df2 = flayer.query().sdf
fset = flayer.query()
from arcgis.geometry import Geometry
g = Geometry(fset.features[0].geometry)
g.as_arcpy

Write out the Arcade Expressions and Stops

Arcade expressions require the stops to be manually provided. In this case, we first create an opacity visual variable based on a percent of dominant parties in registered citizens.

opacity_expression = ("var republican = $feature.MP06025a_B;var democrat = $feature.MP06024a_B;"
                      "var independent = $feature.MP06026a_B;var parties = [republican, democrat, independent];"
                      "var total = Sum(parties);var max = Max(parties);return (max / total) * 100;")
opacity_stops = [
    { "value": 33, "transparency": 0.05 * 255, "label": "< 33%" },
    { "value": 44, "transparency": 1.0 * 255, "label": "> 44%" }]

Next we develop another Arcade expression to obtain the majority party in a given county.

arcade_expression = ("var republican = $feature.MP06025a_B;var democrat = $feature.MP06024a_B;"
                     "var independent = $feature.MP06026a_B;var parties = [republican, democrat, independent];"
                     "return Decode( Max(parties),republican, 'republican', democrat, 'democrat',independent, "
                     "'independent','n/a' );")
uv = [{"label":"Democrat","symbol":{"type":"esriSFS","color":[0,195,255,255],
                                    "outline":{"type":"esriSLS","color":[0,0,0,51],
                                               "width":0.5,"style":"esriSLSSolid"},
                                    "style":"esriSFSSolid"},"value":"democrat"},
      {"label":"Republican","symbol":{"type":"esriSFS","color":[255,0,46,255],"outline":{
          "type":"esriSLS","color":[0,0,0,51],"width":0.5,"style":"esriSLSSolid"},
                                      "style":"esriSFSSolid"},"value":"republican"},
      {"label":"Independent/other party","symbol":{"type":"esriSFS","color":[250,255,0,255],
                                                   "outline":{"type":"esriSLS","color":[0,0,0,51],
                                                              "width":0.5,"style":"esriSLSSolid"},
                                                   "style":"esriSFSSolid"},"value":"independent"}]
gis = GIS()
m3_ua = gis.map('United States', zoomlevel=4)
m3_ua

Visualize the Data

Provide the color scheme, and the arcade_expression to render the data in a dynamic/rich form.

df2.spatial.plot(colors='coolwarm',
                map_widget=m3_ua,
                renderer_type='u-a',  # 'u-a' stands for uniqe value with arcade expression
                unique_values=uv,
                opacity_stops=opacity_stops,
                opacity_expression=opacity_expression,
                arcade_expression=arcade_expression)
True

Visualizing classes with different colors

Often, you may want to classify the numerical values in your data into groups and visualize them on a map. You can accomplish this with a class break renderer which splits your data into specific number of groups and uses color to differentiate each group. You can choose the algorithm that performs the class splits or go with the defualt.

Let us visualize the same major cities point dataset using its POPULATION column.

df[['ST', 'NAME', 'POPULATION']].head()
STNAMEPOPULATION
0IDAmmon15181
1IDBlackfoot11946
2IDBurley10727
3IDChubbuck14655
4IDJerome11403
m4 = gis.map('USA', zoomlevel=4)
m4
df.spatial.plot(map_widget=m4,
               renderer_type='c',  # for class breaks renderer
               method='esriClassifyNaturalBreaks',  # classification algorithm
               class_count=20,  # choose the number of classes
               col='POPULATION',  # numeric column to classify
               cmap='prism',  # color map to pick colors from for each class
               alpha=0.7  # specify opacity
               )
True

You can take this further by plotting a histogram of the same POPULATION column and try to match the colors. To get the class breaks for the above map, you can inspect the layer definition from MapView object as shown below:

dict(m4.layers[0].layer.layerDefinition.drawingInfo.renderer).keys()
dict_keys(['type', 'visualVariables', 'rotationType', 'minValue', 'field', 'defaultSymbol', 'defaultLabel', 'classificationMethod', 'classBreakInfos'])
class_breaks = m4.layers[0].layer.layerDefinition.drawingInfo.renderer.classBreakInfos
print(len(class_breaks))
20

We can loop through each of the class breaks and obtain the min, max values and the colors for each class.

cbs_list = []
cmap_list = []
for cb in class_breaks:
    print(cb.description)  # print the class break labels
    cbs_list.append(cb.classMaxValue)
    cmap_list.append([x/255.0 for x in cb.symbol.color])
7957.0 - 11659.315789473683
11659.315789473683 - 15361.631578947368
15361.631578947368 - 19063.947368421053
19063.947368421053 - 22766.263157894737
22766.263157894737 - 26468.57894736842
26468.57894736842 - 30170.894736842107
30170.894736842107 - 33873.21052631579
33873.21052631579 - 37575.52631578947
37575.52631578947 - 41277.84210526316
41277.84210526316 - 44980.15789473684
44980.15789473684 - 48682.47368421053
48682.47368421053 - 52384.78947368421
52384.78947368421 - 56087.10526315789
56087.10526315789 - 59789.42105263158
59789.42105263158 - 63491.73684210526
63491.73684210526 - 67194.05263157895
67194.05263157895 - 70896.36842105264
70896.36842105264 - 74598.68421052632
74598.68421052632 - 78301.0
78301.0 - 78301.0

Now that we have the individual class splits and the colors, we can plot a histogram using the same breaks and colors.

import matplotlib.pyplot as plt
# build a histogram for the same class breaks
n, bins, patches = plt.hist(df['POPULATION'], bins=cbs_list)

# apply the same color for each class to match the map
idx = 0
for c, p in zip(bins, patches):
    plt.setp(p, 'facecolor', cmap_list[idx])
    idx+=1

plt.title('Histogram of POPULATION column')
Text(0.5, 1.0, 'Histogram of POPULATION column')
<Figure size 432x288 with 1 Axes>

The shape and color of the bars in the histogram match with the number and color of major cities drawn on the map.

Thus using the Spatially Enabled DataFrame we can easily visualize both the spatial and numeric distribution of data using the same symbology.

Colormaps and Colors

The examples above use the default colormap jet. However you can customize them by using the colors paramater. You can specify colors as:

Color Array

RGB + Alpha values can be used to create colors for symbols called in the plot method. RGB stands for red, green, and blue respectively. Each RGB value is a value between 0-255, and the alpha value is a number between 0-255.

Example to produce :

color = [255,0,100,1]

The above example produces a purplish color. Many websites provide details about using colors. For example, see here for a color codes chart.

Color Maps

A color map is a collection of string values that can be given to generate a series of related colors from a defined set.

Color maps can be viewed here: https://matplotlib.org/examples/color/colormaps_reference.html

Color Map Helpers

To better understand the syntax for each input type, the ArcGIS API for Python provides some helper functions:

from arcgis.mapping import display_colormaps

The display_colormaps function provides a quick, easy way to visualize the pre-defined set of colormaps you can use.

display_colormaps()

You can retrieve a list of colormaps as well:

from arcgis.mapping import symbol

colormaps = symbol.ALLOWED_CMAPS
for a,b,c,d,e in zip(colormaps[::5], colormaps[1::5], colormaps[2::5], colormaps[3::5], colormaps[4::5]):
    print("{:<20}{:<20}{:<20}{:<20}{:<}".format(a,b,c,d,e))

You can enter a list of color ramp names as input to the display_colormaps function to filter the output:

display_colormaps(['Greens_r', 'PRGn', 'Dark2', 'Set1'])

The examples above used jet which is the default and some used prism both of which can be seen in the list above. You can customize your charts by using a different colormap of your choice.

Symbology for Simple Renderers

The ArcGIS API for Python provides you the ability to set symbol types so you control data appearance. The show_styles function in the arcgis.mapping module assists you with the syntax to define symbols.

Getting the different symbol styles

from arcgis.mapping import show_styles
# get the styles that are relevant to the current geometry type (points)
show_styles(df.spatial.geometry_type[0])  # the DataFrame in this example is of Point type
MARKERESRI_STYLE
0oCircle (default)
1+Cross
2dDiamond
3sSquare
4xX
show_styles('Polygon')
MARKERESRI_STYLE
0\Backward Diagonal
1/Forward Diagonal
2|Vertical Bar
3-Horizontal Bar
4xDiagonal Cross
5+Cross
6sSolid Fill (default)
show_styles('Line')
MARKERESRI_STYLE
0sSolid (default)
1-Dash
2-.Dash Dot
3-..Dash Dot Dot
4.Dot
5--Long Dash
6--.Long Dash Dot
7nNull
8s-Short Dash
9s-.Short Dash Dot
10s-..Short Dash Dot Dot
11s.Short Dot

For instance, you can represent the same set of points using square symbols instead of default circle using the snippet shown below:

m7 = GIS().map('United States', 4)
m7
m7.center = [39, -98]
df.spatial.plot(map_widget=m7,
                renderer_type='s',
                symbol_type='simple',
                symbol_style='s',  # for square
                cmap='Greens_r',   # color map is Greens_r for green reversed
                cstep=35,   # the individual color in the colormap
                outline_color='binary',
                marker_size=5,
                line_width=.5,)
True

Visualizing line features using simple symbols

Let us search for USA freeway layer and visualize it by looping through the different line symbols

search_result = gis.content.search('title:USA Freeway System AND owner:esri_dm', 
                                  item_type = 'Feature Layer')
freeway_item = search_result[0]
freeway_item
USA Freeway System
This layer presents rural and urban interstate highways.Feature Layer Collection by esri_dm
Last Modified: January 07, 2020
2 comments, 2,862,652 views
freeway_sdf = freeway_item.layers[0].query().sdf
freeway_sdf.head()
OBJECTIDROUTE_NUMCLASSNUMBERSUFFIXDIST_MILESDIST_KMSHAPE
01CG8CG85.129968.25588{"paths": [[[-121.892261711658, 37.32391610061...
12I105I10519.8304131.91401{"paths": [[[-118.368428716304, 33.93055610296...
23I205I20548.2556677.66011{"paths": [[[-122.561879717577, 45.54969107336...
34I215I21593.43523150.36972{"paths": [[[-117.319118714291, 34.14458511089...
45I238I2382.035483.27579{"paths": [[[-122.137358724846, 37.69015308551...
m8 = gis.map('USA', 10)
m8

m8.center=[34.05,-118.2]
m8.zoom=12
m8.basemap='dark-gray'
import time
for row, style in show_styles('Line').iterrows():
    #m8.remove_layers()
    time.sleep(1)  # sleep for 1 second so animation can finish
    print(f"{style['ESRI_STYLE']:19}\t{style['MARKER']}")
    freeway_sdf.spatial.plot(map_widget=m8,
                     renderer_type='s',
                     cmap='Spectral',  # use a different color map
                     symbol_type='simple',
                     symbol_style=style['MARKER']
                    )
Solid (default)    	s
Dash               	-
Dash Dot           	-.
Dash Dot Dot       	-..
Dot                	.
Long Dash          	--
Long Dash Dot      	--.
Null               	n
Short Dash         	s-
Short Dash Dot     	s-.
Short Dash Dot Dot 	s-..
Short Dot          	s.

Visualizing area features using different symbols

from arcgis.features import FeatureLayer
fl = FeatureLayer("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2")
county_sdf = fl.query("STATE_NAME='Washington'", out_sr=4326).sdf
county_sdf.head()
OBJECTIDNAMESTATE_NAMESTATE_FIPSCNTY_FIPSFIPSPOP2000POP2007POP00_SQMIPOP07_SQMI...OWNER_OCCRENTER_OCCNO_FARMS97AVG_SIZE97CROP_ACR97AVG_SALE97SQMIShape_LengthShape_AreaSHAPE
02953AdamsWashington530015300116428175558.59.1...357616536281746808651321.451930.03.8795530.590994{"rings": [[[-119.36961802215399, 46.737285024...
12954AsotinWashington5300353003205512123732.133.1...5612275214021758728269.59640.62.1279050.193369{"rings": [[[-117.48006100031489, 46.085736986...
22955BentonWashington530055300514247516425981.093.3...36344165221078568440291278.791760.03.2591490.531696{"rings": [[[-119.87687899986594, 46.562399986...
32956ChelanWashington5300753007666167193922.324.0...161788843111311141046131.542993.76.2591640.932106{"rings": [[[-121.17981297788657, 47.896832976...
42957ClallamWashington5300953009645257090836.640.2...197577407292721211620.581764.35.6270300.551224{"rings": [[[-124.73315299891175, 48.163761012...

5 rows × 53 columns

m9 = gis.map('Seattle, WA', zoomlevel=6)
m9.basemap='gray'
m9

for idx, style in show_styles('Polygon').iterrows():
    m9.remove_layers()
    time.sleep(0.5)
    
    print(f"{style['ESRI_STYLE']:22}\t{style['MARKER']}")
    county_sdf.spatial.plot(map_widget=m9,
                           renderer_type='s',
                           cmap = 'RdPu',  # use a red to purple color map
                           symbol_type='simple',
                           symbol_style=style['MARKER'],
                           outline_style='s',
                           outline_color=[0,0,0,255],
                           line_width=1.0)
Backward Diagonal     	\
Forward Diagonal      	/
Vertical Bar          	|
Horizontal Bar        	-
Diagonal Cross        	x
Cross                 	+
Solid Fill (default)  	s

Conclusion

The Spatially Enabled DataFrame gives you powerful visualization capabilities that allows you to plot your data on the interactive map widget. You specify colors and symbols using the same syntax that you would specify for a normal Pandas or a matplotlib plot.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.