I want to plot each rgb value for its respective feature of the layer via pyqgis. Atm I struggle to properly implement it for each layer individually. With my current code below, all features get the last color code in row no.3. and therefore they all have the same color instead of every element ist individual one.

I use QGIS 2.8.8 Wien and therefore proposed solutions for QGIS 3 do not work properly.

b = iface.activeLayer()

symbols = b.rendererV2().symbols()
symbol = symbols[0]
for f in b.getFeatures():
    expr = f['rgb']
    express = "color_rgb("+str(expr) +")"
    symbol.symbolLayer(0).setDataDefinedProperty('color', express)


enter image description here enter image description here

  • 1
    Please consider updating to the latest LTR. I doubt there are people around who could help you, who still have access to 2.8.
    – Erik
    Commented Aug 18, 2020 at 13:27
  • You are 100% right but unfortunately the code was preproduced by another guy who never updated to newer versions. I would have never implemented it into 2.8 myself...
    – Tobias
    Commented Aug 18, 2020 at 13:29

1 Answer 1


Here is a code i adapted for you, it worked on my qgis 2.18.28:

b = iface.activeLayer()

fni = b.fieldNameIndex("rgb")
unique_values = b.uniqueValues(fni)

# fill categories
categories = []
for unique_value in unique_values:
    red, blue, green = unique_value.split(",")
    # initialize the default symbol for this geometry type
    symbol = QgsSymbolV2.defaultSymbol(b.geometryType())
    # configure a symbol layer
    layer_style = {}
    layer_style['color'] = '%d, %d, %d' % (red, blue, green)
    layer_style['outline'] = '#000000'
    symbol_layer = QgsSimpleFillSymbolLayerV2.create(layer_style)

    # replace default symbol layer with the configured one
    if symbol_layer is not None:
        symbol.changeSymbolLayer(0, symbol_layer)

    # create renderer object
    category = QgsRendererCategoryV2(unique_value, symbol, str(unique_value))
    # entry for the list of category items

# create renderer object
renderer = QgsCategorizedSymbolRendererV2('$id', categories)

# assign the created renderer to the layer
if renderer is not None:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.