PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 28, 2022

[FIXED] How to sort elements in 2d array like pie chart or sunburst chart in python?

 November 28, 2022     pie-chart, python-3.x, sorting, sunburst-diagram     No comments   

Issue

I have 2d array

I wanted to divide 5 values in this array such that it should be kind of structure like sunburst or pie chart.

I will be very thankful if you could provide python code or idea to do so, any related suggestions are also welcome. Thank you!


Solution

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)



Answered By - Dvarkesh
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing