Change symbology of a layer using arcpy in ArcPro

7244
6
Jump to solution
11-05-2020 04:20 PM
JessicaDecuir
New Contributor II

Hello all -

   I'm using ArcPro version 2.5.2. I want to change the symbology of a layer in my map using python. I'm developing and executing the code out of an ArcGIS Notebook. The layer was added to the map from a feature class geodatabase. I've followed the code examples in the ESRI documentation, but cannot produce results.

    The code I've written will run without an error, but the updated symbology won't change/show in the map view. For example, if I "print" details of the layer's symbology after running my script, it will appear that it has been updated. However, the layer in the map view looks the same, and right clicking symbology on the layer shows the old parameters. This problem is not limited to a particular layer, but all the layers in my projects. Where am I going wrong? Any is help appreciated. Below is the generic code:

import arcpy

# define project
project_path = r"C:\Users\person\Desktop\folder\project.aprx"
p = arcpy.mp.ArcGISProject(project_path)

# specify map in project named "Map"
m = p.listMaps('Map')[0]

# specify layer in map named "polygons"
lyr = m.listLayers('polygons')[0]

# access symbology of layer and change symbology to graduated colors
sym = lyr.symbology
sym.updateRenderer('GraduatedColorsRenderer')
sym.renderer.classificationField = 'value'
sym.classificationMethod = 'NaturalBreaks'
sym.renderer.breakCount = 10

# save symbology back onto layer
lyr.symbology = sym

# save project
p.save()
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

I did some testing with an older version (2.1.2) and ran your script in the Python window.  When I used a path to the open project (line 4 in the code), the changes did not appear in the open version.  I closed the project without saving and reopened it.  The changes were there.

I reset the symbology and commented out line 4.  I changed line 5 to:

p = arcpy.mp.ArcGISProject("CURRENT")

The remainder of the script was the same. After running the script in the Python window, the symbology in the project was updated to the new settings.

As a final test, I reset the project symbology, closed Pro and ran the script outside Pro using the path to the project.  When the project was reopened, the changes were visible.

So, if you are working in Pro's Python window, use "CURRENT" otherwise use the path.

View solution in original post

6 Replies
RandyBurton
MVP Alum

I don't see where 'm' in line 11 has been defined. For line 8, try this:

m = p.listMaps('Map')[0]
0 Kudos
JessicaDecuir
New Contributor II

Oops, good catch. I accidentally left that out in the "pseudo code" I originally posted. The code I'm running has the "m" defined, but it's still not working. I edited the original post to define the "m".

0 Kudos
RandyBurton
MVP Alum

And checking line 16, 'value' is a valid field name found in the layer named 'polygons'?

0 Kudos
JessicaDecuir
New Contributor II

Yes, "value" is the name of a column/field in the layer "polygons". After running the script, if I print the layer's symbology, e.g.:

print(lyr.renderer)
print(lyr.classificationMethod)‍‍

The printed symbology properties appear to have updated. However, the change will not be reflected in the layer in the map view, and if I right click the layer in the TOC and go to symbology, it has all the old properties. I'm wondering if it's a bug.

RandyBurton
MVP Alum

I did some testing with an older version (2.1.2) and ran your script in the Python window.  When I used a path to the open project (line 4 in the code), the changes did not appear in the open version.  I closed the project without saving and reopened it.  The changes were there.

I reset the symbology and commented out line 4.  I changed line 5 to:

p = arcpy.mp.ArcGISProject("CURRENT")

The remainder of the script was the same. After running the script in the Python window, the symbology in the project was updated to the new settings.

As a final test, I reset the project symbology, closed Pro and ran the script outside Pro using the path to the project.  When the project was reopened, the changes were visible.

So, if you are working in Pro's Python window, use "CURRENT" otherwise use the path.

JessicaDecuir
New Contributor II

This worked! Thank you!

0 Kudos