I would like to symbolize raster layers using PyQGIS. If I understand correctly using a prepared style-file is no solution, since the maximum value of the raster changes depending on the inputs of it's calculation. So, if the symbology expects values from 0 to 100, all cells with values above that will disappear. If I configure the style file to go up to 1000 it will warp the colour ramp if the raster values are just up to 100. I would also like to change the labeling and remove the Band information of the raster representation in the legend in the process.
The following code works in doing the first but does not change the Legend Symbol.
sum_raster_in_map = QgsProject.instance().addMapLayer(sum_raster)
ndvi_layer = sum_raster_in_map
prov = ndvi_layer.dataProvider()
stats = prov.bandStatistics(1, QgsRasterBandStats.All, ndvi_layer.extent(), 0)
max_value = stats.maximumValue
renderer = QgsSingleBandPseudoColorRenderer(prov, 1)
style = QgsStyle.defaultStyle()
color_ramp = style.colorRamp('Magma')
renderer.setClassificationMin(1)
renderer.setClassificationMax(max_value)
renderer.createShader(color_ramp, clip=True)
ndvi_layer.setRenderer(renderer)
ndvi_layer.triggerRepaint()