Skip to main content
3 of 6
added 778 characters in body
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 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

Ben W
  • 23.1k
  • 3
  • 19
  • 43