36

In Matlab I can visualize a matrix data quite easily with

data = rand(10,10); % Createas a 10 x 10 random matrix
imagesc(data);
colorbar;

Now I want to do the same thing in python. I already know how to plot a 2d matrix (numpy array):

from matplotlib.pyplot import imshow
import numpy as np
data = np.random.random((10,10))
imshow(np.asarray(img))

but I don't know how to add a colorbar to it. Any ideas?

2 Answers 2

75
import numpy as np
import matplotlib.pyplot as plt

plt.imshow(np.random.random((50,50)))
plt.colorbar()
plt.show()
19

Another possibility is to use plt.matshow()

import numpy as np
import matplotlib.pyplot as plt

plt.matshow(np.random.random((50,50)))
plt.colorbar()
plt.show()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.