repytex/notes_tools/tools/marks_plottings.py

117 lines
3.3 KiB
Python
Raw Normal View History

2016-11-26 13:52:46 +00:00
#!/usr/bin/env python
# encoding: utf-8
2017-03-08 19:23:19 +00:00
from .plottings import radar_graph, pivot_table_to_pie
2017-03-07 15:52:37 +00:00
from .skills_tools import count_levels, count_skill_evaluation
import matplotlib.pyplot as plt
2016-11-26 13:52:46 +00:00
import pandas as pd
import numpy as np
2017-03-08 19:23:19 +00:00
import logging
logger = logging.getLogger(__name__)
2016-11-26 13:52:46 +00:00
2017-03-07 15:52:37 +00:00
__all__ = ["radar_on",
2017-03-08 19:23:19 +00:00
"pie_pivot_table",
2017-03-07 15:52:37 +00:00
"marks_hist",
2017-03-09 11:28:20 +00:00
"parallel_on",
2017-03-07 15:52:37 +00:00
]
2017-03-07 11:06:37 +00:00
2016-11-26 15:03:03 +00:00
def radar_on(df, index, optimum = None):
""" Plot the radar graph concerning index column of the df
2016-11-26 13:52:46 +00:00
2016-11-26 15:03:03 +00:00
:param df: DataFrame with index and "Normalized" column
2016-11-26 13:52:46 +00:00
:returns: exes with radar plot
"""
comp_pt = pd.pivot_table(df,
2016-11-26 15:03:03 +00:00
index = [index],
2016-11-26 13:52:46 +00:00
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
2017-03-08 19:23:19 +00:00
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.
2017-03-07 15:52:37 +00:00
"""
2017-03-08 19:23:19 +00:00
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)
2017-03-07 15:52:37 +00:00
2017-03-09 11:01:52 +00:00
def marks_hist(df, **kwargs):
2016-11-26 13:52:46 +00:00
""" 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.
2017-03-09 11:01:52 +00:00
:param kwargs: argument to pass to hist
2016-11-26 13:52:46 +00:00
"""
bareme = df["Bareme"].max()
2017-01-31 11:06:45 +00:00
bins = int(bareme*2)
2016-11-26 13:52:46 +00:00
2017-03-09 11:01:52 +00:00
ax = df["Mark"].hist(bins = bins, range=(0,bareme), **kwargs)
2016-11-26 13:52:46 +00:00
try:
nom = df["Nom"].unique()
except KeyError:
title="Histogramme"
else:
title="Histogramme pour {}".format(" ".join(nom))
ax.set_title(title)
return ax
2017-03-31 16:01:10 +00:00
def hist_boxplot(df, kwargs_hist=[], kwargs_box=[]):
f, (ax_hist, ax_box) = plt.subplots(2, sharex=True,
gridspec_kw={"height_ratios": (.85, .15)})
marks_hist(df, ax = ax_hist, rwidth=0.9)
ev_desc = df["Mark"].describe()
2017-03-31 16:21:41 +00:00
m = round(ev_desc["mean"], 1)
2017-03-31 16:01:10 +00:00
ax_hist.plot([m,m], ax_hist.get_ylim())
ax_hist.annotate(round(ev_desc["mean"],1),
xy=(ev_desc["mean"] + 0.2, ax_hist.get_ylim()[1]-0.2))
df["Mark"].plot.box(ax = ax_box, vert=False, widths = 0.6)
ax_box.set_yticklabels("")
for e in ["min", "25%", "50%", "75%", "max"]:
2017-03-31 16:21:41 +00:00
ax_box.annotate(round(ev_desc[e], 1),
2017-03-31 16:01:10 +00:00
xy=(ev_desc[e] - 0.2, ax_box.get_ylim()[1]))
return f, (ax_hist, ax_box)
2017-03-09 11:28:20 +00:00
def parallel_on(df, index, student=None):
""" Plot parallel one line by student
2016-11-26 16:26:47 +00:00
: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
2016-11-26 13:52:46 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del