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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 14, 2020 at 5:09

1 Answer 1

3

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
answered Jul 14, 2020 at 5:29
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.