Skip to main content

Symbology

Summary

It is through the Symbology class that you can gain access to a feature layer's renderer or a raster layer's colorizer.

Discussion

The typical workflow is to reference a layer's symbology, make changes, and then apply those changes back to the layer.

It is important to first make sure that a layer supports a renderer or colorizer and to know its type before trying to make updates to it. The built in hasattr function can be used for this purpose.

The following feature layer renderers are currently supported in arcpy.mp: SimpleRenderer, GraduatedColorsRenderer, GraduatedSymbolsRenderer, UnclassedColorsRenderer and UniqueValueRenderer. The following raster colorizers are supported: RasterClassifyColorizer, RasterStretchColorizer and RasterUniqueValueColorizer.

The supported renderers and colorizers only expose a basic set of properties. Additional properties are accessible using the CIM. The Layer class has the getSymbologyDefinition and setSymbologyDefinition methods. These methods can be used to update unsupported arcpy.mp renderers or colorizers by either making direct CIM changes or swapping out the symbology referenced from another layer in a Map or a LayerFile. The third and fourth code samples below demonstrate these workflows.

Properties

Name Explanation Data type

colorizer

(Read only)

Gets the colorizer object for a raster layer. The supported types are: RasterClassifyColorizer and RasterUniqueValueColorizer.

Object

renderer

(Read only)

Gets the renderer object for a feature layer. The supported types are: GraduatedColorsRenderer, GraduatedSymbolsRenderer, SimpleRenderer, or UniqueValueRenderer.

Object

Methods

updateColorizer(colorizer_name)

The updateColorizer method is used to change a raster layer's colorizer.

You can only update from supported colorizer to supported colorizer. For example, you can update a raster classify colorizer to a raster unique value colorizer or vice versa, but you can't update to a raster discrete colorizer, because it is not currently supported.

Name Explanation Data type

colorizer_name

The supported renderers are as follows:

  • RasterClassifyColorizer—A raster classify colorizer.

  • RasterStretchColorizer—A raster stretch colorizer.

  • RasterUniqueValueColorizer—A raster unique value colorizer.

String

updateRenderer(renderer_name)

The updateRenderer method is used to change a layer's renderer.

The SimpleRenderer is the same as the single symbol renderer you see in the application. This name is used to be consistent with the .NET API.

Name Explanation Data type

renderer_name

The supported renderers are as follows:

  • GraduatedColorsRenderer—A graduated colors renderer

  • GraduatedSymbolsRenderer—A graduated symbols renderer

  • SimpleRenderer—A single symbol renderer

  • UnclassedColorsRenderer—An unclassed colors renderer

  • UniqueValueRenderer—A unique value renderer

String

Code sample

Symbology example 1

The following script iterates through each feature layer and tests if its symbology supports the renderer property using the hasattr function. Next, if the renderer type is a SimpleRenderer, it will change it to a GraduatedColorsRenderer using the updateRenderer method and then sets its breakCount value to 6. Finally, it applies the symbology back to the layer before saving the results.

import arcpy, os, sys
relpath = os.path.dirname(sys.argv[0])

p = arcpy.mp.ArcGISProject(relpath + r'\\Symbol.aprx')
m = p.listMaps('Map')[0]

for lyr in m.listLayers():
  if lyr.isFeatureLayer:
    sym = lyr.symbology
    if hasattr(sym, 'renderer'):
      if sym.renderer.type == 'SimpleRenderer':
        sym.updateRenderer('GraduatedColorsRenderer')
        sym.renderer.breakCount = 6

        lyr.symbology = sym

p.saveACopy(relpath + r"\\SavedOutput.aprx")
Symbology example 2

The following script first tests if the layer's symbology supports a colorizer property and then confirms if the renderer is a RasterClassifyColorizer. Next is changes the classificationField and breakCount. Finally, it changes ColorRamp to a color ramp named Cyan to Purple.

import arcpy
import os
import sys

relpath = os.path.dirname(sys.argv[0])

p = arcpy.mp.ArcGISProject(relpath + r'\\RasterClassify.aprx')
m = p.listMaps('Map')[0]
l = m.listLayers('Raster1')[0]
sym = l.symbology

if hasattr(sym, 'colorizer'):
    if sym.colorizer.type == 'RasterClassifyColorizer':
        sym.colorizer.classificationField = 'Value'
        sym.colorizer.breakCount = 7
        sym.colorizer.colorRamp = p.listColorRamps('Cyan to Purple')[0]
        l.symbology = sym

p.saveACopy(relpath + r'\\SavedOutput.aprx')
Symbology example 3

The following script demonstrates how to use Python CIM access to modify additional symbology settings not exposed in the arcpy.mp API. The script adds a dashed effect to the outline symbol layer for a polygon symbol. The script first gets the layer's CIM definition. Next it gets the layer's first symbol layer which is the outline symbol. If a layer effect is not present it creates one and then modifies the dashed effect. Finally, the CIM changes are pushed back to the layer. For more information on the CIM, please review the Python CIM Access help topic.

p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Symbology')[0]
l = m.listLayers('States_SingleSymbol')[0]

l_cim = l.getDefinition('V2')         #Get the Layer's CIM definition

#Symbol Level 1 (Solid Stroke)
symLvl1 = l_cim.renderer.symbol.symbol.symbolLayers[0]

#If a dashed effect does not exist, create one
if len(symLvl1.effects) == 0:
  newDash = arcpy.cim.CreateCIMObjectFromClassName('CIMGeometricEffectDashes', 'V2')
  newDash.dashTemplate = [5, 5]
  symLvl1.effects = [newDash]
#If a dashed effects does exist, modify it
else:
  dash = symLvl1.effects[0]
  dash.dashTemplate = [10, 10]

l.setDefinition(l_cim)                #Set the Layer's CIM definition
Symbology example 4

The following script demonstrates how to use Python CIM Access to apply an unsupported renderer. A pie chart renderer from a layer file is applied to a single symbol renderer in a map using the getSymbologyDefinition and setSymbologyDefinition methods.

import os
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Renderers')[0]

relpath = p.homeFolder

#Reference the two layers
l1 = m.listLayers('Counties_SingleSymbol')[0]
l2 = arcpy.mp.LayerFile(os.path.join(relpath, "LYRXs", "Counties_PieChart_MalesFemales.lyrx")).listLayers()[0]

#Set single symbol to Pie chart (from LYRX)
l1.setSymbologyDefinition(l2.getSymbologyDefinition('V3'))