I have worked out how to change the color of a polygon using pyQGIS but cannot change the stroke color or line with based on the questions detailed here and here. My example code below that works to make the polygon transparent fill but not for the blue border and desired linewidth.
# Attach core modules
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from qgis.core import * # attach main QGIS library
from qgis.utils import * # attach main python library
import os # attach operating system library
import processing
# Set the working directory - recommended to use unix forward slash for path
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wd = "C:/TEST" # Set work directory
os.chdir(wd) # Change the directory
os.getcwd() # Confirm change
# Set a variable for the current project instance
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Prj = QgsProject.instance() # Object for current project
# Save the project to this file name
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pnm = "200924_Test.qgs" # Project file name
pnm = wd + "/" + pnm # Concat. with path
Prj.write(pnm) # Save the project
# Create an array [] object with the polygon vertices for MRE model area
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vrtcs = []
vrtcs.append(QgsPointXY(541600,6504400))
vrtcs.append(QgsPointXY(541600,6505100))
vrtcs.append(QgsPointXY(542160,6505100))
vrtcs.append(QgsPointXY(542160,6504400))
# Create a polygon from the vertices
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ply_01 = QgsGeometry.fromPolygonXY([vrtcs])
# Create a feature object then append the polygon into the feature
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ftr = QgsFeature()
ftr.setGeometry(ply_01)
print(ftr.geometry())
# Create a layer for the feature, in the desired CRS 28351 GDA94/MGA 51
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lyr = QgsVectorLayer('Polygon?crs=epsg:28351', '200924_MRE_Area',"memory")
Prj.addMapLayers([lyr])
# Make the layer editable, add the feature with the polygon and save the layer
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lyr.startEditing()
lyr.addFeature(ftr)
lyr.commitChanges()
# Save as a shapefile
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fl_ou = 'Test'
Fl_ou = wd + '/' + Fl_ou
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "ESRI Shapefile"
# Change the fill, stroke colour and line width of the the polygon
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rndr = lyr.renderer() # object to change rendering
rndr.dump() # report current rendering
sym = rndr.symbol() # object to change the symbology of layer
syms = sym[0] # use first layer in QGIS tree
syms.setColor(QColor("transparent")) # remove the fill colour
syms.symbolLayer(0).setStrokeColor(QColor("blue")) # change the stroke colour (Fails)
syms.symbolLayer(0).setWidth(3) #
lyr.triggerRepaint() # Refresh canvas
Additionally, the questions (linked) also suggest the following lines of code should be included at the end of this process - I would like to understand why they are required
iface.legendInterface().refreshLayerSymbology(layer)
iface.mapCanvas().refresh()