Radar chart with different values per axes

87 Views Asked by At

*I'm a novice coder and I'm really out of depth here, hence the questions might be stupid as I'm not sure what to ask really.
*
I've created a couple of functions that extracts specific metrics that I would like to display in a radar-chart. The values of the metrics varies, and it doesn't make sense to scale them into a 0 to 1 range. Hence I've attempted to create a radar-chart with different ranges per axes. The problem I have is that the values displayed in the graphs that gets generated aren't correct.

This is how one of the radar charts looks when generated: radar-chart

I've added print statements in order to check that the values that should be displayed are correct (which it is):

Data for €100 NL: [27.84, 84.08, 0.21, 1.12, 9.51, 0.35, 0.08, 190.34]Plotting values: [27.84, 84.08, 0.21, 1.12, 9.51, 0.35, 0.08, 190.34]Values types: [<class 'float'>, <class 'float'>, <class 'float'>, <class 'float'>, <class 'float'>, <class 'float'>, <class 'float'>, <class 'float'>]

But as the graph shows the values are way of. Any ideas on how I can fix it? (see relevant code below):

class Radar(object):
    def __init__(self, fig, titles, ranges, rect=None):
        if rect is None:
            rect = [0.1, 0.1, 0.8, 0.8]  # Adjust this as needed

        self.n = len(titles)
        self.angles = np.linspace(0, 2 * np.pi, self.n, endpoint=False).tolist()

        self.axes = [fig.add_axes(rect, polar=True, label="axes%d" % i) for i in range(self.n)]

        self.ax = self.axes[0]
        self.ax.set_thetagrids(np.degrees(self.angles), labels=titles, fontsize=10)

        for ax in self.axes[1:]:
            ax.patch.set_visible(False)
            ax.xaxis.set_visible(False)

        for ax, angle, rng in zip(self.axes, self.angles, ranges):
            grid_values = np.linspace(rng[0], rng[1], num=5)
            ax.set_rgrids(grid_values, labels=[str(val) for val in grid_values], angle=np.degrees(angle), fontsize=8)
            ax.spines["polar"].set_visible(False)
            if rng[0] == rng[1]:
                ax.set_ylim([rng[0] - 1, rng[1] + 1])  # Expand range if singular
            else:
                ax.set_ylim([rng[0], rng[1]])

    def plot(self, values, *args, **kw):
        values = [float(val) for val in values]

        print(f"Plotting values: {values}")
        print(f"Values types: {[type(val) for val in values]}")

        values_to_plot = values + [values[0]]
        angles_to_plot = self.angles + [self.angles[0]]

        self.ax.plot(angles_to_plot, values_to_plot, *args, **kw)

        self.ax.fill(angles_to_plot, values_to_plot, alpha=0.1)

        print("Plotting completed.")


def plot_radar_for_games(final_df, metrics_ranges):
    color_cycle = plt.cm.Dark2(np.linspace(0, 1, final_df['game_name'].nunique()))
    color_iter = iter(color_cycle)

    for game in final_df['game_name'].unique():
        df_game = final_df[final_df['game_name'] == game].iloc[0]
        fig = plt.figure(figsize=(10, 10))
        radar = Radar(fig, list(metrics_ranges.keys()), [metrics_ranges[metric] for metric in metrics_ranges.keys()])

        data = []
        for metric in metrics_ranges.keys():
            if metric in df_game:
                value = df_game[metric]
                if metric == 'average_std_deviation':
                    value = float(value.replace('€', '').replace(',', ''))
                data.append(value)
            else:
                data.append(0)

        print(f"Data for {game}: {data}")

        radar.plot(data, '-', lw=2, alpha=0.4, label=game, color=next(color_iter))
        radar.ax.legend(loc='upper right', bbox_to_anchor=(1.1, 1.1))
        plt.title(f"Radar Chart for {game}")

    plt.show()
& defined in main:
metrics_ranges = {'VPIP': [0, 100],'bb_100': [0, 200],'winning_ratio': [0, 1],'ecology_drain_ratio': [0, 2],'Rakebb100': [0, 20],'played_last_week_ratio': [0, 1],'winning_players_ratio': [0, 1],'average_std_deviation': [0, 1500]}
0

There are 0 best solutions below