English

How To: Modify the symbology of a feature or a raster programmatically using Python

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.

This article describes two ways to apply symbology from an existing layer file.

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

Method 1 Using UpdateLayer (arcpy.mapping):

Code:
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,update_layer,source_layer, symbology_only = True)

Note:
If the symbology_only property in the UpdateLayer function is set to True, only the layer's symbology is updated.


Method 2 Using Apply Symbology From Layer (Data Management):

Code:
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)

Related Information