Parliament chart sample

After the above remarks, I expected @U-Danny to comment and explain his code. Since he didn’t, I do it.

What is called arco_total is the sum of the lengths of arcs that form the configuration of parliament seats.
Each arc of a such configuration subtends an angle Ψ=angle_total (given in degrees), but has a diferent radius.
The length of an arc of the circle of radius r, that subtends an angle θ (in radians) is θ*r.
Hence in your function, parlamentary_Coord, arco_total should be arco_total += angle_total * math.pi* (ratio+i)/180 (i.e. the degrees must be transformed into radians to be valid the formula of arc length). Since in your code it is just arco_total += math.pi* (ratio+i), it means that you performed the right computation only when arco_total=180
Analogously, arco_radio should be arco_radio = angle_total*math.pi * (ratio+i)/180 (this is the length of the arc that subtends the angle angle_total and has the radius ratio+i (ratio is not an intuitive name for its significance, as the difference of two consecutive radii in the parliamentary configuration).

Now let us see why your wrong math computations worked even if they were performed by your code only for angle_total=180. Because of the same arguments I gave before:
angle_total * math.pi/180 is a common factor in the fraction that appears as argument of round, and as a consequence it is simplified and finally, for a good implementation
is sufficient to define:
arco_total += (ratio+i)
arco_radio=ratio+i

with the lines of code:

for a in range(len(angles)):
    current_angle = angles[a]/2
    for i in range(int(round(angle_total/angles[a],0))):        
    coord.append((ratio+a, current_angle))
    current_angle += angles[a]

simplified, as well, your function gets:

def parlamentary_Coord(df, angle_total=180, rows=4, ratio=6, initial='NAME'):    
    arco_total = 0
    angles = []
    for i in range(rows):
        arco_total += ratio+i
    for i in range(rows):
        arco_radio = ratio+i
        angles.append(angle_total/round(arco_radio/(arco_total/len(df)),0))
    coord = []
    for k, angle in enumerate(angles):
        current_angle = angle/2
        N = int(round(angle_total/angle, 0))
        for i in range(N):        
            coord.append((ratio+k, current_angle))
            current_angle += angle
    coord = Sort_Tuple(coord)    
    df["radio"] = list(zip(*coord))[0]
    df["tetha"] = list(zip(*coord))[1]
    df["INITIAL"] = df[initial].apply(lambda x: x[0]) # only for text in marker chart
    return df
2 Likes