Calculating the area of a radar chart over time by combining multiple arrays in Python

Suppose I have an array containing scores for n characters over t days. I have some more arrays of the same shape (t, n) containing different types of scores for the same n characters. For concreteness, let’s say I have 5 scores, i.e. 5 arrays of shape (t, n). The individual scores (each normalized between 0 and 100) will change over time and my goal is to create a composite score using the area of the radar chart of the five scores for each character per day. (FYI: the area of the polygon is calculated by first assigning polar coordinates for each score (score_value, theta), converting these to (x, y) co-ordinates and then using a standard formula.) The final output containing the areas should also be a (t, n) array.

My initial idea was to combine the 5 scores into an array of shape (t, n, 5) achieved using np.dstack. Not sure if that’s a good idea. Here’s what I have so far:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import numpy as np
# Random data for demonstration
mat_1 = np.ones((20, 10))
mat_2 = np.ones((20, 10))
mat_3 = np.ones((20, 10))
mat_4 = np.ones((20, 10))
mat_5 = np.ones((20, 10))
matrix = [mat_1, mat_2, mat_3, mat_4]
my_list = np.dstack(matrix)
def radar_plot_area(data):
num_vars = len(data)
# Compute angle for each axis
angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False)
# Calculate area using shoelace formula
x = [r * np.cos(a) for r, a in zip(data, angles)]
y = [r * np.sin(a) for r, a in zip(data, angles)]
area = 0.5 * np.abs(np.sum(x * np.roll(y, -1)) - np.sum(x * np.roll(y, 1)))
return area
# Create output matrix
areas_matrix = np.zeros((20, 10))
for i in range(20):
for j in range(10):
areas_matrix[i][j] = radar_plot_area(my_list[i][j])
print(areas_matrix)
</code>
<code>import numpy as np # Random data for demonstration mat_1 = np.ones((20, 10)) mat_2 = np.ones((20, 10)) mat_3 = np.ones((20, 10)) mat_4 = np.ones((20, 10)) mat_5 = np.ones((20, 10)) matrix = [mat_1, mat_2, mat_3, mat_4] my_list = np.dstack(matrix) def radar_plot_area(data): num_vars = len(data) # Compute angle for each axis angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False) # Calculate area using shoelace formula x = [r * np.cos(a) for r, a in zip(data, angles)] y = [r * np.sin(a) for r, a in zip(data, angles)] area = 0.5 * np.abs(np.sum(x * np.roll(y, -1)) - np.sum(x * np.roll(y, 1))) return area # Create output matrix areas_matrix = np.zeros((20, 10)) for i in range(20): for j in range(10): areas_matrix[i][j] = radar_plot_area(my_list[i][j]) print(areas_matrix) </code>
import numpy as np

# Random data for demonstration
mat_1 = np.ones((20, 10))
mat_2 = np.ones((20, 10))
mat_3 = np.ones((20, 10))
mat_4 = np.ones((20, 10)) 
mat_5 = np.ones((20, 10)) 

matrix = [mat_1, mat_2, mat_3, mat_4]

my_list = np.dstack(matrix)

def radar_plot_area(data):
    num_vars = len(data)
    
    # Compute angle for each axis
    angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False)
    
    # Calculate area using shoelace formula
    x = [r * np.cos(a) for r, a in zip(data, angles)]
    y = [r * np.sin(a) for r, a in zip(data, angles)]
    area = 0.5 * np.abs(np.sum(x * np.roll(y, -1)) - np.sum(x * np.roll(y, 1)))
    
    return area

# Create output matrix
areas_matrix = np.zeros((20, 10))

for i in range(20):
    for j in range(10):
        areas_matrix[i][j] = radar_plot_area(my_list[i][j])

print(areas_matrix)

I can see it does output the correct values for the area at the end but I’m having trouble generalizing it to work with larger arrays. Ideally, I would prefer to work with arrays all the way throughout without having to explicitly refer to the indices too much, i.e. I want to have a function as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def radar_plot_area(*scores: np.array): #the inputs are the 5 scores (which are arrays) I mentioned
...
return area #output is a (t, n) matrix containing the areas for each character per day
</code>
<code>def radar_plot_area(*scores: np.array): #the inputs are the 5 scores (which are arrays) I mentioned ... return area #output is a (t, n) matrix containing the areas for each character per day </code>
def radar_plot_area(*scores: np.array): #the inputs are the 5 scores (which are arrays) I mentioned
...
return area #output is a (t, n) matrix containing the areas for each character per day

Is there a better way to achieve what I’m describing? Any suggestions would be much appreciated.

New contributor

DirkDiggler890 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật