Extract the max and min values from a ColumnDataSource column

I’m doing a Bokeh application in which I have an input table, some calculations are performed on it and it produces a new table. I’m trying to plot a heatmap of this new table, so I have to create a colorbar using the LinearColorMapper function, however I can’t use the min and max values from the calculated table (which is a ColumnDataSource), this is how the table is stored:

def val_portafolio_mostrar():
  val_portafolio=datos_calcular()
  val_mapa=pd.DataFrame(val_portafolio.stack(), columns=['valoracion']).reset_index()
  datos_heatmap.data=dict(val_mapa)

The values which are going to be plotted on the heatmap are in the ‘valoracion’ column from datos_heatmap, this is the code I’m using for the LinearColorMapper

colors = ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d"]
mapper = LinearColorMapper(palette=colors, low=min(datos_heatmap.data['valoracion']), 
                            high=max(datos_heatmap.data['valoracion']))

however I’m getting the following error:

in mapper_fun
    mapper = LinearColorMapper(palette=colors, low=min(datos_heatmap.data['valoracion']),
ValueError: min() arg is an empty sequence

I think it’s because in order to access a ColumnDataSource value, the function needs to have the “source” parameter, however the LinearColorMapper function does not have this parameter so it’s not possible to solve it this way. I also tried to store the max and min values in another ColumnDataSource but I get the same error because I’m not using a source rather just extracting the values as in “datos_heatmap.data[‘valoracion’]”

Thanks in advance!

@A_P your code is structured just fine, the issue is that you are passing min an empty list or array:

In [1]: min([])
------------------------------------------------------------
ValueError                 Traceback (most recent call last)
<ipython-input-1-cb4199d4da81> in <module>
----> 1 min([])

ValueError: min() arg is an empty sequence

You have to actively take care of this in your code:

  • make sure that datos_heatmap.data['valoracion'] is never empty

or

  • check for it being empty, and take (different) appropriate action that does not call min if it is empty