1

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")

My result looks like this: Good idea, but not exactly what I need

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:

Ideal output

But other ways of marking excluded cells are highly welcome.

offeltoffel
  • 2,691
  • 2
  • 21
  • 35
  • If you make those cells you can't affort buying red you loose the information about how much you like the item. Is that really desired? I think currently it's pretty nice to see at the same time which items you like and which items you can affort via the red text color. – ImportanceOfBeingErnest Feb 08 '19 at 14:20
  • Thanks for your input. Changing the cell color is not wanted. But changing cell style adds another important piece of information. To be concrete: I am trying to plot RMSE values of my model, but want to "mask" the runs which have an R² below a certain threshold. This is important, because only then my results can be interpreted. Adding the numbers instead will not look any good in my publication (grids are too small, I don't meet minimum font size requirements) – offeltoffel Feb 08 '19 at 14:24
  • What would "style" be, if not color? – ImportanceOfBeingErnest Feb 08 '19 at 14:25
  • Maybe opacity if that looks good. Or added lines with + or - 45 ° orientation, ... – offeltoffel Feb 08 '19 at 14:27
  • Ok, sorry, I can't know if opacity allows a meaningful visual in your case (but I would guess it's hard to grasp, unless you mean 100% transparency). I also cannot know what `...` means. I simply cannot guess what this question asks for. If you have a concrete question on how to achieve a certain thing, please clarify. – ImportanceOfBeingErnest Feb 08 '19 at 14:31
  • I marked as duplicate of a question asking how to add crosses to imshow heatmaps. (A 45° line is half a cross). If you need something else, ask a new question with more details on the desired outcome. – ImportanceOfBeingErnest Feb 08 '19 at 14:35
  • The cross idea is actually pretty nice, I will definetely consider this! Please also find an updated image manipulation on how the output could look like with inclined lines. – offeltoffel Feb 08 '19 at 14:40
  • 1
    I added a duplicate on how to add hatching to imshow plots. – ImportanceOfBeingErnest Feb 08 '19 at 14:49
  • In the meantime, I adapted the crosses (they work like a charm) and I will do the - hatching, it's called? I'll decide on what looks better in the end. Thank you for helping me out! – offeltoffel Feb 08 '19 at 14:52

0 Answers0