Feature layer legend label format with arcpy

200
3
Jump to solution
01-06-2025 08:03 AM
ErzsikeTerezsi
Emerging Contributor

How can I round to 2 decimal places the feature class legend label in arcgis pro with python code, if it is divided into 5 classes using natural break method? The value is rounded up with my code, but still 6 decimal places are displayed. How to do it from code, because it would be part of a pdf printer process and it would be very important that the symbol explanation is easy to read. I can only do this by manual adjustment (Advanced symbolgy options/Rounding). 

original label:          0.234567 - 0.41277

rounded by script: 0.230000 - 0.410000

I would like this:      0.23 - 0.41


p = arcpy.mp.ArcGISProject('CURRENT')
m = p.listMaps('MAP')[0]
l = m.listLayers("NDVI_mean")[0]
sym = l.symbology

if sym.renderer.type == "GraduatedColorsRenderer":
    if sym.renderer.classificationMethod == "NaturalBreaks":
        breaks = sym.renderer.classBreaks
        for tor in breaks:
            rounded_break_values = round(float(tor.upperBound), 2)
            tor.upperBound = rounded_break_values
l.symbology = sym

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor

I personally have found trying to change the labels in symbols always a bit flaky through direct manipulation of arcpy classes, but may be that's just me...

Anyway an alternative method is to access the CIM object module for fine tuning layer properties, this includes symbology.  As you say in your question you can easily change the decimal places through Advanced symbology options in the UI, so lets replicate that!

 

import arcpy

# Get handle on layer in first map
p = arcpy.mp.ArcGISProject('CURRENT')
m = p.listMaps('MAP')[0]
l = m.listLayers("fc_AreasMerged")[0]
sym = l.symbology

# Change rounding value using CIM
lcim = l.getDefinition('V2')
lcim.renderer.numberFormat.roundingValue = 2
l.setDefinition(lcim)

# Update/refresh renderer
sym = l.symbology
sym.updateRenderer ('GraduatedColorsRenderer')
l.symbology = sym

 

 

If you want to get into manipulating CIM, best bit advice I can give is get hold of the CIM viewer AddIn, this allows you to see the CIM for a layer selected in the Contents Pane. I couldn't live without it!

View solution in original post

Tags (3)
3 Replies
DanPatterson
MVP Esteemed Contributor
a = 1.2345678
float(f"{round(a,2):.2f}")
1.23

might work unless the upperBound etc is handled after the number formatting


... sort of retired...
0 Kudos
DuncanHornby
MVP Notable Contributor

I personally have found trying to change the labels in symbols always a bit flaky through direct manipulation of arcpy classes, but may be that's just me...

Anyway an alternative method is to access the CIM object module for fine tuning layer properties, this includes symbology.  As you say in your question you can easily change the decimal places through Advanced symbology options in the UI, so lets replicate that!

 

import arcpy

# Get handle on layer in first map
p = arcpy.mp.ArcGISProject('CURRENT')
m = p.listMaps('MAP')[0]
l = m.listLayers("fc_AreasMerged")[0]
sym = l.symbology

# Change rounding value using CIM
lcim = l.getDefinition('V2')
lcim.renderer.numberFormat.roundingValue = 2
l.setDefinition(lcim)

# Update/refresh renderer
sym = l.symbology
sym.updateRenderer ('GraduatedColorsRenderer')
l.symbology = sym

 

 

If you want to get into manipulating CIM, best bit advice I can give is get hold of the CIM viewer AddIn, this allows you to see the CIM for a layer selected in the Contents Pane. I couldn't live without it!

Tags (3)
ErzsikeTerezsi
Emerging Contributor

Thank you! It is work fine.

0 Kudos