repytex/notes_tools/tools/marks_plottings.py

96 lines
2.6 KiB
Python

#!/usr/bin/env python
# encoding: utf-8
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_pivot_table",
"marks_hist",
"parallele_on",
]
def radar_on(df, index, optimum = None):
""" Plot the radar graph concerning index column of the df
:param df: DataFrame with index and "Normalized" column
:returns: exes with radar plot
"""
comp_pt = pd.pivot_table(df,
index = [index],
values = ["Normalized"],
aggfunc=np.mean,
)
labels = list(comp_pt.index)
values = [i[0] for i in comp_pt.values]
if optimum is None:
optimum = [1]*len(values)
fig, ax = radar_graph(labels, values, optimum)
return fig, ax
def pie_pivot_table(df, pies_per_lines = 3, **kwargs):
""" Plot a pie plot of the pivot_table of df
: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, **kwargs):
""" Return axe for the histogramme of the dataframe
:param df: Dataframe with "Mark" and "Bareme" columns. If it has "Nom" column, it is use in title.
:param kwargs: argument to pass to hist
"""
bareme = df["Bareme"].max()
bins = int(bareme*2)
ax = df["Mark"].hist(bins = bins, range=(0,bareme), **kwargs)
try:
nom = df["Nom"].unique()
except KeyError:
title="Histogramme"
else:
title="Histogramme pour {}".format(" ".join(nom))
ax.set_title(title)
return ax
def parallele_on(df, index, student=None):
""" Plot parallele one line by student
:param df: TODO
:param index: TODO
:returns: TODO
"""
pt = pd.pivot_table(df,
index = [index],
values = ["Normalized"],
columns = ["Eleve"],
aggfunc = np.mean,
)["Normalized"]
ax = pt.plot(color="b", figsize=(10,5), legend=False)
pt.T.describe().T[["min", "25%","50%", "75%", "max"]].plot(ax=ax,
kind='area', stacked=False, alpha=0.2)
if not student is None:
pt.ix[:,student].plot(ax=ax, color="r")
return ax
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del