Home > Blockchain >  How to sort elements in 2d array like pie chart or sunburst chart in python?
How to sort elements in 2d array like pie chart or sunburst chart in python?

Time:08-28

I generate 2d array as follows square_array = np.reshape(list(np.random.choice(a=[i for i in range(1,6)], size=30*30, p=[0.3262,0.0171,0.11,0.053,0.4937])), (30,30)) I wanted to sort 1,2,3,4,5 values in this array such that it should be kind of structure like sunburst or pie chart. If square is divided in square shapes ,this way will also work. or one solution I was thinking of :

  1. Consider circle circumscribed in square where πr^2 = 900, so r = 16.92 let say 17. so square will of 34*34 in which imaginary circle will of r = 17 and all other values outside are -1. Then I should able to sort our 5 elements inside that imaginary circle like pie chart or sunburst fashion. (we can not divide exactly like circle in this case as array and elements in array are like square, just approximate classification will work). Check desired output (-1: Black ,1:white , 2:red , 3:green, 4:blue, 5:yellow)
  2. approximate sorting will also work. Please fell free if you need to change the values in probability array p.

Desired Final output : Check sample result where ( 1:white, 2:red, 3:green, 4:blue, 5:yellow). My array should be sorted/ divided into 5 parts such that values should originate from center/near center. and divide elements somewhat like sunburst or pie chart.

Approach shown in 1 & 2 are different , In both 900 cells(in 1 it may be approx.) are divided into 5 parts as per 2d array generated with given probabilities. This are rough outputs I have prepared. and your output may vary. I will be very thankful if you could provide python code or idea to do so, any related suggestions are also welcome. Thank you!

CodePudding user response:

This may be the way that you can divide your square grid according to percentages into five types. While I am not sure how to do it in a circular way.


    mat = np.reshape(list(np.random.choice(a=[i for i in range(1,6)], size=30*30, p=[0.326 ,0.02 ,0.11,0.053,0.491])), (30,30))
    mat2= [[0 for col in range(30)] for row in range(30)]
    
    lst=[0,0,0,0,0]
    for i in range(30):
        for j in range(30):
            lst[mat[i][j]-1]  = 1
    print(lst) 
    
    lst2=[0,0,0,0,0]
    for i in range(len(lst)):
        lst2[i] = [lst[i], i 1]
    print(lst2)    
    print(sorted(lst2))
    lst3=sorted(lst2)
    
    
    def action(n,v):
        count = n
        for j in range(len(mat2)//2,-1,-1):
            for i in range(0,len(mat2)//2):
                if mat2[i][j]==0 and count!=0:
                    mat2[i][j] = v
                    count -= 1
                    
        for j in range(len(mat2)//2):
            for i in range(len(mat2)//2,len(mat2)):
                 if mat2[i][j]==0 and count!=0:
                    mat2[i][j] = v
                    count -= 1
            
        for j in range(len(mat2)//2,len(mat2)):
            for i in range(len(mat2)//2,len(mat2)):
                if mat2[i][j]==0 and count!=0:
                    mat2[i][j] = v
                    count -= 1
            
        for i in range(len(mat2)//2,-1,-1):
            for j in range(len(mat2)//2,len(mat2),-1):
                if mat2[i][j]==0 and count!=0:
                    mat2[i][j] = v
                    count -= 1
               
        for i in range(len(mat2)):
            for j in range(len(mat2)):
                if mat2[i][j]==0 and count != 0:
                    mat2[i][j]=v
                    count-=1
       
    for val in lst3:                    
        action(val[0],val[1])
    for i in mat2:
        print(i)

  • Related