I am using PyQGIS within my work and I currently have a script that uses QgsGraduatedSymbolRenderer to symbolise my data. Previously, I have used a script that symbolises my data using CategorizedSymbolRenderer (see script below). However, this prints out too many values on my exported map, so I have decided to use Graduated instead, so that I can use min/max values to classify my data.
default_style = QgsStyle().defaultStyle()
color_ramp = default_style.colorRamp('Spectral') # Spectral color ramp
color_ramp.invert()
layer = QgsProject().instance().mapLayersByName('Intensity')[0]
exp_string = '\'Intensity: \' || "Intensity" || \'\nAffected Population: \' || sum("LAC_Popu_1", "Intensity")'
unique_values = []
exp = QgsExpression(exp_string)
#print(exp.isValid())
context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.globalScope())
context.appendScope(QgsExpressionContextUtils.projectScope(QgsProject.instance()))
context.appendScope(QgsExpressionContextUtils.atlasScope(None))
context.appendScope(QgsExpressionContextUtils.layerScope(layer))
exp.prepare(context)
for feat in layer.getFeatures():
context.setFeature(feat)
val = exp.evaluate(context)
if val not in unique_values:
unique_values.append(val)
# print(unique_values)
categories = []
for value in sorted(unique_values):
symbol = QgsSymbol.defaultSymbol(layer.geometryType())
category = QgsRendererCategory(value, symbol, str(value))
categories.append(category)
renderer = QgsCategorizedSymbolRenderer(exp_string, categories)
renderer.updateColorRamp(color_ramp)
layer.setRenderer(renderer)
layer.setOpacity(0.55)
layer.triggerRepaint()
My script that I have created so far retrieves intensity data from a field called 'Intensity' within my vlayer and uses Graduated Symbology to say if the intensity is between 1.0 - 1.99 for example, then a blue colour is given. This script runs perfectly fine to produce graduated intensity values with corresponding colour. However, I am struggling to get the 'Affected Population' factor within my label, like previously shown, where a value is underneath each intensity. The affected population field will have no involvement as to how the graduated symbology is laid out, it is just extra information for each intensity. This script is as follows:
Intensity_rules = (
('MMI Intensity 1.0 - 1.99', 0.0, 1.99, '#2b83ba'),
('MMI Intensity 2.0 - 2.99', 2.0, 2.99, '#71b4ae'),
('MMI Intensity 3.0 - 3.99', 3.0, 3.99, '#abdda4'),
('MMI Intensity 4.0 - 4.99', 4.0, 4.99, '#d4eeb1'),
('MMI Intensity 5.0 - 5.99', 5.0, 5.99, '#ffffbf'),
('MMI Intensity 6.0 - 6.99', 6.0, 6.99, '#fed58e'),
('MMI Intensity 7.0 - 7.99', 7.0, 7.99, '#fdae61'),
('MMI Intensity 8.0 - 8.99', 8.0, 8.99, '#ee7144'),
('MMI Intensity 9.0 - 9.99', 9.0, 9.99, '#d7191c'),
('MMI Intensity 10.0 - 10.99', 10.0, 10.99, '#d7191c'),
)
# create a category for each item in intensity
ranges = []
for label, lower, upper, color in Intensity_rules:
symbol = QgsSymbol.defaultSymbol(vlayer.geometryType())
symbol.setColor(QColor(color))
rng = QgsRendererRange(lower, upper, symbol, label)
ranges.append(rng)
#create the renderer and assign it to layer
expression = '"Intensity" ' #field name '\'Intensity: \' || "Intensity" || \'\nAffected Population: \' || sum("LAC_Popu_1", "Intensity")'
renderer = QgsGraduatedSymbolRenderer(expression, ranges)
vlayer.setRenderer(renderer)
vlayer.setOpacity(0.70)
vlayer.triggerRepaint()
I run zonal statistics on the intensity shapefile layer, along with a population raster, to produce an affected population column called "LAC_Popu_1". Is there a way I can add this column of produced statistics underneath each line of intensity like in this first script/photo where I used Categorized symbology e.g. MMI Intensity 1.0 - 1.99: 'Blue colour' Affected Population: 545432