2

I want to plot two matrices in the same graph. These matrices have the shape 3x5. They were created using meshgrid for two arrays of the size 3 & 5 (a is size 3, b is size 5). The entries of the matrices were calculated using the values from the arrays, which I want to show in the plot, so e.g. if M1 was calculated with the entries a1 and b1, M1 should be shown where the two indices meet in the diagram. Also, the axis of the diagram should be labeled with the entries of the two arrays.

To ensure a better understanding of my question, I will post a picture of the desired output in this post. In my specific usecase, some values of the two matrices will be NaNs, so I can see, where the two matrices overlap. An example of the two matrices would be:

M1 = ([5, 3, nan], 
      [2, 5, nan], 
      [6, 7, nan], 
      [9, 10, nan], 
      [11, 12, nan])
M2 = ([nan, nan, nan],
      [nan, 1, 2], 
      [nan, 8, 5], 
      [nan, 6, 9], 
      [nan, nan, nan])

enter image description here I am sure this is a basic question, but I am new to python and appreciate any help.

Thank you in advance!

Mr. T
  • 11,960
  • 10
  • 32
  • 54
lempy
  • 103
  • 8
  • 2
    Could you give an example of the two matrices? – Ruthger Righart Nov 30 '20 at 08:03
  • M1 = ([5, 3, nan], [2, 5, nan], [6, 7, nan], [9, 10, nan], [11, 12, nan]) ; M2 = ([nan, nan, nan], [nan, 1, 2], [nan, 8, 5], [nan, 6, 9], [nan, nan, nan]) – lempy Nov 30 '20 at 08:10
  • I tried to write in nan's in the examples where the matrices in the picture I posted dont have entrys, I hope thats correct. – lempy Nov 30 '20 at 08:16
  • 1
    A) Are the two matrices always of the same size (e.g., both 5x7)? B) Are both matrices filled with values that guarantee a rectangle (not that M1 starts like `M1 = ([5, 3, nan], [2, 5, 4],...`)? If both answers are yes, then calculate min/max indexes of non-NaN values for each matrix and plot these coordinates as a rectangle. – Mr. T Nov 30 '20 at 08:23
  • @Mr.T A) yes, both matrices are always the same size B) No, they are not always rectangles.. the example you gave for M1 is possible. – lempy Nov 30 '20 at 08:29
  • Would then `M1 = ([5, 3, 1], [2, nan, 4], [1, 3, 5],...)` be possible? If so, should this "hole" appear in the graph? I suggest you edit your question when you add information - comments are often ignored when reading the question. Also, what have you tried so far, why did it fail? – Mr. T Nov 30 '20 at 08:34

1 Answers1

1

I thought about how difficult it will be to find the hull figure of each matrix, and it is not even clear if there might be holes in your matrix. But why don't we let numpy/matplotlib do all the work?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors

M1 = ([5,      3,      6,      7], 
      [2,      np.nan, 3,      6], 
      [6,      7,      8,      np.nan], 
      [9,      10,     np.nan, np.nan], 
      [11,     12,     np.nan, np.nan])

M2 = ([np.nan, np.nan, np.nan, np.nan],
      [np.nan, np.nan, 1,      2], 
      [np.nan, 4,      8,      5], 
      [np.nan, np.nan, 6,      9], 
      [np.nan, np.nan, np.nan, np.nan])

#convert arrays into truth values regarding the presence of NaNs
M1arr = ~np.isnan(M1)
M2arr = ~np.isnan(M2)

#combine arrays based on False = 0, True = 1
M1M2arr = np.sum([M1arr, 2 * M2arr], axis=0) 

#define color scale for the plot
cmapM1M2 = colors.ListedColormap(["white", "tab:blue", "tab:orange", "tab:red"])

cb = plt.imshow(M1M2arr, cmap=cmapM1M2)
cbt= plt.colorbar(cb, ticks=np.linspace(0, 3, 9)[1::2])
cbt.ax.set_yticklabels(["M1 & M2 NaN", "only M1 values", "only M2 values", "M1 & M2 values"])
plt.xlabel("a[i]")
plt.ylabel("b[i]")

plt.tight_layout()
plt.show()

Sample output: ![enter image description here

I have kept the orientation of imshow because this is how you would read the matrix entries when printed out. You can invert this image into the usual coordinate representation by changing this line:

cb = plt.imshow(M1M2arr, cmap=cmapM1M2, origin="lower")
Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • Thank you for your amazing answer. This is exactly what I wanted to do. Now the only thing left to do is to read further in the documentation of your solution. :D – lempy Nov 30 '20 at 14:26
  • I fully support this. The matplotlib documentation is excellent, but for a start I would go through the [tutorials](https://matplotlib.org/tutorials/index.html). And tbh, most everyday problems can be solved by adapting the code in their [example gallery](https://matplotlib.org/gallery/index.html#). – Mr. T Nov 30 '20 at 14:38