Last active
December 7, 2023 15:49
-
-
Save sergiobuj/6721187 to your computer and use it in GitHub Desktop.
Matplotlib radar chart. Adapted from http://matplotlib.org/examples/api/radar_chart.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## How to Use: | |
from radar import radar_graph | |
labels = ['v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9'] | |
values = [1, 1, 2, 7, 4, 0, 3, 10, 6] | |
optimum = [5, 3, 2, 4, 5, 7, 5, 8, 5] | |
radar_graph(labels, values, optimum) | |
## Notes: | |
## Colors -> values will draw in black and optimum in red | |
## png resolution -> change the resolution to 'x' in the last line like: plt.savefig("radar.png", dpi=x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib.path import Path | |
from matplotlib.spines import Spine | |
from matplotlib.projections.polar import PolarAxes | |
from matplotlib.projections import register_projection | |
def _radar_factory(num_vars): | |
theta = 2*np.pi * np.linspace(0, 1-1./num_vars, num_vars) | |
theta += np.pi/2 | |
def unit_poly_verts(theta): | |
x0, y0, r = [0.5] * 3 | |
verts = [(r*np.cos(t) + x0, r*np.sin(t) + y0) for t in theta] | |
return verts | |
class RadarAxes(PolarAxes): | |
name = 'radar' | |
RESOLUTION = 1 | |
def fill(self, *args, **kwargs): | |
closed = kwargs.pop('closed', True) | |
return super(RadarAxes, self).fill(closed=closed, *args, **kwargs) | |
def plot(self, *args, **kwargs): | |
lines = super(RadarAxes, self).plot(*args, **kwargs) | |
for line in lines: | |
self._close_line(line) | |
def _close_line(self, line): | |
x, y = line.get_data() | |
# FIXME: markers at x[0], y[0] get doubled-up | |
if x[0] != x[-1]: | |
x = np.concatenate((x, [x[0]])) | |
y = np.concatenate((y, [y[0]])) | |
line.set_data(x, y) | |
def set_varlabels(self, labels): | |
self.set_thetagrids(theta * 180/np.pi, labels) | |
def _gen_axes_patch(self): | |
verts = unit_poly_verts(theta) | |
return plt.Polygon(verts, closed=True, edgecolor='k') | |
def _gen_axes_spines(self): | |
spine_type = 'circle' | |
verts = unit_poly_verts(theta) | |
verts.append(verts[0]) | |
path = Path(verts) | |
spine = Spine(self, spine_type, path) | |
spine.set_transform(self.transAxes) | |
return {'polar': spine} | |
register_projection(RadarAxes) | |
return theta | |
def radar_graph(labels = [], values = [], optimum = []): | |
N = len(labels) | |
theta = _radar_factory(N) | |
max_val = max(max(optimum), max(values)) | |
fig = plt.figure() | |
ax = fig.add_subplot(1, 1, 1, projection='radar') | |
ax.plot(theta, values, color='k') | |
ax.plot(theta, optimum, color='r') | |
ax.set_varlabels(labels) | |
#plt.show() | |
plt.savefig("radar.png", dpi=100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I was using your code to visualize some results in my research,
and couldn't help but notice you aren't using the max_val (line 60);
Do you know how to setup a min_val and max_val for the whole graph?
Thanks,