I'm developing a script which includes a function to style raster layers (based on Setting 'classificationMode = Continuous' to a raster layer using PyQGIS? and Qgis: style a discrete raster). The script executes without any errors and colours the layer correctly. But the styled raster does not show any value labels (see image below).
If I go to the Style tab in Layer Properties and click apply, the values are then shown, so I think I must just be missing some sort of refresh command. My understanding was that lyr.triggerRepaint() should take care of this issue. I also tried iface.mapCanvas().refresh() but no such luck.
def styleRaster(lyr, lst):
fcn = QgsColorRampShader()
fcn.setColorRampType(QgsColorRampShader.INTERPOLATED)
fcn.setColorRampItemList(lst)
shader = QgsRasterShader()
shader.setRasterShaderFunction(fcn)
renderer = QgsSingleBandPseudoColorRenderer(lyr.dataProvider(), 1, shader)
lyr.setRenderer(renderer)
lyr.triggerRepaint()
return
tciLyr = iface.addRasterLayer(hydroLyrs['tci'])
lst = [QgsColorRampShader.ColorRampItem(0, QColor("#f7fbff")),\
QgsColorRampShader.ColorRampItem(4, QColor("#deebf7")),\
QgsColorRampShader.ColorRampItem(8, QColor("#c6dbef")),\
QgsColorRampShader.ColorRampItem(12, QColor("#9ecae1")),\
QgsColorRampShader.ColorRampItem(16, QColor("#6baed6")),\
QgsColorRampShader.ColorRampItem(20, QColor("#4292c6")),\
QgsColorRampShader.ColorRampItem(24, QColor("#2171b5")),\
QgsColorRampShader.ColorRampItem(28, QColor("#08519c")),\
QgsColorRampShader.ColorRampItem(32, QColor("#08306b"))]
styleRaster(tciLyr, lst)

iface.legendInterface().refreshLayerSymbology(lyr)? – Joseph Jan 10 '18 at 11:20