Let's say I have the following matrix (as a numpy.matrix
or np.array
object):
#x/column labels: = -1, 0, 1
#y/row labels: 0, 1, 2 (like normal)
data: np.matrix([[1, 2, 3], [-1, np.nan, -2], [7, -5, -6]])
plt.matshow(data, colorbar = 'RdBu')
plt.colorbar()
plt.show()
I want to use plt.matshow()
on this matrix. First, I don't know why, but sometimes, my np.matrix
objects display like this if I simply print them out (just ignore that the numbers are slightly different, please):
matrix([[ 1., 2., 3.],
[-1., nan, -2.],
[-3., 5., 2.]])
These will be accepted by np.matshow()
. However sometimes they appear like this:
matrix([[1, 2, 3],
[-1, nan, -2],
[7, -5, -6]], dtype=object)
First question:
If that dtype=object
is there, np.matshow()
will not plot them if a nan
or None
is present. I don't know why this tag is there sometimes and sometimes not. I generate these through a somewhat convoluted script, and I suspect it may be sometimes because I am doing np.matrix(a)
where a
is some python array or np.array
. Could that be the cause? I have played around with simple cases in terminal windows but have not been able to figure it out. Sometimes the "error marker" is np.nan
, sometimes it's python's None
.
Second question:
When there is a np.nan
in the matrix, matshow()
just paints that square white. This is okay if the color map used, like the default one, does not include white. However, I want to use a "binary" type color scale like RdBu
; positive for blue, negative for red, white for 0. Is there a way to, for example, put a big green X over nan
's, or make its square black, etc? Basically, a way to clearly distinguish nan
's from 0's in this color map.
Third question: please notice in my first code snipped that the x/column labels are supposed to be -1, 0, 1
. Since the np.matrix
object seems to not have custom indices, it just plots starting from 0
. Is there a way to pass this info to np.matshow
? If I use plt.xlim([-1, 1])
for example, it just only shows two of the rows of the matrix, since there is no data at -1
, and the third row is at x = 2
.