Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

draw grid lines between same values in a matrix in python

I have a np.array in python as below:

mat = np.array([[0.2, 0.2, 0.1, 0.1, 0.1],
      [0.2, 0.2, 0.1, 0.1, 0.0],
      [0.2, 0.2, 0.1, 0.1, 0.0],
      [0.2, 0.1, 0.1, 0.0, 0.0]])
mat

Output

I would like to draw lines between the same values in this array such that:

Output with grid line

And finally I would like to have a plot like this:

Grid Plot

I looked for a matplotlib and turtle library in python but could not find a way to plot this.

like image 698
Earl Hur Avatar asked Dec 18 '25 20:12

Earl Hur


1 Answers

Using only matplotlib :

import matplotlib.pyplot as plt

mat = [[0.2, 0.2, 0.1, 0.1, 0.1],
      [0.2, 0.2, 0.1, 0.1, 0.0],
      [0.2, 0.2, 0.1, 0.1, 0.0],
      [0.2, 0.1, 0.1, 0.0, 0.0]]

def line(x,y):
     plt.plot(x,y,marker = 'o',color = 'red',
     markerfacecolor='black',markeredgecolor='black')

def draw(mat):
     for i in range(len(mat)):
          for k in range(len(mat[i])-1):
               if mat[i][k] == mat[i][k+1] :
                    line([k,k+1],[len(mat)-i,len(mat)-i])
     for i in range(len(mat)-1):
          for k in range(len(mat[i])):
               if mat[i][k] == mat[i+1][k]:
                    line([k,k],[len(mat)-i,len(mat)-i-1])

draw(mat)
plt.show()

like image 106
Ayyoub ESSADEQ Avatar answered Dec 20 '25 11:12

Ayyoub ESSADEQ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!