MatplotLibAPI 3.2.14__py3-none-any.whl → 3.2.16__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.
@@ -0,0 +1,39 @@
1
+ """Sankey plotting helpers."""
2
+
3
+ from typing import Dict, List, Optional
4
+
5
+ import pandas as pd
6
+ import plotly.graph_objects as go
7
+
8
+ from .StyleTemplate import SANKEY_STYLE_TEMPLATE, StyleTemplate, validate_dataframe
9
+
10
+
11
+ def fplot_sankey(
12
+ pd_df: pd.DataFrame,
13
+ source: str,
14
+ target: str,
15
+ value: str,
16
+ title: Optional[str] = None,
17
+ style: StyleTemplate = SANKEY_STYLE_TEMPLATE,
18
+ ) -> go.Figure:
19
+ """Plot a Sankey diagram showing flows between categories."""
20
+ validate_dataframe(pd_df, cols=[source, target, value])
21
+
22
+ labels: List[str] = list(pd.unique(pd.concat([pd_df[source], pd_df[target]])))
23
+ label_to_index: Dict[str, int] = {name: idx for idx, name in enumerate(labels)}
24
+
25
+ sankey = go.Sankey(
26
+ node=dict(label=labels, pad=15, thickness=20, color=style.font_color),
27
+ link=dict(
28
+ source=[label_to_index[val] for val in pd_df[source]],
29
+ target=[label_to_index[val] for val in pd_df[target]],
30
+ value=pd_df[value],
31
+ ),
32
+ )
33
+
34
+ fig = go.Figure(sankey)
35
+ if title:
36
+ fig.update_layout(
37
+ title_text=title, font=dict(color=style.font_color, size=style.font_size)
38
+ )
39
+ return fig
@@ -306,4 +306,9 @@ PIVOTLINES_STYLE_TEMPLATE = StyleTemplate(
306
306
  )
307
307
 
308
308
  NETWORK_STYLE_TEMPLATE = StyleTemplate()
309
+ DISTRIBUTION_STYLE_TEMPLATE = StyleTemplate(palette="magma")
310
+ HEATMAP_STYLE_TEMPLATE = StyleTemplate(palette="viridis")
311
+ AREA_STYLE_TEMPLATE = StyleTemplate(palette="crest")
312
+ PIE_STYLE_TEMPLATE = StyleTemplate(palette="Set2")
313
+ SANKEY_STYLE_TEMPLATE = StyleTemplate()
309
314
  # endregion
@@ -0,0 +1,83 @@
1
+ """Sunburst chart plotting utilities."""
2
+
3
+ from typing import Optional
4
+
5
+ import pandas as pd
6
+ import plotly.graph_objects as go
7
+
8
+ from .StyleTemplate import (
9
+ TREEMAP_STYLE_TEMPLATE,
10
+ StyleTemplate,
11
+ validate_dataframe,
12
+ )
13
+
14
+
15
+ def fplot_sunburst(
16
+ pd_df: pd.DataFrame,
17
+ labels: str,
18
+ parents: str,
19
+ values: str,
20
+ style: StyleTemplate = TREEMAP_STYLE_TEMPLATE,
21
+ title: Optional[str] = None,
22
+ sort_by: Optional[str] = None,
23
+ ascending: bool = False,
24
+ max_values: int = 100,
25
+ fig: Optional[go.Figure] = None,
26
+ ) -> go.Figure:
27
+ """Return a figure containing the sunburst plot.
28
+
29
+ Parameters
30
+ ----------
31
+ pd_df : pd.DataFrame
32
+ DataFrame containing the data to plot.
33
+ labels : str
34
+ Column representing the labels of the sectors.
35
+ parents : str
36
+ Column representing the parent of each sector.
37
+ values : str
38
+ Column containing values for each sunburst sector.
39
+ style : StyleTemplate, optional
40
+ Style configuration. The default is `TREEMAP_STYLE_TEMPLATE`.
41
+ title : str, optional
42
+ Plot title.
43
+ sort_by : str, optional
44
+ Column used to sort data.
45
+ ascending : bool, optional
46
+ Sort order for the data. The default is `False`.
47
+ max_values : int, optional
48
+ Maximum number of rows to plot. The default is 100.
49
+ fig : go.Figure, optional
50
+ Existing figure to add the sunburst chart to.
51
+
52
+ Returns
53
+ -------
54
+ go.Figure
55
+ Figure containing the sunburst plot.
56
+ """
57
+ cols = [labels, parents, values]
58
+ validate_dataframe(pd_df, cols=cols, sort_by=sort_by)
59
+ if not sort_by:
60
+ sort_by = values
61
+ df = pd_df.sort_values(by=sort_by, ascending=ascending)[cols].head(max_values)
62
+
63
+ trace = go.Sunburst(
64
+ labels=df[labels],
65
+ parents=df[parents],
66
+ values=df[values],
67
+ textinfo="label+percent entry",
68
+ )
69
+
70
+ if not fig:
71
+ fig = go.Figure(trace)
72
+ else:
73
+ fig.add_trace(trace)
74
+
75
+ fig.update_layout(
76
+ title=title,
77
+ plot_bgcolor=style.background_color,
78
+ paper_bgcolor=style.background_color,
79
+ font=dict(family=style.font_name, size=style.font_size, color=style.font_color),
80
+ showlegend=style.legend if style else True,
81
+ )
82
+
83
+ return fig
@@ -0,0 +1,82 @@
1
+ """Waffle 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
+ from matplotlib.patches import Rectangle
10
+ import matplotlib.pyplot as plt
11
+
12
+ from .StyleTemplate import PIE_STYLE_TEMPLATE, StyleTemplate, validate_dataframe
13
+ from ._visualization_utils import _get_axis, _wrap_aplot
14
+
15
+
16
+ def aplot_waffle(
17
+ pd_df: pd.DataFrame,
18
+ category: str,
19
+ value: str,
20
+ rows: int = 10,
21
+ title: Optional[str] = None,
22
+ style: StyleTemplate = PIE_STYLE_TEMPLATE,
23
+ ax: Optional[Axes] = None,
24
+ **kwargs: Any,
25
+ ) -> Axes:
26
+ """Plot a simple waffle chart as a grid of proportional squares."""
27
+ validate_dataframe(pd_df, cols=[category, value])
28
+ plot_ax = _get_axis(ax)
29
+ total = float(pd_df[value].sum())
30
+ squares = rows * rows
31
+ colors = sns.color_palette(style.palette, n_colors=len(pd_df))
32
+ plot_ax.set_aspect("equal")
33
+
34
+ start = 0
35
+ for idx, (label, val) in enumerate(zip(pd_df[category], pd_df[value])):
36
+ count = int(round((val / total) * squares))
37
+ for square in range(start, min(start + count, squares)):
38
+ row = square // rows
39
+ col = square % rows
40
+ plot_ax.add_patch(
41
+ Rectangle(
42
+ (col, rows - row),
43
+ 1,
44
+ 1,
45
+ facecolor=colors[idx],
46
+ edgecolor=style.background_color,
47
+ )
48
+ )
49
+ start += count
50
+
51
+ plot_ax.set_xlim(0, rows)
52
+ plot_ax.set_ylim(0, rows + 1)
53
+ plot_ax.axis("off")
54
+ if title:
55
+ plot_ax.set_title(title)
56
+ legend_handles = [Rectangle((0, 0), 1, 1, color=color) for color in colors]
57
+ plot_ax.legend(
58
+ legend_handles, pd_df[category], loc="upper center", ncol=3, frameon=False
59
+ )
60
+ return plot_ax
61
+
62
+
63
+ def fplot_waffle(
64
+ pd_df: pd.DataFrame,
65
+ category: str,
66
+ value: str,
67
+ rows: int = 10,
68
+ title: Optional[str] = None,
69
+ style: StyleTemplate = PIE_STYLE_TEMPLATE,
70
+ figsize: Tuple[float, float] = (8, 8),
71
+ ) -> Figure:
72
+ """Plot waffle charts on a new figure."""
73
+ return _wrap_aplot(
74
+ aplot_waffle,
75
+ pd_df=pd_df,
76
+ figsize=figsize,
77
+ category=category,
78
+ value=value,
79
+ rows=rows,
80
+ title=title,
81
+ style=style,
82
+ )