2

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?

2

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]
        print(lblClass.name)
        lblClass.expression = "$feature.Ratio"
        lyr.showLabels = True
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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