0

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.

2 Answers 2

1

Partial solution to your question: Similar problem (the colormap thing) has been partially addressed here: How can I plot NaN values as a special color with imshow in matplotlib? ... Specifically for your array, it would be as follows where you can choose any color nan_color you like to plot the nan values.

import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt

data =  np.matrix([[1, 2, 3], [-1, np.nan, -2], [7, -5, -6]])
data_masked = np.ma.array(data, mask=np.isnan(data))
nan_color = 'black' # or 'white' or whatever you like
color = cm.RdBu
color.set_bad(nan_color,1.)
plt.matshow(data_masked, cmap=color)
plt.colorbar()
Sign up to request clarification or add additional context in comments.

2 Comments

Your "here" link seems to not work, but thanks for the code snip, I'll try it out.
I don't know why hyperlink wasn't working but I edited with the complete link
1

You can first make your return a numpy float array like this:

data = np.array(data).astype(np.float)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.