[GIS] How to change default symbol in Categorized symbol renderer (python console) or use different icons as vector layer symbols

pyqgispython-2.7qgisrenderingsymbology

I have a point shapefile in QGIS 2.10 and I want to display one of its fields attributes based on its unique values (I always know that it has 4 unique values and their values so I do not care for the moment to extract them) I just want points to be displayed with different symbols depending on its value for the specified field.

The categorized symbol renderer styles features based on unique values in the layer's attribute table (QGIS python programming cookbook, chapter 5, page: 145). Looking at the API for QgsSymbolV2 I think that maybe I have to use the public member fuction changeSymbolLayer instead of

QgsSymbolV2.defaultSymbol(lyr.geometryType())

but I get very confused on how to follow the API.

Then, there is the option to use icons as vector layer symbols (QGIS python programming cookbook, chapter 5, page: 140) but I am not sure in how to achieve to have different icons for different categories.

Best Answer

I've only used the Graduated Symbol renderer; however, the categorized symbol marker seems to be quite similar. I was also seeking what you were and here was my solution:

# replace
QgsSymbolV2.defaultSymbol(layer.geometryType())

# with
sym = QgsMarkerSymbolV2.createSimple({'name' : 'triangle', 'color' : 'red'})

'triangle' in place of whatever marker you would like displayed and 'red' in place of whatever color you would like.

That just fixed my problem.