''' QgsClassificationMethod QgsClassificationMethodRegistry.addMethod(QgsClassificationMethod) from qgis.core import QgsClassificationMethod, QgsApplication from abc import ABC, abstractmethod class QgsClassificationHeadTail(QgsClassificationMethod): def name(self): return 'Head Tail method' def id(self): return 'HeadTail' def icon(self): return QgsApplication.getThemeIcon("classification_methods/mClassificationHeadTail.svg") from qgis.core import QgsClassificationMethod class QgsClassificationMyCustomMethod(QgsClassificationMethod): def __init__(self): super().__init__(self) def name(self): return 'my custom method' class QgsClassificationMyCustomMethod(QgsClassificationMethod): def __init__(self, parent=None): super().__init__(parent) ''' from method_headtail import headtail # Set layer name and desired paremeters layer_name = 'NUTS_RG_10M_2021_3035_LEVL_3_WITH_POP NUTS_RG_10M_2021_3035_LEVL_3.geojson' ramp_name = 'Reds' value_field = 'POP' num_classes = 5 classification_method = QgsClassificationJenks() #You can use any of these classification method classes: #QgsClassificationQuantile() #QgsClassificationEqualInterval() #QgsClassificationJenks() #QgsClassificationPrettyBreaks() #QgsClassificationLogarithmic() #QgsClassificationStandardDeviation() def createFillSymbol(): symbol = QgsFillSymbol.createSimple({ "color": "100,150,50", 'joinstyle': 'bevel', 'outline_color': '35,35,35,255', 'outline_style': 'solid', 'outline_width': '0.26', 'style': 'solid' }) return symbol layer = QgsProject().instance().mapLayersByName(layer_name)[0] values = QgsVectorLayerUtils.getValues(layer, 'POP') values = values[0] valuesNotEmpty = [v for v in values if v != QVariant()] # change format settings as necessary format = QgsRendererRangeLabelFormat() format.setFormat("%1 - %2") format.setPrecision(2) format.setTrimTrailingZeroes(True) default_style = QgsStyle().defaultStyle() color_ramp = default_style.colorRamp(ramp_name) renderer = QgsGraduatedSymbolRenderer() symbol = createFillSymbol() # {'border_width_map_unit_scale': '3x:0,0,0,0,0,0', 'color': '164,113,88,255', 'joinstyle': 'bevel', 'offset': '0,0', 'offset_map_unit_scale': '3x:0,0,0,0,0,0', 'offset_unit': 'MM', 'outline_color': '35,35,35,255', 'outline_style': 'solid', 'outline_width': '0.26', 'outline_width_unit': 'MM', 'style': 'solid'} renderer.setSourceSymbol(symbol.clone()) symbol.setColor(QColor(255, 0, 0)) values1 = [min(valuesNotEmpty)] + headtail(valuesNotEmpty, {"nb": 5, "minmax": False, "precision": None}) + [max(valuesNotEmpty)] print(values1) print(headtail(valuesNotEmpty, {"nb": 5, "minmax": True})) #print(values1) # [values1[i:i+2] for i in range(num_classes)] r = QgsColorBrewerColorRamp(ramp_name, 5) for i in range(5): mycolor = r.color(QgsColorBrewerColorRamp().value(i)) symClone = symbol.clone() symClone.setColor(mycolor) myrange = QgsRendererRange(*values1[i:i+2], symClone, ' - '.join([str(j) for j in values1[i:i+2]]), True) renderer.addClassRange(myrange) renderer.setClassAttribute(value_field) renderer.setClassificationMethod(classification_method) renderer.setLabelFormat(format) # renderer.updateClasses(layer, num_classes) renderer.updateColorRamp(color_ramp) layer.setRenderer(renderer) layer.triggerRepaint() newRenderer = QgsRuleBasedRenderer.convertFromRenderer(renderer) symbolEmpty = createFillSymbol() symbolEmpty.setColor(QColor(255, 255, 255)) ruleEmpty = QgsRuleBasedRenderer.Rule(symbolEmpty, filterExp='ELSE', label='Vide', elseRule=True) newRenderer.rootRule().insertChild(0, ruleEmpty) layer.setRenderer(newRenderer) layer.triggerRepaint()