I have this small code to change a feature layer's symbology. I'm referencing this help doc.
When I try to change the break count I get a RuntimeError. And when I try to identify the classification field I don't seem to find it. Using a print statement, I identify other properties but not the field. The only thing i can think of is possibly because this field is set to a custom expression:
for lyr in m.listLayers():
if lyr.isFeatureLayer:
sym = lyr.symbology
if hasattr(sym, 'renderer'):
#type of renderer
if sym.renderer.type == 'GraduatedColorsRenderer':
#sym.renderer.breakCount = 4 -RuntimeError: Invalid breakCount 4
print('break count: {}\nmethod: {}\nfield: {}'.format(sym.renderer.breakCount,
sym.renderer.classificationMethod,
sym.renderer.classificationField))
prints:
break count: 5
method: ManualInterval
field:
Solved! Go to Solution.
The classification field has to be a number in order for the renderer to recognize it. It validated both with float and double. It will not work with text fields. I'm in contact with an ESRI analyst who is looking into why that is.
Looking into this further, i realized this classification field I'm trying to identify is a from a joined table. If I use a field not from the joined table it works fine.
The 'USER_PATIENT_ZIP' field is not from a joined table, and it validates. But, the field I want is from a joined table, and it's invalid somehow.
if hasattr(sym, 'renderer'):
if sym.renderer.type == 'GraduatedColorsRenderer':
sym.renderer.classificationField = 'USER_PATIENT_ZIP'
print('Zip OK')
sym.renderer.classificationField = 'Blckgrp2020_iCareCountOfVaccinated_ALL_FirstDose_Black_PercentVa'
print('Dose OK')
### Zip OK
### ---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
In [193]:
Line 5: sym.renderer.classificationField = 'Blckgrp2020_iCareCountOfVaccinated_ALL_FirstDose_Black_PercentVa'
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py, in _set:
Line 109: return setattr(self._arc_object, attr_name, cval(val))
RuntimeError: Invalid classificationField : Blckgrp2020_iCareCountOfVaccinated_ALL_FirstDose_Black_PercentVa
---------------------------------------------------------------------------
The classification field has to be a number in order for the renderer to recognize it. It validated both with float and double. It will not work with text fields. I'm in contact with an ESRI analyst who is looking into why that is.