Page 20 sur 23
Classification
Intervalles égaux
# Set layer name and desired params ramp_name = 'Spectral' # ramp_name = 'PuRd' value_field = 'Population (2024)' num_classes = 10 classification_method = QgsClassificationEqualInterval() layer = QgsProject().instance().mapLayersByName('Countries')[0] format = QgsRendererRangeLabelFormat() format.setTrimTrailingZeroes(True) default_style = QgsStyle().defaultStyle() color_ramp = default_style.colorRamp(ramp_name) renderer = QgsGraduatedSymbolRenderer() 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()