MatplotLibAPI 3.2.13__py3-none-any.whl → 3.2.15__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
MatplotLibAPI/Area.py ADDED
@@ -0,0 +1,76 @@
1
+ """Area chart helpers."""
2
+
3
+ from typing import Any, Optional, Tuple
4
+
5
+ import pandas as pd
6
+ from matplotlib.axes import Axes
7
+ from matplotlib.figure import Figure
8
+
9
+ from .StyleTemplate import (
10
+ AREA_STYLE_TEMPLATE,
11
+ StyleTemplate,
12
+ string_formatter,
13
+ validate_dataframe,
14
+ )
15
+ from ._visualization_utils import _get_axis, _wrap_aplot
16
+
17
+
18
+ def aplot_area(
19
+ pd_df: pd.DataFrame,
20
+ x: str,
21
+ y: str,
22
+ label: Optional[str] = None,
23
+ stacked: bool = True,
24
+ title: Optional[str] = None,
25
+ style: StyleTemplate = AREA_STYLE_TEMPLATE,
26
+ ax: Optional[Axes] = None,
27
+ **kwargs: Any,
28
+ ) -> Axes:
29
+ """Plot area charts, optionally stacked for part-to-whole trends."""
30
+ cols = [x, y]
31
+ if label:
32
+ cols.append(label)
33
+ validate_dataframe(pd_df, cols=cols)
34
+ plot_ax = _get_axis(ax)
35
+
36
+ if label:
37
+ pivot_df = pd_df.pivot_table(
38
+ index=x, columns=label, values=y, aggfunc="sum"
39
+ ).sort_index()
40
+ pivot_df.plot(kind="area", stacked=stacked, alpha=0.7, ax=plot_ax)
41
+ else:
42
+ sorted_df = pd_df.sort_values(by=x)
43
+ plot_ax.fill_between(
44
+ sorted_df[x], sorted_df[y], color=style.font_color, alpha=0.4
45
+ )
46
+ plot_ax.plot(sorted_df[x], sorted_df[y], color=style.font_color)
47
+
48
+ plot_ax.set_xlabel(string_formatter(x))
49
+ plot_ax.set_ylabel(string_formatter(y))
50
+ if title:
51
+ plot_ax.set_title(title)
52
+ return plot_ax
53
+
54
+
55
+ def fplot_area(
56
+ pd_df: pd.DataFrame,
57
+ x: str,
58
+ y: str,
59
+ label: Optional[str] = None,
60
+ stacked: bool = True,
61
+ title: Optional[str] = None,
62
+ style: StyleTemplate = AREA_STYLE_TEMPLATE,
63
+ figsize: Tuple[float, float] = (10, 6),
64
+ ) -> Figure:
65
+ """Plot area charts on a new figure."""
66
+ return _wrap_aplot(
67
+ aplot_area,
68
+ pd_df=pd_df,
69
+ figsize=figsize,
70
+ x=x,
71
+ y=y,
72
+ label=label,
73
+ stacked=stacked,
74
+ title=title,
75
+ style=style,
76
+ )
MatplotLibAPI/Bar.py ADDED
@@ -0,0 +1,79 @@
1
+ """Bar and stacked bar chart helpers."""
2
+
3
+ from typing import Any, Optional, Tuple
4
+
5
+ import pandas as pd
6
+ import seaborn as sns
7
+ from matplotlib.axes import Axes
8
+ from matplotlib.figure import Figure
9
+
10
+ from .StyleTemplate import (
11
+ DISTRIBUTION_STYLE_TEMPLATE,
12
+ StyleTemplate,
13
+ string_formatter,
14
+ validate_dataframe,
15
+ )
16
+ from ._visualization_utils import _get_axis, _wrap_aplot
17
+
18
+
19
+ def aplot_bar(
20
+ pd_df: pd.DataFrame,
21
+ category: str,
22
+ value: str,
23
+ group: Optional[str] = None,
24
+ stacked: bool = False,
25
+ title: Optional[str] = None,
26
+ style: StyleTemplate = DISTRIBUTION_STYLE_TEMPLATE,
27
+ ax: Optional[Axes] = None,
28
+ **kwargs: Any,
29
+ ) -> Axes:
30
+ """Plot bar or stacked bar charts for categorical comparisons."""
31
+ cols = [category, value]
32
+ if group:
33
+ cols.append(group)
34
+ validate_dataframe(pd_df, cols=cols)
35
+
36
+ plot_ax = _get_axis(ax)
37
+ plot_df = pd_df.copy()
38
+
39
+ if group:
40
+ pivot_df = plot_df.pivot_table(
41
+ index=category, columns=group, values=value, aggfunc="sum"
42
+ )
43
+ pivot_df.plot(kind="bar", stacked=stacked, ax=plot_ax, alpha=0.85)
44
+ else:
45
+ sns.barplot(
46
+ data=plot_df, x=category, y=value, palette=style.palette, ax=plot_ax
47
+ )
48
+
49
+ plot_ax.set_facecolor(style.background_color)
50
+ plot_ax.set_xlabel(string_formatter(category))
51
+ plot_ax.set_ylabel(string_formatter(value))
52
+ if title:
53
+ plot_ax.set_title(title)
54
+ plot_ax.tick_params(axis="x", labelrotation=45)
55
+ return plot_ax
56
+
57
+
58
+ def fplot_bar(
59
+ pd_df: pd.DataFrame,
60
+ category: str,
61
+ value: str,
62
+ group: Optional[str] = None,
63
+ stacked: bool = False,
64
+ title: Optional[str] = None,
65
+ style: StyleTemplate = DISTRIBUTION_STYLE_TEMPLATE,
66
+ figsize: Tuple[float, float] = (10, 6),
67
+ ) -> Figure:
68
+ """Plot bar or stacked bar charts on a new figure."""
69
+ return _wrap_aplot(
70
+ aplot_bar,
71
+ pd_df=pd_df,
72
+ figsize=figsize,
73
+ category=category,
74
+ value=value,
75
+ group=group,
76
+ stacked=stacked,
77
+ title=title,
78
+ style=style,
79
+ )
@@ -0,0 +1,69 @@
1
+ """Box and violin plot helpers."""
2
+
3
+ from typing import Any, Optional, Tuple
4
+
5
+ import pandas as pd
6
+ import seaborn as sns
7
+ from matplotlib.axes import Axes
8
+ from matplotlib.figure import Figure
9
+
10
+ from .StyleTemplate import (
11
+ DISTRIBUTION_STYLE_TEMPLATE,
12
+ StyleTemplate,
13
+ string_formatter,
14
+ validate_dataframe,
15
+ )
16
+ from ._visualization_utils import _get_axis, _wrap_aplot
17
+
18
+
19
+ def aplot_box_violin(
20
+ pd_df: pd.DataFrame,
21
+ column: str,
22
+ by: Optional[str] = None,
23
+ violin: bool = False,
24
+ title: Optional[str] = None,
25
+ style: StyleTemplate = DISTRIBUTION_STYLE_TEMPLATE,
26
+ ax: Optional[Axes] = None,
27
+ **kwargs: Any,
28
+ ) -> Axes:
29
+ """Plot box or violin charts to summarize distributions."""
30
+ cols = [column]
31
+ if by:
32
+ cols.append(by)
33
+ validate_dataframe(pd_df, cols=cols)
34
+ plot_ax = _get_axis(ax)
35
+
36
+ if violin:
37
+ sns.violinplot(data=pd_df, x=by, y=column, palette=style.palette, ax=plot_ax)
38
+ else:
39
+ sns.boxplot(data=pd_df, x=by, y=column, palette=style.palette, ax=plot_ax)
40
+
41
+ plot_ax.set_facecolor(style.background_color)
42
+ plot_ax.set_ylabel(string_formatter(column))
43
+ if by:
44
+ plot_ax.set_xlabel(string_formatter(by))
45
+ if title:
46
+ plot_ax.set_title(title)
47
+ return plot_ax
48
+
49
+
50
+ def fplot_box_violin(
51
+ pd_df: pd.DataFrame,
52
+ column: str,
53
+ by: Optional[str] = None,
54
+ violin: bool = False,
55
+ title: Optional[str] = None,
56
+ style: StyleTemplate = DISTRIBUTION_STYLE_TEMPLATE,
57
+ figsize: Tuple[float, float] = (10, 6),
58
+ ) -> Figure:
59
+ """Plot box or violin charts on a new figure."""
60
+ return _wrap_aplot(
61
+ aplot_box_violin,
62
+ pd_df=pd_df,
63
+ figsize=figsize,
64
+ column=column,
65
+ by=by,
66
+ violin=violin,
67
+ title=title,
68
+ style=style,
69
+ )