From ac4040641c36b257f4e7a59d2c757893de1c6ce2 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Wed, 8 Mar 2017 22:23:19 +0300 Subject: [PATCH] pivot table pie plots --- notes_tools/tools/marks_plottings.py | 27 +++++++++++++-------------- notes_tools/tools/plottings.py | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/notes_tools/tools/marks_plottings.py b/notes_tools/tools/marks_plottings.py index 5b87f20..8e01c6a 100644 --- a/notes_tools/tools/marks_plottings.py +++ b/notes_tools/tools/marks_plottings.py @@ -1,15 +1,16 @@ #!/usr/bin/env python # encoding: utf-8 -from .plottings import radar_graph +from .plottings import radar_graph, pivot_table_to_pie from .skills_tools import count_levels, count_skill_evaluation import matplotlib.pyplot as plt import pandas as pd import numpy as np +import logging +logger = logging.getLogger(__name__) __all__ = ["radar_on", - "pie_skill_evaluation", - "pies_skills_level", + "pie_pivot_table", "marks_hist", "parallele_on", ] @@ -33,18 +34,16 @@ def radar_on(df, index, optimum = None): fig, ax = radar_graph(labels, values, optimum) return fig, ax -def pie_skill_evaluation(df, skill): - """ Plot a pie plot with the repartition of skill evaluations - """ - ax = count_skill_evaluation(df, skill).plot.pie(autopct='%.0f') - return ax +def pie_pivot_table(df, pies_per_lines = 3, **kwargs): + """ Plot a pie plot of the pivot_table of df -def pies_skills_level(df, skill): - """ Plot series of pies (one by different skill) with level repartition """ - levels_counts = count_levels(df, skill) - fig, ax = plt.subplots(nrows=1, ncols=len(levels_counts), figsize=(16,3)) - plots = levels_counts.T.plot(ax=ax ,kind="pie", subplots=True, legend=False) - return fig, ax + :param df: the dataframe. + :param pies_per_lines: Number of pies per line. + :param kwargs: arguments to pass to pd.pivot_table. + """ + logger.debug(f"pie_pivot_table avec les arguments {kwargs}") + pv = pd.pivot_table(df, **kwargs) + return pivot_table_to_pie(pv, pies_per_lines) def marks_hist(df): """ Return axe for the histogramme of the dataframe diff --git a/notes_tools/tools/plottings.py b/notes_tools/tools/plottings.py index 6f61ac5..54c4a21 100644 --- a/notes_tools/tools/plottings.py +++ b/notes_tools/tools/plottings.py @@ -71,6 +71,33 @@ def radar_graph(labels = [], values = [], optimum = []): ax.set_varlabels(labels) return fig, ax +def my_autopct(values): + def my_autopct(pct): + total = sum(values) + val = int(round(pct*total/100.0)) + return f'{val}' + return my_autopct + +def pivot_table_to_pie(pv, pies_per_lines = 3): + nbr_pies = len(pv.columns) + nbr_cols = min(pies_per_lines, nbr_pies) + nbr_rows = max(nbr_pies % nbr_cols,1) + f, axs = plt.subplots(nbr_rows, nbr_cols, figsize = (4*nbr_cols,4*nbr_rows)) + for (c, ax) in zip(pv, axs.flatten()): + datas = pv[c] + explode = [0.1]*len(datas) + pv[c].plot(kind="pie", + ax=ax, + use_index = False, + title = f"{c} (total={datas.sum()})", + legend = False, + autopct=my_autopct(datas), + explode = explode, + ) + ax.set_ylabel("") + for i in range(nbr_pies//nbr_cols, nbr_cols*nbr_rows): + axs.flat[i].axis("off") + return (f, axs) # ----------------------------- # Reglages pour 'vim'