0

I need to discriminate against non physical data in my heatmaps. I am using python(bokeh and holoviews).

Example Code:

import numpy as np
import holoviews as hv
import warnings\
warnings.filterwarnings("ignore")
hv.extension('bokeh')
%opts Image [colorbar=True tools=['hover']
%opts Image (cmap='rainbow')
%output max_frames=3000
import Definitions as def #these are my equations

a = 2
b = .2
c = .3
d = .4
e = 0
f = 0
g = 0 
h = 0
i = .2
j = 0
l = .2
m = 1


N = 100 # number of points

yval = np.linspace(0.1,1,N)
xval = np.linspace(0,5,N)
bounds = (0,.1,5,1) #this sets the bounds from .1 to 1 on y axis and 0 to 5 on x axis

xval,yval = np.meshgrid(xval, yval, indexing 'xy')


v1val = def.v1(yval,b,a,m,l,xval)  #Calling my these definitions from a seperate file
v2val = def.v2(b,m,a)




Zlist = def.Z(a,v2val/d,v2val/c,h,e,i,j,xval,l,v1val,f,g)


plot = hv.Image(np.flipud(Zlist), label = "Z Heat Map" \
                   ,bounds = bounds, vdims = hv.Dimension('Z', range=(0,1))).redimlabel(x = 'x', y = 'y')

plot

This code makes a heat map where the value of the function Z is mapped as a color for a region of x and y. So Z depends on x and y and for different values of x and y, Z will have different colors.

My problem: I need to discriminate against any situations where v1val < c . The code I currently have plots all of the data but I need it to plot only the data for v1val > c and maybe assign a color like white or black to portion of the graph corresponding to v1val < c. I also similarly need to white out or blackout any region where v2val < d. Essentially I want to black out or white out regions of my heatmap that correspond to non physical data ie when v1val < c and when v2val < d.

I have been trying different things but each time I have some idea I get an error like " The truth of a value of an array with more than one element is ambiguous. Use a.any() or a.all()"

Help with blacking out this non physical data would be much appreciated.

codes4toads
  • 65
  • 1
  • 1
  • 5

1 Answers1

0

You can see how to black out or white out regions of a heatmap in http://pyviz.org/tutorial/01_Workflow_Introduction.html :

def nansum(a, **kwargs):
    return np.nan if np.isnan(a).all() else np.nansum(a, **kwargs)

heatmap = df.hvplot.heatmap('Year', 'State', 'measles', reduce_function=nansum)

enter image description here

James A. Bednar
  • 3,195
  • 1
  • 9
  • 13
  • Thanks, I can see how this could be useful if I were a better programmer. I am having trouble applying this since my data is continuous (I don't really have data, I am using equations so it is technically a simulation). Can anyone help me to apply this to my situation? – codes4toads Oct 10 '18 at 13:09
  • Since you are sampling your data on a grid, you can substitute np.nan for any grid element that should not be colormapped. Or you can do it with colormapping, e.g. to make any items in your "plot" object that have a Z value below 0.1 be transparent, do `plot.redim.range(Z=(0.1, None)).options(clipping_colors={'min': 'transparent'})` – James A. Bednar Oct 10 '18 at 17:43
  • thanks, I am trying to see how that will work for me since I need it to be "white" when v1val < c, and when v2val < d for the function Z(a,v2val/d,v2val/c,h,e,i,j,xval,l,v1val,f,g). So its not that I have a condition on Z itself, I only have a condition on two of the variables that Z depends on , ie I need to say something like: if v2val/d < 0 and or v1val/c < 0 then plot Z as "white". d and c are numerical inputs but v1val and v2val depend on multiple inputs and also on the independent variables so there values change throughout the graph. – codes4toads Oct 10 '18 at 22:37
  • Then you should be able to use the first method: Iterate through your grid cells, check whatever conditions you like, then set the grid cell to np.nan if your conditions aren't met. Then display your grid, where np.nan should end up transparent. – James A. Bednar Oct 11 '18 at 17:10