From 97a0d7117cb8f994e44e6f2e1644bdd404b6529a Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Thu, 17 Nov 2016 17:58:01 +0300 Subject: [PATCH] add radar_plot --- notes_tools/__init__.py | 2 +- notes_tools/tools/__init__.py | 1 + notes_tools/tools/plottings.py | 77 ++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 notes_tools/tools/plottings.py diff --git a/notes_tools/__init__.py b/notes_tools/__init__.py index ab2f3a6..9500821 100644 --- a/notes_tools/__init__.py +++ b/notes_tools/__init__.py @@ -2,7 +2,7 @@ # encoding: utf-8 from .generate_bilan import generate_bilan -from .tools import extract_flat_marks, get_class_ws, digest_flat_df, students_pov +from .tools import extract_flat_marks, get_class_ws, digest_flat_df, students_pov, radar_graph # ----------------------------- # Reglages pour 'vim' diff --git a/notes_tools/tools/__init__.py b/notes_tools/tools/__init__.py index 9c05167..a8609cb 100644 --- a/notes_tools/tools/__init__.py +++ b/notes_tools/tools/__init__.py @@ -5,6 +5,7 @@ from .extract import extract_flat_marks, get_class_ws from .df_marks_manip import digest_flat_df, students_pov from .eval_tools import select_eval, get_present_absent, keep_only_presents +from .plottings import radar_graph diff --git a/notes_tools/tools/plottings.py b/notes_tools/tools/plottings.py new file mode 100644 index 0000000..57645b5 --- /dev/null +++ b/notes_tools/tools/plottings.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +# encoding: utf-8 + +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) + + + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del