Skip to main content
added 1 character in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43

It's absolutely possible! Just use:

renderer.updateColorRamp(QgsRandomColorRamp())

I modified your script only very slightly:

layer = iface.activeLayer()

field_name = 'zona_manejo'
field_index = layer.fields().indexFromName(field_name)
unique_values = layer.uniqueValues(field_index)

category_list = []
for value in unique_values:
    symbol = QgsSymbol.defaultSymbol(layer.geometryType())
    category = QgsRendererCategory(value, symbol, str(value))
    category_list.append(category)

renderer = QgsCategorizedSymbolRenderer(field_name, category_list)
renderer.updateColorRamp(QgsRandomColorRamp())
layer.setRenderer(renderer)
layer.triggerRepaint()

See results on a test layer below (I changed only the field name to 'reg_code_8'.

Before:

enter image description here

After:

enter image description here

Incidently, because random colors are the default for the categorized renderer, you don't even need to update the color ramp. You can just use:

layer = iface.activeLayer()

field_name = 'zona_manejo'
field_index = layer.fields().indexFromName(field_name)
unique_values = layer.uniqueValues(field_index)

category_list = []
for value in unique_values:
    symbol = QgsSymbol.defaultSymbol(layer.geometryType())
    category = QgsRendererCategory(value, symbol, str(value))
    category_list.append(category)

renderer = QgsCategorizedSymbolRenderer(field_name, category_list)
layer.setRenderer(renderer)
layer.triggerRepaint()

It will give you random colors. However, interestingly I foundnotice that the resulting palette is consistently a bit different (colors are not as 'vibrant'). An example:

enter image description here

It's absolutely possible! Just use:

renderer.updateColorRamp(QgsRandomColorRamp())

I modified your script only very slightly:

layer = iface.activeLayer()

field_name = 'zona_manejo'
field_index = layer.fields().indexFromName(field_name)
unique_values = layer.uniqueValues(field_index)

category_list = []
for value in unique_values:
    symbol = QgsSymbol.defaultSymbol(layer.geometryType())
    category = QgsRendererCategory(value, symbol, str(value))
    category_list.append(category)

renderer = QgsCategorizedSymbolRenderer(field_name, category_list)
renderer.updateColorRamp(QgsRandomColorRamp())
layer.setRenderer(renderer)
layer.triggerRepaint()

See results on a test layer below (I changed only the field name to 'reg_code_8'.

Before:

enter image description here

After:

enter image description here

Incidently, because random colors are the default for the categorized renderer, you don't even need to update the color ramp. You can just use:

layer = iface.activeLayer()

field_name = 'zona_manejo'
field_index = layer.fields().indexFromName(field_name)
unique_values = layer.uniqueValues(field_index)

category_list = []
for value in unique_values:
    symbol = QgsSymbol.defaultSymbol(layer.geometryType())
    category = QgsRendererCategory(value, symbol, str(value))
    category_list.append(category)

renderer = QgsCategorizedSymbolRenderer(field_name, category_list)
layer.setRenderer(renderer)
layer.triggerRepaint()

It will give you random colors. However, interestingly I found that the resulting palette is consistently a bit different (colors are not as 'vibrant'). An example:

enter image description here

It's absolutely possible! Just use:

renderer.updateColorRamp(QgsRandomColorRamp())

I modified your script only very slightly:

layer = iface.activeLayer()

field_name = 'zona_manejo'
field_index = layer.fields().indexFromName(field_name)
unique_values = layer.uniqueValues(field_index)

category_list = []
for value in unique_values:
    symbol = QgsSymbol.defaultSymbol(layer.geometryType())
    category = QgsRendererCategory(value, symbol, str(value))
    category_list.append(category)

renderer = QgsCategorizedSymbolRenderer(field_name, category_list)
renderer.updateColorRamp(QgsRandomColorRamp())
layer.setRenderer(renderer)
layer.triggerRepaint()

See results on a test layer below (I changed only the field name to 'reg_code_8'.

Before:

enter image description here

After:

enter image description here

Incidently, because random colors are the default for the categorized renderer, you don't even need to update the color ramp. You can just use:

layer = iface.activeLayer()

field_name = 'zona_manejo'
field_index = layer.fields().indexFromName(field_name)
unique_values = layer.uniqueValues(field_index)

category_list = []
for value in unique_values:
    symbol = QgsSymbol.defaultSymbol(layer.geometryType())
    category = QgsRendererCategory(value, symbol, str(value))
    category_list.append(category)

renderer = QgsCategorizedSymbolRenderer(field_name, category_list)
layer.setRenderer(renderer)
layer.triggerRepaint()

It will give you random colors. However, interestingly I notice that the resulting palette is consistently a bit different (colors are not as 'vibrant'). An example:

enter image description here

Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43

It's absolutely possible! Just use:

renderer.updateColorRamp(QgsRandomColorRamp())

I modified your script only very slightly:

layer = iface.activeLayer()

field_name = 'zona_manejo'
field_index = layer.fields().indexFromName(field_name)
unique_values = layer.uniqueValues(field_index)

category_list = []
for value in unique_values:
    symbol = QgsSymbol.defaultSymbol(layer.geometryType())
    category = QgsRendererCategory(value, symbol, str(value))
    category_list.append(category)

renderer = QgsCategorizedSymbolRenderer(field_name, category_list)
renderer.updateColorRamp(QgsRandomColorRamp())
layer.setRenderer(renderer)
layer.triggerRepaint()

See results on a test layer below (I changed only the field name to 'reg_code_8'.

Before:

enter image description here

After:

enter image description here

Incidently, because random colors are the default for the categorized renderer, you don't even need to update the color ramp. You can just use:

layer = iface.activeLayer()

field_name = 'zona_manejo'
field_index = layer.fields().indexFromName(field_name)
unique_values = layer.uniqueValues(field_index)

category_list = []
for value in unique_values:
    symbol = QgsSymbol.defaultSymbol(layer.geometryType())
    category = QgsRendererCategory(value, symbol, str(value))
    category_list.append(category)

renderer = QgsCategorizedSymbolRenderer(field_name, category_list)
layer.setRenderer(renderer)
layer.triggerRepaint()

It will give you random colors. However, interestingly I found that the resulting palette is consistently a bit different (colors are not as 'vibrant'). An example:

enter image description here