I'm fetching a polygon layer from postgres using the following:
uri = QgsDataSourceURI()
uri.setConnection("localhost", "5432", "mydb", "postgres", "xxx")
uri.setDataSource("public", "my_table", "geom")
lyr = QgsVectorLayer(uri.uri(), "Blocks", "postgres")
QgsMapLayerRegistry.instance().addMapLayer(lyr)
This layer has an attribute field named blocks that has 6 values: 'A-6', 'B-4', 'G-5', 'D-2', 'P-7', 'C-3'.
I want to apply the color green to polygons where the attribute 'block' value is 'B-4' and red color to the other polygons in the layer that have a different value in the 'block' attribute.
I've seen code to apply graduated symbols using PyQGIS but I want categorized symbols.
How can I do this?