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:
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)