I'm trying to set the color of a vectorlayer feature depending on the feature attributes with PyQGIS. How do you acces the attributes of a vectorlayer feature?
Right click on the layer in the table of contents > properties > style tab > there you have options to style as a single symbol, using categories, using graduated colours, and rules.
You then have the option to choose the field containing the values to be displayed.
See the QGIS manual for more information: http://docs.qgis.org/user_guide/html/en/
To set the color of a vector layer feature depending on the feature attributes with PyQGIS is necessary to change the QgsSingleSymbolRendererV2 default object in a, for example, QgsGraduatedSymbolRendererV2 object. To test this I created the 'random' field name in a Polygon Vector Layer and this field was filled with aleatory values between 1 and 24. These values were grouped in three arbitrary classes in a tuple for being used in the following code at the Python Console of QGIS:
Before running the code at Python Console:
After running the code at the Python Console:
The renderer produced was of the QgsGraduatedSymbolRendererV2 type.