HOW TO

Modify the symbology of a feature or a raster programmatically using Python

Last Published: April 25, 2020

Summary

The symbology property of a feature or a layer is a read-only property unless the symbology type is Graduated Colors, Graduated Symbols, Raster Classified, or Unique Values. Therefore, if the symbology type does not fall in any of these four categories, the symbology property cannot be modified directly. It can only be modified using a preexisting .lyr file or a arcpy layer object that has the desired symbology.

Note:
To access the symbology property of a feature or raster, a layer object must be created either using the arcpy.mapping module or Make Feature Layer (Data Management).

Procedure

This article describes two ways to apply symbology from an existing layer file, using the Layer object, or using the Apply Symbology From Layer geoprocessing tool.

Using UpdateLayer (arcpy.mapping)
import arcpy
mxd = arcpy.mapping.MapDocument(r'C:\temp\sample.mxd')
df = arcpy.mapping.ListDataFrames(mxd, "Counties")[0]
updateLayer = arcpy.mapping.ListLayers(mxd, "Rivers", df)[0]
sourceLayer = arcpy.mapping.Layer(r"C:\Project\Data\Rivers.lyr")
arcpy.mapping.UpdateLayer(df,updateLayer,sourceLayer, symbology_only = True)
Note:
If the symbology_only property in the UpdateLayer function is set to True, only the layer's symbology is updated. 
Using Apply Symbology From Layer (Data Management)
import arcpy
mxd = arcpy.mapping.MapDocument(r'C:\temp\sample.mxd')
df = arcpy.mapping.ListDataFrames(mxd, "Counties")[0]
in_layer = arcpy.mapping.ListLayers(mxd, "Rivers", df)[0]
in_symbology_layer = arcpy.mapping.Layer(r"C:\Project\Data\Rivers.lyr")
arcpy.ApplySymbologyFromLayer_management(in_layer,in_symbology_layer)

Article ID:000012251

Software:
  • ArcMap

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic