Angry Bird
Angry Bird

Reputation: 89

How can I create an image in python by using a matrix?

m = [98.75, 97, 96.66, 42, 9, 4, 1,98.33, 95, 93.33, 44, 7, 4, 1,95,94,87.5, 38.33, 5, 0, 
0,95, 93, 85, 35.55,5,0,0,95,92,83,30,3.33,0,0,95,91,80,28,1.66,0,0,95,90,75,21.25,1.66,0,0]

From the above list, I need to create a 7x7 matrix, as follows:

      [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]
 [1,] 98.75 98.33 95.00 95.00 95.00 95.00 95.00
 [2,] 97.00 95.00 94.00 93.00 92.00 91.00 90.00
 [3,] 96.66 93.33 87.50 85.00 83.00 80.00 75.00
 [4,] 42.00 44.00 38.33 35.55 30.00 28.00 21.25
 [5,]  9.00  7.00  5.00  5.00  3.33  1.66  1.66
 [6,]  4.00  4.00  0.00  0.00  0.00  0.00  0.00
 [7,]  1.00  1.00  0.00  0.00  0.00  0.00  0.00

from the matrix need to generate an image like:

enter image description here

Upvotes: 0

Views: 134

Answers (2)

My Work
My Work

Reputation: 2508

First, convert list to array with numpy, then reshape and then use your favourite package to plot, eg. seaborn or matplotlib:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

m = [98.75, 97, 96.66, 42, 9, 4, 1,98.33, 95, 93.33, 44, 7, 4, 1,95,94,87.5, 38.33, 5, 0, 
0,95, 93, 85, 35.55,5,0,0,95,92,83,30,3.33,0,0,95,91,80,28,1.66,0,0,95,90,75,21.25,1.66,0,0]


data = np.asarray(m).reshape(7,7)[::-1]
sns.heatmap(data, cmap = 'jet')
# plt.imshow(data)
plt.xlabel('Gain USRP2 (receiver side) (db)')
plt.ylabel('Gain USRP1 (sender side) (db)')

enter image description here

enter image description here

Upvotes: 3

Nicolas Gervais
Nicolas Gervais

Reputation: 36604

I like to use plt.matshow():

import numpy as np
import matplotlib.pyplot as plt

m = [98.75, 97, 96.66, 42, 9, 4, 1,98.33, 95, 93.33, 
     44, 7, 4, 1,95,94,87.5, 38.33, 5, 0, 0,95, 93, 
     85, 35.55,5,0,0,95,92,83,30,3.33,0,0,95,91,80,28,
     1.66,0,0,95,90,75,21.25,1.66,0,0]

plt.matshow(np.array(m).reshape(7, 7).T)
plt.show()

enter image description here

Upvotes: 2

Related Questions