I have a 3d data set that is actually two 2D matrices.
Example data:
z = np.random.uniform(low=0, high=100, size=(6,5,2))
Think of it as a 2d matrix with two layers. Speaking in analogies, one layer ([:,:,0]
) could be the price of an item from 0$ to 100$ and the other layer ([:,:,1]
) is an indication on how much I like it (0 - 100). Now I want to plot an imshow
plot for the second layer. The map would then show me which item are more desirable to buy and which are not.
plt.imshow(z[:,:,1])
plt.show()
But now let's assume, I only have 80 $ in my pocket. So I need to mask every item that costs more than 80 $. I would like to do that by changing the style of that cell, e.g. placing a red cross over it, or changing the alpha-value or such.
In research of an answer, I stumbled across a technique on how to change the style of the text color inside, which is nice but not exactly what I am looking for:
for i, j in itertools.product(range(z.shape[0]), range(z.shape[1])):
plt.text(j, i, format(z[i, j, 1], '.2f'),
ha="center", va="center",
color="black" if z[i, j, 0] < 80 else "red")
The interpretation would be that I am going to buy the item in row 3, col 4 because I love it and it seems to be under 80 $. I don't care about how much it costs, as long as it's not "marked".
If I only had one layer to plot colors from in my imshow
, I guess I could solve it by manipulating the colormap and setting each color with a value > 80 $ to red or give it another style. But I don't know how to do it with data from another matrix. Maybe there is a way like with the text labels, with an if-statement to check for styles in imshow
?
My ideal output would look like this:
But other ways of marking excluded cells are highly welcome.