Nov-26-2024, 09:52 PM
(This post was last modified: Nov-26-2024, 09:52 PM by jedan_decko.)
Hello,
I am new here, so if I make some mistake please don`t take it seriously. :-)
I have one situation on my job. I need to execute 360 questions quiz of people in company.
I started from making forms in Microsoft Forms, then used Power Automate to make action and send collected information to Power Bi.
There i come to the problem.
I wanted to make some diagram that looks like 1 in the attachment (picture 1), so I decided to go for Radar (spider) chart and wrote code in Python to execute that.
After several iterations I made something like this from attachment (picture 2). Still that is not completely I wanted, and the thing is that i want to place text in the middle of column to be rotated for 90 degrees perpendicular to last outside circle. (picture 3). Or even better to make it like on picture 1, that subcategory should be written in the cells on the most outside radius.
My question and ask for help is how to make that text go there?
Here is my code.
Please review it and give me some feedback.
Thank You in advance.
code:
I am new here, so if I make some mistake please don`t take it seriously. :-)
I have one situation on my job. I need to execute 360 questions quiz of people in company.
I started from making forms in Microsoft Forms, then used Power Automate to make action and send collected information to Power Bi.
There i come to the problem.
I wanted to make some diagram that looks like 1 in the attachment (picture 1), so I decided to go for Radar (spider) chart and wrote code in Python to execute that.
After several iterations I made something like this from attachment (picture 2). Still that is not completely I wanted, and the thing is that i want to place text in the middle of column to be rotated for 90 degrees perpendicular to last outside circle. (picture 3). Or even better to make it like on picture 1, that subcategory should be written in the cells on the most outside radius.
My question and ask for help is how to make that text go there?
Here is my code.
Please review it and give me some feedback.
Thank You in advance.
code:
import matplotlib.pyplot as plt import numpy as np import pandas as pd # Pretpostavlja se da Power BI koristi promenljivu 'dataset' df = dataset # Direktno koristimo 'dataset' iz Power BI okruženja # Provera i uklanjanje suvišnih kolona (ako postoje) if 'Unnamed: 0' in df.columns: df = df.drop(columns=['Unnamed: 0']) # Kreiranje lista etiketa, ocena i kategorija labels = df['Podkategorija'].tolist() scores = df['Ocena'].tolist() categories = df['Kategorija'].tolist() # Dodavanje zatvarajućeg elementa za ocene, etikete i kategorije scores.append(scores[0]) # Dodavanje prvog elementa na kraj labels.append(labels[0]) # Dodavanje prvog elementa na kraj categories.append(categories[0]) # Dodavanje prvog elementa na kraj # Generisanje uglova na osnovu broja ocena num_vars = len(scores) # Broj vrednosti u 'scores' određuje broj uglova angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=True).tolist() # Provera dimenzija kako bismo osigurali usklađenost assert len(angles) == len(scores), "Broj uglova i ocena nije usklađen!" assert len(labels) == len(scores), "Broj etiketa i ocena nije usklađen!" assert len(categories) == len(scores), "Broj kategorija i ocena nije usklađen!" # Dodeljivanje boja po kategorijama unique_categories = list(dict.fromkeys(categories)) # Zadržavamo redosled colors_list = ['orange', 'blue', 'green', 'red', 'purple', 'yellow', 'brown', 'pink', 'gray', 'cyan'] category_colors = {category: colors_list[i % len(colors_list)] for i, category in enumerate(unique_categories)} # Lista boja za svaki segment na osnovu kategorije segment_colors = [category_colors[category] for category in categories] # Kreiranje radijalnog dijagrama fig, ax = plt.subplots(figsize=(12, 12), subplot_kw=dict(polar=True)) # Popunjavanje segmenata sa ispravkom do grid lukova for i in range(len(scores) - 1): radii = np.linspace(0, scores[i], 100) # Glatka tranzicija do grid linija theta = np.linspace(angles[i], angles[i + 1], 100) # Glatki segment ax.fill_between(theta, 0, radii[-1], color=segment_colors[i], alpha=0.6, edgecolor="black", linewidth=1.5) # Dodavanje crnih lukova za ocene na mreži (grid) for r in range(1, 6): # Prolazak kroz nivoe ocena (1 do 5) for i in range(len(scores) - 1): if scores[i] >= r: # Ako ocena zadovoljava nivo theta = np.linspace(angles[i], angles[i + 1], 100) ax.plot( theta, [r] * len(theta), # Luk na trenutnom nivou mreže color="black", linewidth=0.8, ) # Podešavanje tickova i etiketa ax.set_xticks(angles[:-1]) # Tickovi za sve uglove bez ponovljenog ax.set_xticklabels(labels[:-1], fontsize=6, ha='center', va='center', wrap=True) # Pomeranje etiketa pod 90 stepeni u odnosu na radijalne linije for label, angle in zip(ax.get_xticklabels(), angles[:-1]): angle_deg = np.degrees(angle) if angle_deg <= 90 or angle_deg > 270: label.set_rotation(angle_deg - 90) label.set_horizontalalignment('left') else: label.set_rotation(angle_deg + 90) label.set_horizontalalignment('center') label.set_verticalalignment('center') # Podešavanje y-tickova (pretpostavljena skala od 1 do 5) ax.set_ylim(0, 5) # Postavljanje maksimalne vrednosti na osnovu podataka ax.set_yticks(range(1, 6)) ax.set_yticklabels(range(1, 6), fontsize=10) # Dodavanje mreže (sivi vodilje za neispunjene lukove) ax.grid(color='gray', linestyle='--', linewidth=0.5) # Uklanjanje spoljne ivice ax.spines['polar'].set_visible(False) # Dodavanje legende import matplotlib.patches as mpatches legend_patches = [mpatches.Patch(color=category_colors[cat], label=cat) for cat in unique_categories] ax.legend(handles=legend_patches, loc='upper right', bbox_to_anchor=(1.1, 1.1)) # Naslov za dijagram ax.set_title("360° Evaluacija", fontsize=16, pad=20) # Prikaz dijagrama plt.show()