[GIS] Changing graduated color class values with ArcPy

arcmaparcpycolorsymbology

I am trying to use python to adjust my graduated color symbols' class breaks and labels. The layer is a query layer tied to an SQL DB and I want to be able to execute my python (after the database has been updated) to automatically author a map and have the layer/legend reflect the changes made in the database. I've tried this script but it doesn't work and I get an error I don't recognize.

import arcpy
mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Unsafe_Driving", df)[0]
if lyr.symbologyType == "GRADUATED_COLORS":
    lyr.symbology.valueField = "CSA.DBO.%Unsafe_Driving_1.VARIANCE"
    lyr.symbology.classBreakValues = [-.01, .01, .0866]
    lyr.symbology.classBreakLabels = ["-8.66% to -1.00%", "-.99% to 1.00%","1.01% to 8.66%"] 

arcpy.RefreshActiveView()

Runtime error
Traceback (most recent call last):
File "", line 7, in
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects_base.py", line 87, in _set
return setattr(self._arc_object, attr_name, cval(val))
RuntimeError

The goal here is to have 3 classes:

  1. Lowest value to -1.00%
  2. -.99% to 1.00%
  3. 1.01% to the highest value.

Since the lowest and highest values can change, do I need to use a query to dynamically insert those into the lyr.symbology.classBreakValues?

Best Answer

I just had this exact same issue running ArcGIS 10.2. It turns out that (in 10.2 at least) you cannot set the symbology values via classBreakValues when the valueField is in a joined table. Instead, you need to export everything to a new feature class first, and then the following code should work:

if lyr.symbologyType == "GRADUATED_COLORS":
    lyr.symbology.valueField = "NewField_Variance" #Placeholder name
    lyr.symbology.classBreakValues = [-.01, .01, .0866]
    lyr.symbology.classBreakLabels = ["-8.66% to -1.00%", "-.99% to 1.00%","1.01% to 8.66%"]

I'm about to upgrade to 10.3--hopefully it will be fixed.

Hat tip to this post on the Esri forums by simontp, which in turn references this post by cheffing.

Related Question