I have seen Specifying Resource Field For Labeling in ArcPy with ArcMap but it refers to ArcMap and arcpy.mapping whereas I am using arcpy.mp with ArcGIS Pro.

I have a polygon feature class called "Python_fc" in a layer of the same name on my map called "Map" and this feature class (and layer) has a few fields, among them "Id" and "Ratio"

I want to write a code that makes labels on each polygon using the field "Ratio" but when I use the code below it labels by the "Id" field:

for m in aprx.listMaps("Map"):
for lyr in m.listLayers("python_FC"):
        lyr.showLabels = True

How do I get labels to come from the "Ratio" field instead?

1 Answer 1


To change the field used for labeling an ArcGIS Pro layer from ArcPy you will need to Use label classes:

Label classes can be used to restrict labels to certain features or to specify label fields, symbols, scale ranges, label priorities, and sets of label placement options for groups of labels.

This code snippet illustrates how the part bolded above (by me) is done using your code as its starting point:

for m in aprx.listMaps("Map"):
    for lyr in m.listLayers("python_FC"):
        lblClass = lyr.listLabelClasses()[0]
        lblClass.expression = "$feature.Ratio"
        lyr.showLabels = True

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.