Matplotlib,how to represent array as image?

时间:2016-04-04 18:38:01

标签: python arrays matplotlib

This is what I have tried so far

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

with open('base.txt','r') as f:
    vst = map(int, itertools.imap(float, f))

v1=vst[::3]
print type(v1)

a=np.asarray(v1)
print len(a)
a11=a.reshape(50,100)

plt.imshow(a11, cmap='hot')
plt.colorbar()
plt.show()

I have (50,100) array and each element has numerical value(range 1200-5400).I would like to have image that would represent array.But I got this enter image description here

What should I change to get proper image?

1 个答案:

答案 0 :(得分:2)

我没有base.txt的数据 但是,为了模拟您的问题,我在1500 to 5500之间创建了一个随机数,并创建了50 x 100 numpy array,我认为这与您的数据和要求非常接近。

然后我根据您的绘图代码简单地绘制数据。 我得到了数组的真实表示。 看看这是否有帮助。

演示代码

#import itertools
import numpy as np
from numpy import array
import matplotlib.pyplot as plt
import random


#Generate a list of 5000 int between 1200,5500
M = 5000
myList = [random.randrange(1200,5500)  for i in xrange(0,M)]

#Convert to 50 x 100 list
n = 50
newList = [myList[i:i+n] for i in range(0, len(myList), n)]

#Convert to 50 x 100 numpy array
nArray = array(newList)
print nArray

a11=nArray.reshape(50,100)
plt.imshow(a11, cmap='hot')
plt.colorbar()
plt.show()

<强>剧情 enter image description here