pivot table pie plots

This commit is contained in:
Benjamin Bertrand 2017-03-08 22:23:19 +03:00
parent 1ca9a45550
commit ac4040641c
2 changed files with 40 additions and 14 deletions

View File

@ -1,15 +1,16 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # 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 from .skills_tools import count_levels, count_skill_evaluation
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import pandas as pd import pandas as pd
import numpy as np import numpy as np
import logging
logger = logging.getLogger(__name__)
__all__ = ["radar_on", __all__ = ["radar_on",
"pie_skill_evaluation", "pie_pivot_table",
"pies_skills_level",
"marks_hist", "marks_hist",
"parallele_on", "parallele_on",
] ]
@ -33,18 +34,16 @@ def radar_on(df, index, optimum = None):
fig, ax = radar_graph(labels, values, optimum) fig, ax = radar_graph(labels, values, optimum)
return fig, ax return fig, ax
def pie_skill_evaluation(df, skill): def pie_pivot_table(df, pies_per_lines = 3, **kwargs):
""" Plot a pie plot with the repartition of skill evaluations """ Plot a pie plot of the pivot_table of df
"""
ax = count_skill_evaluation(df, skill).plot.pie(autopct='%.0f')
return ax
def pies_skills_level(df, skill): :param df: the dataframe.
""" Plot series of pies (one by different skill) with level repartition """ :param pies_per_lines: Number of pies per line.
levels_counts = count_levels(df, skill) :param kwargs: arguments to pass to pd.pivot_table.
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) logger.debug(f"pie_pivot_table avec les arguments {kwargs}")
return fig, ax pv = pd.pivot_table(df, **kwargs)
return pivot_table_to_pie(pv, pies_per_lines)
def marks_hist(df): def marks_hist(df):
""" Return axe for the histogramme of the dataframe """ Return axe for the histogramme of the dataframe

View File

@ -71,6 +71,33 @@ def radar_graph(labels = [], values = [], optimum = []):
ax.set_varlabels(labels) ax.set_varlabels(labels)
return fig, ax 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' # Reglages pour 'vim'