Richard Rublev
Richard Rublev

Reputation: 8164

Matplotlib,how to represent array as image?

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?

Upvotes: 3

Views: 19882

Answers (1)

Anil_M
Anil_M

Reputation: 11453

I don't have data from base.txt.
However, in order to simulate your problem, I created random numbers between 1500 to 5500 and created a 50 x 100 numpy array , which I believe is close to your data and requirement.

Then I simply plotted the data as per your plot code. I am getting true representation of the array. See if this helps.

Demo Code

#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()

Plot enter image description here

Upvotes: 9

Related Questions