赞
踩
这是一个把矩阵或者数组绘制成图像的函数,使用方法如下:
下面的代码将生成一个12*12的矩阵,每个位置的数据随机生成
- import matplotlib.pylab as plt
- import numpy as np
-
- def samplemat(dims):
- aa = np.zeros(dims)
- for i in range(dims[0]):
- for j in range(dims[1]):
- aa[i,j] = np.random.randint(1, 10)
- return aa
-
-
- dimlist = [(12, 12)]
- for d in dimlist:
- arr = samplemat(d)
- plt.matshow(arr)
-
- plt.show()
运行结果为:
也可以指定任意行和列的矩阵绘图,下面的代码将画出来一个一行12列的图:
- import matplotlib.pylab as plt
- import numpy as np
-
- def samplemat(dims):
- aa = np.zeros(dims)
- for i in range(dims[1]):
- aa[0,i] = i
- return aa
-
-
- dimlist = [(1, 12)]
- for d in dimlist:
- arr = samplemat(d)
- plt.matshow(arr)
-
- plt.show()
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。