如何使用颜色条在python中绘制二维矩阵?(如 Matlab 中的 imagesc)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42116671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to plot a 2d matrix in python with colorbar? (like imagesc in Matlab)
提问by mcExchange
In Matlab I can visualize a matrix data
quite easily with
在 Matlab 中,我可以data
很容易地将矩阵可视化
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):
现在我想在 python 中做同样的事情。我已经知道如何绘制二维矩阵(numpy 数组):
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?
但我不知道如何为其添加颜色条。有任何想法吗?
回答by Alexandre Kempf
import numpy as np
import matplotlib.pyplot as plt
plt.imshow(np.random.random((50,50)))
plt.colorbar()
plt.show()
回答by Gartmair
Another possibility is to use plt.matshow()
另一种可能性是使用plt.matshow()
import numpy as np
import matplotlib.pyplot as plt
plt.matshow(np.random.random((50,50)));
plt.colorbar()
plt.show()
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.matshow.html
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.matshow.html