Skip to main content
added 489 characters in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43

And to update the label precision on an existing graduated renderer:

layer = iface.activeLayer()
renderer = layer.renderer().clone()
class_method = renderer.classificationMethod()
class_method.setLabelFormat("%1 - %2")
class_method.setLabelPrecision(3)
class_method.setLabelTrimTrailingZeroes(False)
renderer.updateRangeLabels()
layer.setRenderer(renderer)
layer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(layer.id())

And to update the label precision on an existing graduated renderer:

layer = iface.activeLayer()
renderer = layer.renderer().clone()
class_method = renderer.classificationMethod()
class_method.setLabelFormat("%1 - %2")
class_method.setLabelPrecision(3)
class_method.setLabelTrimTrailingZeroes(False)
renderer.updateRangeLabels()
layer.setRenderer(renderer)
layer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(layer.id())
added 3164 characters in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43

WhenSince you set upare manually creating each custom range, instantiating the QgsRendererRange class, you can simply pass your legend label as the 4th argument to the QgsRendererRange class constructor in whatever form you like.

Edit: As for using If you want to use QgsRendererRangeLabelFormat, I may be missing something but as far as I can tell, it just has an effect onyou need to call updateRangeLabels() after setting the legendlabel format settings in the layer symbology dialoge.g.

lyr_name = 'ibra7_regions'
vlayer = QgsProject.instance().mapLayersByName(lyr_name)[0]
target_field = 'SQ_KM'

def apply_graduated_symbology(lyr, fld):
    range_list = []
    color_1 = QColor('#FFD700')
    color_2 = QColor('#FF4500')
    color_3 = QColor('#DC143C')
    symbol_1 = QgsSymbol.defaultSymbol(lyr.geometryType())
    symbol_1.setColor(color_1)
    range_1 = QgsRendererRange(1.00, 150000.00, symbol_1, '1-150000')
    range_list.append(range_1)
    symbol_2 = QgsSymbol.defaultSymbol(lyr.geometryType())
    symbol_2.setColor(color_2)
    range_2 = QgsRendererRange(150000.00, 300000.00, symbol_2, '150000-300000')
    range_list.append(range_2)
    symbol_3 = QgsSymbol.defaultSymbol(lyr.geometryType())
    symbol_3.setColor(color_3)
    range_3 = QgsRendererRange(300000.00, 450000.00, symbol_3, '300000-450000')
    range_list.append(range_3)
    renderer = QgsGraduatedSymbolRenderer(fld, range_list)
    renderer.setMode(QgsGraduatedSymbolRenderer.Custom)
    
    # Set label format and update range labels
    lbl_format = QgsRendererRangeLabelFormat()
    lbl_format.setFormat("%1 - %2")
    lbl_format.setPrecision(2)
    lbl_format.setTrimTrailingZeroes(False)
    renderer.setLabelFormat(lbl_format)
    renderer.updateRangeLabels()
    
    lyr.setRenderer(renderer)
    lyr.triggerRepaint()
    
apply_graduated_symbology(vlayer, target_field)

Make a vector layer active and run this code inTo update the Python Consolelabel precision of an existing graduated renderer:

myLayer = iface.activeLayer()
myFormat = QgsRendererRangeLabelFormat()
myFormat.setFormat("%1 - %2")
myFormat.setPrecision(2)
myFormat.setTrimTrailingZeroes(True)
myRenderer = QgsGraduatedSymbolRenderer()
myRenderer.setLabelFormat(myFormat)
myLayer.setRenderer(myRenderer)
layer = iface.activeLayer()
#print(layer.renderer().labelFormat().precision())
lbl_format = QgsRendererRangeLabelFormat()
lbl_format.setFormat("%1 - %2")
lbl_format.setPrecision(2)
lbl_format.setTrimTrailingZeroes(False)
renderer = layer.renderer().clone()
renderer.setLabelFormat(lbl_format)
renderer.updateRangeLabels()
layer.setRenderer(renderer)
layer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(layer.id())

Setting label precision programmatically is more commonly used when calculating classes automatically.

Note that from QGIS 3.10 onwards, QgsRendererRangeLabelFormat is deprecated. Now we should set the label format and precision in the QgsClassificationMethod subclass e.g. QgsClassificationEqualInterval(). The snippet below creates a graduated renderer has been applied tousing the layerequal interval method, with 7 classes and thea label format settings have been appliedprecision of 1:

enter image description here

layer = iface.activeLayer()

default_style = QgsStyle().defaultStyle()

# Use any existing color ramp e.g.
# 'Greens', 'Purples', 'Cividis' etc
# or create a custom color ramp
# Here we are using the purple/blue/green ramp...
color_ramp = default_style.colorRamp('PuBuGn')

renderer = QgsGraduatedSymbolRenderer()
renderer.setClassAttribute('SQ_KM')

# using equal interval method...
classification_method = QgsClassificationEqualInterval()
classification_method.setLabelPrecision(1)
classification_method.setLabelTrimTrailingZeroes(True)
renderer.setClassificationMethod(classification_method)

# creating 7 classes...
renderer.updateClasses(layer, 7)
renderer.updateColorRamp(color_ramp)

layer.setRenderer(renderer)
layer.triggerRepaint()

# optionally expand the layer legend...
lyr_tree_root = iface.layerTreeView().layerTreeModel().rootGroup()
lyr_node = lyr_tree_root.findLayer(layer.id())
if not lyr_node.isExpanded():
    lyr_node.setExpanded(True)

When you set up each custom range, instantiating the QgsRendererRange class, you can simply pass your legend label as the 4th argument in whatever form you like.

Edit: As for using QgsRendererRangeLabelFormat, I may be missing something but as far as I can tell, it just has an effect on the legend format settings in the layer symbology dialog.

Make a vector layer active and run this code in the Python Console:

myLayer = iface.activeLayer()
myFormat = QgsRendererRangeLabelFormat()
myFormat.setFormat("%1 - %2")
myFormat.setPrecision(2)
myFormat.setTrimTrailingZeroes(True)
myRenderer = QgsGraduatedSymbolRenderer()
myRenderer.setLabelFormat(myFormat)
myLayer.setRenderer(myRenderer)

The graduated renderer has been applied to the layer and the label format settings have been applied:

enter image description here

Since you are manually creating each custom range, you can simply pass your legend label as the 4th argument to the QgsRendererRange class constructor in whatever form you like.

If you want to use QgsRendererRangeLabelFormat, you need to call updateRangeLabels() after setting the label format e.g.

lyr_name = 'ibra7_regions'
vlayer = QgsProject.instance().mapLayersByName(lyr_name)[0]
target_field = 'SQ_KM'

def apply_graduated_symbology(lyr, fld):
    range_list = []
    color_1 = QColor('#FFD700')
    color_2 = QColor('#FF4500')
    color_3 = QColor('#DC143C')
    symbol_1 = QgsSymbol.defaultSymbol(lyr.geometryType())
    symbol_1.setColor(color_1)
    range_1 = QgsRendererRange(1.00, 150000.00, symbol_1, '1-150000')
    range_list.append(range_1)
    symbol_2 = QgsSymbol.defaultSymbol(lyr.geometryType())
    symbol_2.setColor(color_2)
    range_2 = QgsRendererRange(150000.00, 300000.00, symbol_2, '150000-300000')
    range_list.append(range_2)
    symbol_3 = QgsSymbol.defaultSymbol(lyr.geometryType())
    symbol_3.setColor(color_3)
    range_3 = QgsRendererRange(300000.00, 450000.00, symbol_3, '300000-450000')
    range_list.append(range_3)
    renderer = QgsGraduatedSymbolRenderer(fld, range_list)
    renderer.setMode(QgsGraduatedSymbolRenderer.Custom)
    
    # Set label format and update range labels
    lbl_format = QgsRendererRangeLabelFormat()
    lbl_format.setFormat("%1 - %2")
    lbl_format.setPrecision(2)
    lbl_format.setTrimTrailingZeroes(False)
    renderer.setLabelFormat(lbl_format)
    renderer.updateRangeLabels()
    
    lyr.setRenderer(renderer)
    lyr.triggerRepaint()
    
apply_graduated_symbology(vlayer, target_field)

To update the label precision of an existing graduated renderer:

layer = iface.activeLayer()
#print(layer.renderer().labelFormat().precision())
lbl_format = QgsRendererRangeLabelFormat()
lbl_format.setFormat("%1 - %2")
lbl_format.setPrecision(2)
lbl_format.setTrimTrailingZeroes(False)
renderer = layer.renderer().clone()
renderer.setLabelFormat(lbl_format)
renderer.updateRangeLabels()
layer.setRenderer(renderer)
layer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(layer.id())

Setting label precision programmatically is more commonly used when calculating classes automatically.

Note that from QGIS 3.10 onwards, QgsRendererRangeLabelFormat is deprecated. Now we should set the label format and precision in the QgsClassificationMethod subclass e.g. QgsClassificationEqualInterval(). The snippet below creates a graduated renderer using the equal interval method, with 7 classes and a label precision of 1:

layer = iface.activeLayer()

default_style = QgsStyle().defaultStyle()

# Use any existing color ramp e.g.
# 'Greens', 'Purples', 'Cividis' etc
# or create a custom color ramp
# Here we are using the purple/blue/green ramp...
color_ramp = default_style.colorRamp('PuBuGn')

renderer = QgsGraduatedSymbolRenderer()
renderer.setClassAttribute('SQ_KM')

# using equal interval method...
classification_method = QgsClassificationEqualInterval()
classification_method.setLabelPrecision(1)
classification_method.setLabelTrimTrailingZeroes(True)
renderer.setClassificationMethod(classification_method)

# creating 7 classes...
renderer.updateClasses(layer, 7)
renderer.updateColorRamp(color_ramp)

layer.setRenderer(renderer)
layer.triggerRepaint()

# optionally expand the layer legend...
lyr_tree_root = iface.layerTreeView().layerTreeModel().rootGroup()
lyr_node = lyr_tree_root.findLayer(layer.id())
if not lyr_node.isExpanded():
    lyr_node.setExpanded(True)
added 2 characters in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43

When you set up each custom range, instantiating the QgsRendererRange class, you can simply pass your legend label as the 4th argument in whatever form you like.

For example:

myRange1 = QgsRendererRange(0.00, 3.00, mySymbol1, '0.00-3.00')
...
myRange2 = QgsRendererRange(3.10, 6.00, mySymbol2, '3.10-6.00')

etc.

In practice the appropriate precision, both for setting and interpreting each range, would probably depend on factors like the actual precision of the numeric attribute field you are using, the size of each value range etc.

Since that argument is just a string which will be used to label each range in the table of contents panel/ legend you can also include units to provide context e.g. '0-3 patients' or use very simple labels to broadly interpret each value range such as 'Low', 'Medium', 'High' etc.

By the way, you may find you also need to call: join_layer.triggerRepaint() after setting the renderer to apply the new layer symbology in the map canvas.

Edit: As for using QgsRendererRangeLabelFormatQgsRendererRangeLabelFormat, I may be missing something but as far as I can tell, it just has an effect on the legend format settings in the layer symbology dialog.

Make a vector layer active and run this code in the Python Console:

myLayer = iface.activeLayer()
myFormat = QgsRendererRangeLabelFormat()
myFormat.setFormat("%1 - %2")
myFormat.setPrecision(2)
myFormat.setTrimTrailingZeroes(True)
myRenderer = QgsGraduatedSymbolRenderer()
myRenderer.setLabelFormat(myFormat)
myLayer.setRenderer(myRenderer)

The graduated renderer has been applied to the layer and the label format settings have been applied:

enter image description here

When you set up each custom range, instantiating the QgsRendererRange class, you can simply pass your legend label as the 4th argument in whatever form you like.

For example:

myRange1 = QgsRendererRange(0.00, 3.00, mySymbol1, '0.00-3.00')
...
myRange2 = QgsRendererRange(3.10, 6.00, mySymbol2, '3.10-6.00')

etc.

In practice the appropriate precision, both for setting and interpreting each range, would probably depend on factors like the actual precision of the numeric attribute field you are using, the size of each value range etc.

Since that argument is just a string which will be used to label each range in the table of contents panel/ legend you can also include units to provide context e.g. '0-3 patients' or use very simple labels to broadly interpret each value range such as 'Low', 'Medium', 'High' etc.

By the way, you may find you also need to call: join_layer.triggerRepaint() after setting the renderer to apply the new layer symbology in the map canvas.

Edit: As for using QgsRendererRangeLabelFormat, I may be missing something but as far as I can tell, it just has an effect on the legend format settings in the layer symbology dialog.

Make a vector layer active and run this code in the Python Console:

myLayer = iface.activeLayer()
myFormat = QgsRendererRangeLabelFormat()
myFormat.setFormat("%1 - %2")
myFormat.setPrecision(2)
myFormat.setTrimTrailingZeroes(True)
myRenderer = QgsGraduatedSymbolRenderer()
myRenderer.setLabelFormat(myFormat)
myLayer.setRenderer(myRenderer)

The graduated renderer has been applied to the layer and the label format settings have been applied:

enter image description here

When you set up each custom range, instantiating the QgsRendererRange class, you can simply pass your legend label as the 4th argument in whatever form you like.

For example:

myRange1 = QgsRendererRange(0.00, 3.00, mySymbol1, '0.00-3.00')
...
myRange2 = QgsRendererRange(3.10, 6.00, mySymbol2, '3.10-6.00')

etc.

In practice the appropriate precision, both for setting and interpreting each range, would probably depend on factors like the actual precision of the numeric attribute field you are using, the size of each value range etc.

Since that argument is just a string which will be used to label each range in the table of contents panel/ legend you can also include units to provide context e.g. '0-3 patients' or use very simple labels to broadly interpret each value range such as 'Low', 'Medium', 'High' etc.

By the way, you may find you also need to call: join_layer.triggerRepaint() after setting the renderer to apply the new layer symbology in the map canvas.

Edit: As for using QgsRendererRangeLabelFormat, I may be missing something but as far as I can tell, it just has an effect on the legend format settings in the layer symbology dialog.

Make a vector layer active and run this code in the Python Console:

myLayer = iface.activeLayer()
myFormat = QgsRendererRangeLabelFormat()
myFormat.setFormat("%1 - %2")
myFormat.setPrecision(2)
myFormat.setTrimTrailingZeroes(True)
myRenderer = QgsGraduatedSymbolRenderer()
myRenderer.setLabelFormat(myFormat)
myLayer.setRenderer(myRenderer)

The graduated renderer has been applied to the layer and the label format settings have been applied:

enter image description here

added 778 characters in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43
Loading
edited body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43
Loading
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43
Loading