Q:

Additional No Data Value Raster Pyqgis 3

I would like to improve way to adjust histogram of Sentinel 2 images in Qgis 3. I am using pyqgis to do the first task to it (create histogram rightly). So I have images that 0 is a no data value. To set it I use this code: 
Set raster layer additional no data value with PyQgis
See below:
import processing
self=qgis.utils
layers = self.iface.mapCanvas().layers()

path_save = '/home/diogocaribe/sld/'

# Change o backgroud value
for layer in layers:
    layerType = layer.type()
    if layerType == QgsMapLayer.RasterLayer:
        for band in range(1, layer.bandCount()+1):
            provider = layer.dataProvider()
            provider.setNoDataValue(band, 0) #first one is referred to band number 
            layer.triggerRepaint()

So, the first thing that I have to do is create histogram. However, if I don't set 0 in additional no data value the histogram is created with different behavior:

When I defined additional no data value as 0 and recompute histogram it works well.

So I would like to fill the blank in proprieties layer using pyqgis in order to put additional no data field as 0 and fix the problem described above.

A:

Additional raster no data values are computed by using 'setUserNoDataValue' QgsRasterDataProvider method. To corroborate that, I created a very simple raster (5x5) with random values between 0 and 10 (to facilitate the counting of each raster value). At following image you can observe that there are not assigned 'no data values' in that raster. 

After running following code, for initializing its respective histogram and getting minimum, maximum and histogram vector:
layer = iface.activeLayer()

provider = layer.dataProvider()

provider.initHistogram(QgsRasterHistogram(), 0, 10)

histogram = provider.histogram(1)

print(histogram.minimum, histogram.maximum)
print(histogram.histogramVector)

I got below result:
0.0 10.0
[2, 2, 4, 3, 3, 1, 1, 2, 5, 1, 1]

Into the list, there are two zero values, two one values, four two values and so on (I corroborated each single value into the list).
For initializing a new histogram with provider methods you need following code:
layer = iface.activeLayer()
provider = layer.dataProvider()
layer.triggerRepaint()

provider.setNoDataValue(1, 0) #first one is referred to band number 
provider.setUserNoDataValue(1, [QgsRasterRange(0,0)]) #Additional No Data Value Raster

provider.initHistogram(QgsRasterHistogram(), 1, 10)

histogram = provider.histogram(1)

print(histogram.minimum, histogram.maximum)
print(histogram.histogramVector)

After running above code, I got result of following image:

It can be observed at above image that there are two zero values (brown color due its transparency as no data). Values printed at Python Console of QGIS 3.8 are in this case:
1.0 10.0
[2, 4, 3, 3, 1, 1, 2, 5, 1, 1]

Maximum, minimum and vector values, as expected, are excluding zero no data value. The definitive proof is in the following image where it can be observed zero value as "Additional no data value".

