MatplotLibAPI 3.2.21__py3-none-any.whl → 4.0.0__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/__init__.py +4 -86
- MatplotLibAPI/accessor.py +519 -196
- MatplotLibAPI/area.py +177 -0
- MatplotLibAPI/bar.py +185 -0
- MatplotLibAPI/base_plot.py +88 -0
- MatplotLibAPI/box_violin.py +180 -0
- MatplotLibAPI/bubble.py +568 -0
- MatplotLibAPI/{Composite.py → composite.py} +127 -106
- MatplotLibAPI/heatmap.py +223 -0
- MatplotLibAPI/histogram.py +170 -0
- MatplotLibAPI/mcp/__init__.py +17 -0
- MatplotLibAPI/mcp/metadata.py +90 -0
- MatplotLibAPI/mcp/renderers.py +45 -0
- MatplotLibAPI/mcp_server.py +626 -0
- MatplotLibAPI/network/__init__.py +28 -0
- MatplotLibAPI/network/constants.py +22 -0
- MatplotLibAPI/network/core.py +1360 -0
- MatplotLibAPI/network/plot.py +597 -0
- MatplotLibAPI/network/scaling.py +56 -0
- MatplotLibAPI/pie.py +154 -0
- MatplotLibAPI/pivot.py +274 -0
- MatplotLibAPI/sankey.py +99 -0
- MatplotLibAPI/{StyleTemplate.py → style_template.py} +27 -22
- MatplotLibAPI/sunburst.py +139 -0
- MatplotLibAPI/{Table.py → table.py} +112 -87
- MatplotLibAPI/{Timeserie.py → timeserie.py} +98 -42
- MatplotLibAPI/{Treemap.py → treemap.py} +43 -55
- MatplotLibAPI/typing.py +12 -0
- MatplotLibAPI/{_visualization_utils.py → utils.py} +7 -13
- MatplotLibAPI/waffle.py +173 -0
- MatplotLibAPI/word_cloud.py +489 -0
- {matplotlibapi-3.2.21.dist-info → matplotlibapi-4.0.0.dist-info}/METADATA +98 -9
- matplotlibapi-4.0.0.dist-info/RECORD +36 -0
- {matplotlibapi-3.2.21.dist-info → matplotlibapi-4.0.0.dist-info}/WHEEL +1 -1
- matplotlibapi-4.0.0.dist-info/entry_points.txt +2 -0
- MatplotLibAPI/Area.py +0 -80
- MatplotLibAPI/Bar.py +0 -83
- MatplotLibAPI/BoxViolin.py +0 -75
- MatplotLibAPI/Bubble.py +0 -458
- MatplotLibAPI/Heatmap.py +0 -121
- MatplotLibAPI/Histogram.py +0 -73
- MatplotLibAPI/Network.py +0 -989
- MatplotLibAPI/Pie.py +0 -70
- MatplotLibAPI/Pivot.py +0 -134
- MatplotLibAPI/Sankey.py +0 -46
- MatplotLibAPI/Sunburst.py +0 -89
- MatplotLibAPI/Waffle.py +0 -86
- MatplotLibAPI/Wordcloud.py +0 -373
- MatplotLibAPI/_typing.py +0 -17
- matplotlibapi-3.2.21.dist-info/RECORD +0 -26
- {matplotlibapi-3.2.21.dist-info → matplotlibapi-4.0.0.dist-info}/licenses/LICENSE +0 -0
MatplotLibAPI/Area.py
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
"""Area chart helpers."""
|
|
2
|
-
|
|
3
|
-
from typing import Any, Dict, 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
|
-
save_path: Optional[str] = None,
|
|
65
|
-
savefig_kwargs: Optional[Dict[str, Any]] = None,
|
|
66
|
-
) -> Figure:
|
|
67
|
-
"""Plot area charts on a new figure."""
|
|
68
|
-
return _wrap_aplot(
|
|
69
|
-
aplot_area,
|
|
70
|
-
pd_df=pd_df,
|
|
71
|
-
figsize=figsize,
|
|
72
|
-
x=x,
|
|
73
|
-
y=y,
|
|
74
|
-
label=label,
|
|
75
|
-
stacked=stacked,
|
|
76
|
-
title=title,
|
|
77
|
-
style=style,
|
|
78
|
-
save_path=save_path,
|
|
79
|
-
savefig_kwargs=savefig_kwargs,
|
|
80
|
-
)
|
MatplotLibAPI/Bar.py
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"""Bar and stacked bar chart helpers."""
|
|
2
|
-
|
|
3
|
-
from typing import Any, Dict, 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
|
-
save_path: Optional[str] = None,
|
|
68
|
-
savefig_kwargs: Optional[Dict[str, Any]] = None,
|
|
69
|
-
) -> Figure:
|
|
70
|
-
"""Plot bar or stacked bar charts on a new figure."""
|
|
71
|
-
return _wrap_aplot(
|
|
72
|
-
aplot_bar,
|
|
73
|
-
pd_df=pd_df,
|
|
74
|
-
figsize=figsize,
|
|
75
|
-
category=category,
|
|
76
|
-
value=value,
|
|
77
|
-
group=group,
|
|
78
|
-
stacked=stacked,
|
|
79
|
-
title=title,
|
|
80
|
-
style=style,
|
|
81
|
-
save_path=save_path,
|
|
82
|
-
savefig_kwargs=savefig_kwargs,
|
|
83
|
-
)
|
MatplotLibAPI/BoxViolin.py
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
"""Box and violin plot helpers."""
|
|
2
|
-
|
|
3
|
-
from typing import Any, Dict, 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
|
-
common_kwargs = {"data": pd_df, "x": by, "y": column, "palette": style.palette}
|
|
37
|
-
|
|
38
|
-
if violin:
|
|
39
|
-
sns.violinplot(**common_kwargs, hue=by, legend=False, ax=plot_ax)
|
|
40
|
-
else:
|
|
41
|
-
sns.boxplot(**common_kwargs, hue=by, legend=False, ax=plot_ax)
|
|
42
|
-
|
|
43
|
-
plot_ax.set_facecolor(style.background_color)
|
|
44
|
-
plot_ax.set_ylabel(string_formatter(column))
|
|
45
|
-
if by:
|
|
46
|
-
plot_ax.set_xlabel(string_formatter(by))
|
|
47
|
-
if title:
|
|
48
|
-
plot_ax.set_title(title)
|
|
49
|
-
return plot_ax
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
def fplot_box_violin(
|
|
53
|
-
pd_df: pd.DataFrame,
|
|
54
|
-
column: str,
|
|
55
|
-
by: Optional[str] = None,
|
|
56
|
-
violin: bool = False,
|
|
57
|
-
title: Optional[str] = None,
|
|
58
|
-
style: StyleTemplate = DISTRIBUTION_STYLE_TEMPLATE,
|
|
59
|
-
figsize: Tuple[float, float] = (10, 6),
|
|
60
|
-
save_path: Optional[str] = None,
|
|
61
|
-
savefig_kwargs: Optional[Dict[str, Any]] = None,
|
|
62
|
-
) -> Figure:
|
|
63
|
-
"""Plot box or violin charts on a new figure."""
|
|
64
|
-
return _wrap_aplot(
|
|
65
|
-
aplot_box_violin,
|
|
66
|
-
pd_df=pd_df,
|
|
67
|
-
figsize=figsize,
|
|
68
|
-
column=column,
|
|
69
|
-
by=by,
|
|
70
|
-
violin=violin,
|
|
71
|
-
title=title,
|
|
72
|
-
style=style,
|
|
73
|
-
save_path=save_path,
|
|
74
|
-
savefig_kwargs=savefig_kwargs,
|
|
75
|
-
)
|
MatplotLibAPI/Bubble.py
DELETED
|
@@ -1,458 +0,0 @@
|
|
|
1
|
-
"""Bubble chart plotting helpers.
|
|
2
|
-
|
|
3
|
-
Provides functions to create and render bubble charts using seaborn and matplotlib,
|
|
4
|
-
with customizable styling via `StyleTemplate`.
|
|
5
|
-
"""
|
|
6
|
-
|
|
7
|
-
from typing import Any, Dict, Optional, Tuple, cast
|
|
8
|
-
|
|
9
|
-
import matplotlib.pyplot as plt
|
|
10
|
-
import pandas as pd
|
|
11
|
-
import seaborn as sns
|
|
12
|
-
from matplotlib.axes import Axes
|
|
13
|
-
from matplotlib.figure import Figure
|
|
14
|
-
from matplotlib.ticker import NullLocator
|
|
15
|
-
|
|
16
|
-
from .StyleTemplate import (
|
|
17
|
-
BUBBLE_STYLE_TEMPLATE,
|
|
18
|
-
FIG_SIZE,
|
|
19
|
-
MAX_RESULTS,
|
|
20
|
-
StyleTemplate,
|
|
21
|
-
bmk_formatter,
|
|
22
|
-
format_func,
|
|
23
|
-
generate_ticks,
|
|
24
|
-
percent_formatter,
|
|
25
|
-
string_formatter,
|
|
26
|
-
validate_dataframe,
|
|
27
|
-
DynamicFuncFormatter,
|
|
28
|
-
FormatterFunc,
|
|
29
|
-
)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
def _prepare_bubble_data(
|
|
33
|
-
pd_df: pd.DataFrame,
|
|
34
|
-
label: str,
|
|
35
|
-
x: str,
|
|
36
|
-
y: str,
|
|
37
|
-
z: str,
|
|
38
|
-
sort_by: Optional[str],
|
|
39
|
-
ascending: bool,
|
|
40
|
-
max_values: int,
|
|
41
|
-
center_to_mean: bool,
|
|
42
|
-
style: StyleTemplate,
|
|
43
|
-
) -> pd.DataFrame:
|
|
44
|
-
"""Prepare data for bubble chart.
|
|
45
|
-
|
|
46
|
-
Parameters
|
|
47
|
-
----------
|
|
48
|
-
pd_df : pd.DataFrame
|
|
49
|
-
Input DataFrame.
|
|
50
|
-
label : str
|
|
51
|
-
Column name for bubble labels.
|
|
52
|
-
x : str
|
|
53
|
-
Column name for x-axis values.
|
|
54
|
-
y : str
|
|
55
|
-
Column name for y-axis values.
|
|
56
|
-
z : str
|
|
57
|
-
Column name for bubble sizes.
|
|
58
|
-
sort_by : Optional[str]
|
|
59
|
-
Column to sort by.
|
|
60
|
-
ascending : bool
|
|
61
|
-
Sort order.
|
|
62
|
-
max_values : int
|
|
63
|
-
Maximum number of bubbles to display.
|
|
64
|
-
center_to_mean : bool
|
|
65
|
-
Whether to center x-axis values around the mean.
|
|
66
|
-
style : StyleTemplate
|
|
67
|
-
Styling for the plot.
|
|
68
|
-
|
|
69
|
-
Returns
|
|
70
|
-
-------
|
|
71
|
-
pd.DataFrame
|
|
72
|
-
Prepared DataFrame for plotting.
|
|
73
|
-
|
|
74
|
-
Raises
|
|
75
|
-
------
|
|
76
|
-
AttributeError
|
|
77
|
-
If required columns are missing from the DataFrame.
|
|
78
|
-
"""
|
|
79
|
-
validate_dataframe(pd_df, cols=[label, x, y, z], sort_by=sort_by)
|
|
80
|
-
sort_col = sort_by or z
|
|
81
|
-
|
|
82
|
-
plot_df = (
|
|
83
|
-
pd_df[[label, x, y, z]]
|
|
84
|
-
.sort_values(by=[sort_col], ascending=ascending) # type: ignore
|
|
85
|
-
.head(max_values)
|
|
86
|
-
.copy()
|
|
87
|
-
)
|
|
88
|
-
|
|
89
|
-
if center_to_mean:
|
|
90
|
-
plot_df[x] -= plot_df[x].mean()
|
|
91
|
-
|
|
92
|
-
plot_df["quintile"] = pd.qcut(plot_df[z], 5, labels=False, duplicates="drop")
|
|
93
|
-
plot_df["fontsize"] = plot_df["quintile"].map(style.font_mapping) # type: ignore
|
|
94
|
-
return plot_df
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
def _setup_bubble_axes(
|
|
98
|
-
ax: Axes,
|
|
99
|
-
style: StyleTemplate,
|
|
100
|
-
pd_df: pd.DataFrame,
|
|
101
|
-
x: str,
|
|
102
|
-
y: str,
|
|
103
|
-
format_funcs: Optional[Dict[str, Optional[FormatterFunc]]],
|
|
104
|
-
) -> None:
|
|
105
|
-
"""Configure axes for the bubble chart.
|
|
106
|
-
|
|
107
|
-
Parameters
|
|
108
|
-
----------
|
|
109
|
-
ax : Axes
|
|
110
|
-
Matplotlib axes object.
|
|
111
|
-
style : StyleTemplate
|
|
112
|
-
Styling for the plot.
|
|
113
|
-
pd_df : pd.DataFrame
|
|
114
|
-
DataFrame used for plotting.
|
|
115
|
-
x : str
|
|
116
|
-
Column name for x-axis values.
|
|
117
|
-
y : str
|
|
118
|
-
Column name for y-axis values.
|
|
119
|
-
format_funcs : Optional[Dict[str, Optional[FormatterFunc]]]
|
|
120
|
-
Functions to format axis tick labels.
|
|
121
|
-
"""
|
|
122
|
-
ax.set_facecolor(style.background_color)
|
|
123
|
-
|
|
124
|
-
if style.xscale:
|
|
125
|
-
ax.set(xscale=style.xscale)
|
|
126
|
-
if style.yscale:
|
|
127
|
-
ax.set(yscale=style.yscale)
|
|
128
|
-
|
|
129
|
-
# X-axis ticks and formatting
|
|
130
|
-
x_min, x_max = cast(float, pd_df[x].min()), cast(float, pd_df[x].max())
|
|
131
|
-
ax.xaxis.set_ticks(generate_ticks(x_min, x_max, num_ticks=style.x_ticks))
|
|
132
|
-
ax.xaxis.grid(True, "major", linewidth=0.5, color=style.font_color)
|
|
133
|
-
if format_funcs and (fmt_x := format_funcs.get(x)):
|
|
134
|
-
ax.xaxis.set_major_formatter(DynamicFuncFormatter(fmt_x))
|
|
135
|
-
|
|
136
|
-
# Y-axis ticks and formatting
|
|
137
|
-
y_min, y_max = cast(float, pd_df[y].min()), cast(float, pd_df[y].max())
|
|
138
|
-
ax.yaxis.set_ticks(generate_ticks(y_min, y_max, num_ticks=style.y_ticks))
|
|
139
|
-
if style.yscale == "log":
|
|
140
|
-
ax.yaxis.set_minor_locator(NullLocator())
|
|
141
|
-
else:
|
|
142
|
-
ax.minorticks_off()
|
|
143
|
-
ax.yaxis.grid(True, "major", linewidth=0.5, color=style.font_color)
|
|
144
|
-
if format_funcs and (fmt_y := format_funcs.get(y)):
|
|
145
|
-
ax.yaxis.set_major_formatter(DynamicFuncFormatter(fmt_y))
|
|
146
|
-
|
|
147
|
-
ax.tick_params(
|
|
148
|
-
axis="both", which="major", colors=style.font_color, labelsize=style.font_size
|
|
149
|
-
)
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
def _draw_bubbles(
|
|
153
|
-
ax: Axes, plot_df: pd.DataFrame, x: str, y: str, z: str, style: StyleTemplate
|
|
154
|
-
) -> None:
|
|
155
|
-
"""Draw bubbles on the axes.
|
|
156
|
-
|
|
157
|
-
Parameters
|
|
158
|
-
----------
|
|
159
|
-
ax : Axes
|
|
160
|
-
Matplotlib axes object.
|
|
161
|
-
plot_df : pd.DataFrame
|
|
162
|
-
DataFrame with data for plotting.
|
|
163
|
-
x : str
|
|
164
|
-
Column name for x-axis values.
|
|
165
|
-
y : str
|
|
166
|
-
Column name for y-axis values.
|
|
167
|
-
z : str
|
|
168
|
-
Column name for bubble sizes.
|
|
169
|
-
style : StyleTemplate
|
|
170
|
-
Styling for the plot.
|
|
171
|
-
"""
|
|
172
|
-
sns.scatterplot(
|
|
173
|
-
data=plot_df,
|
|
174
|
-
x=x,
|
|
175
|
-
y=y,
|
|
176
|
-
size=z,
|
|
177
|
-
hue="quintile",
|
|
178
|
-
sizes=(100, 2000),
|
|
179
|
-
legend=False,
|
|
180
|
-
palette=sns.color_palette(style.palette, as_cmap=True),
|
|
181
|
-
edgecolor=style.background_color,
|
|
182
|
-
ax=ax,
|
|
183
|
-
)
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
def _draw_bubble_labels(
|
|
187
|
-
ax: Axes,
|
|
188
|
-
plot_df: pd.DataFrame,
|
|
189
|
-
label: str,
|
|
190
|
-
x: str,
|
|
191
|
-
y: str,
|
|
192
|
-
style: StyleTemplate,
|
|
193
|
-
format_funcs: Optional[Dict[str, Optional[FormatterFunc]]],
|
|
194
|
-
) -> None:
|
|
195
|
-
"""Draw labels for each bubble.
|
|
196
|
-
|
|
197
|
-
Parameters
|
|
198
|
-
----------
|
|
199
|
-
ax : Axes
|
|
200
|
-
Matplotlib axes object.
|
|
201
|
-
plot_df : pd.DataFrame
|
|
202
|
-
DataFrame with data for plotting.
|
|
203
|
-
label : str
|
|
204
|
-
Column name for bubble labels.
|
|
205
|
-
x : str
|
|
206
|
-
Column name for x-axis values.
|
|
207
|
-
y : str
|
|
208
|
-
Column name for y-axis values.
|
|
209
|
-
style : StyleTemplate
|
|
210
|
-
Styling for the plot.
|
|
211
|
-
format_funcs : Optional[Dict[str, Optional[FormatterFunc]]]
|
|
212
|
-
Functions to format bubble labels.
|
|
213
|
-
"""
|
|
214
|
-
for _, row in plot_df.iterrows():
|
|
215
|
-
x_val, y_val, label_val = row[x], row[y], str(row[label])
|
|
216
|
-
if format_funcs and (fmt_label := format_funcs.get(label)):
|
|
217
|
-
label_val = fmt_label(label_val, None)
|
|
218
|
-
ax.text(
|
|
219
|
-
cast(float, x_val),
|
|
220
|
-
cast(float, y_val),
|
|
221
|
-
label_val,
|
|
222
|
-
ha="center",
|
|
223
|
-
fontsize=row["fontsize"],
|
|
224
|
-
color=style.font_color,
|
|
225
|
-
)
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
def _draw_mean_lines(
|
|
229
|
-
ax: Axes,
|
|
230
|
-
plot_df: pd.DataFrame,
|
|
231
|
-
x: str,
|
|
232
|
-
y: str,
|
|
233
|
-
hline: bool,
|
|
234
|
-
vline: bool,
|
|
235
|
-
style: StyleTemplate,
|
|
236
|
-
) -> None:
|
|
237
|
-
"""Draw horizontal and vertical mean lines.
|
|
238
|
-
|
|
239
|
-
Parameters
|
|
240
|
-
----------
|
|
241
|
-
ax : Axes
|
|
242
|
-
Matplotlib axes object.
|
|
243
|
-
plot_df : pd.DataFrame
|
|
244
|
-
DataFrame with data for plotting.
|
|
245
|
-
x : str
|
|
246
|
-
Column name for x-axis values.
|
|
247
|
-
y : str
|
|
248
|
-
Column name for y-axis values.
|
|
249
|
-
hline : bool
|
|
250
|
-
Whether to draw a horizontal line at the mean of y.
|
|
251
|
-
vline : bool
|
|
252
|
-
Whether to draw a vertical line at the mean of x.
|
|
253
|
-
style : StyleTemplate
|
|
254
|
-
Styling for the plot.
|
|
255
|
-
"""
|
|
256
|
-
if vline:
|
|
257
|
-
ax.axvline(
|
|
258
|
-
int(cast(float, plot_df[x].mean())), linestyle="--", color=style.font_color
|
|
259
|
-
)
|
|
260
|
-
if hline:
|
|
261
|
-
ax.axhline(
|
|
262
|
-
int(cast(float, plot_df[y].mean())), linestyle="--", color=style.font_color
|
|
263
|
-
)
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
def aplot_bubble(
|
|
267
|
-
pd_df: pd.DataFrame,
|
|
268
|
-
label: str,
|
|
269
|
-
x: str,
|
|
270
|
-
y: str,
|
|
271
|
-
z: str,
|
|
272
|
-
title: Optional[str] = None,
|
|
273
|
-
style: StyleTemplate = BUBBLE_STYLE_TEMPLATE,
|
|
274
|
-
max_values: int = MAX_RESULTS,
|
|
275
|
-
center_to_mean: bool = False,
|
|
276
|
-
sort_by: Optional[str] = None,
|
|
277
|
-
ascending: bool = False,
|
|
278
|
-
hline: bool = False,
|
|
279
|
-
vline: bool = False,
|
|
280
|
-
ax: Optional[Axes] = None,
|
|
281
|
-
) -> Axes:
|
|
282
|
-
"""Plot a bubble chart onto the given axes.
|
|
283
|
-
|
|
284
|
-
Parameters
|
|
285
|
-
----------
|
|
286
|
-
pd_df : pd.DataFrame
|
|
287
|
-
DataFrame containing the data to plot.
|
|
288
|
-
label : str
|
|
289
|
-
Column name used for labeling bubbles.
|
|
290
|
-
x : str
|
|
291
|
-
Column name for x-axis values.
|
|
292
|
-
y : str
|
|
293
|
-
Column name for y-axis values.
|
|
294
|
-
z : str
|
|
295
|
-
Column name for bubble sizes.
|
|
296
|
-
title : str, optional
|
|
297
|
-
Plot title.
|
|
298
|
-
style : StyleTemplate, optional
|
|
299
|
-
Plot styling options. The default is `BUBBLE_STYLE_TEMPLATE`.
|
|
300
|
-
max_values : int, optional
|
|
301
|
-
Max number of rows to display. The default is `MAX_RESULTS`.
|
|
302
|
-
center_to_mean : bool, optional
|
|
303
|
-
Whether to center x values around their mean. The default is `False`.
|
|
304
|
-
sort_by : str, optional
|
|
305
|
-
Column to sort by before slicing.
|
|
306
|
-
ascending : bool, optional
|
|
307
|
-
Sort order. The default is `False`.
|
|
308
|
-
hline : bool, optional
|
|
309
|
-
Whether to draw a horizontal line at the mean of y. The default is `False`.
|
|
310
|
-
vline : bool, optional
|
|
311
|
-
Whether to draw a vertical line at the mean of x. The default is `False`.
|
|
312
|
-
ax : Axes, optional
|
|
313
|
-
Existing matplotlib axes to use. If None, uses current axes.
|
|
314
|
-
|
|
315
|
-
Returns
|
|
316
|
-
-------
|
|
317
|
-
Axes
|
|
318
|
-
The matplotlib Axes object containing the bubble chart.
|
|
319
|
-
|
|
320
|
-
Raises
|
|
321
|
-
------
|
|
322
|
-
AttributeError
|
|
323
|
-
If required columns are not in the DataFrame.
|
|
324
|
-
|
|
325
|
-
Examples
|
|
326
|
-
--------
|
|
327
|
-
>>> import pandas as pd
|
|
328
|
-
>>> import matplotlib.pyplot as plt
|
|
329
|
-
>>> from MatplotLibAPI.Bubble import aplot_bubble
|
|
330
|
-
>>> data = {
|
|
331
|
-
... 'country': ['A', 'B', 'C', 'D'],
|
|
332
|
-
... 'gdp_per_capita': [45000, 42000, 52000, 48000],
|
|
333
|
-
... 'life_expectancy': [81, 78, 83, 82],
|
|
334
|
-
... 'population': [10, 20, 5, 30]
|
|
335
|
-
... }
|
|
336
|
-
>>> df = pd.DataFrame(data)
|
|
337
|
-
>>> fig, ax = plt.subplots()
|
|
338
|
-
>>> aplot_bubble(df, label='country', x='gdp_per_capita', y='life_expectancy', z='population', ax=ax)
|
|
339
|
-
"""
|
|
340
|
-
if ax is None:
|
|
341
|
-
ax = cast(Axes, plt.gca())
|
|
342
|
-
|
|
343
|
-
plot_df = _prepare_bubble_data(
|
|
344
|
-
pd_df, label, x, y, z, sort_by, ascending, max_values, center_to_mean, style
|
|
345
|
-
)
|
|
346
|
-
|
|
347
|
-
format_funcs = format_func(style.format_funcs, label=label, x=x, y=y, z=z)
|
|
348
|
-
|
|
349
|
-
_setup_bubble_axes(ax, style, plot_df, x, y, format_funcs)
|
|
350
|
-
|
|
351
|
-
_draw_bubbles(ax, plot_df, x, y, z, style)
|
|
352
|
-
|
|
353
|
-
_draw_mean_lines(ax, plot_df, x, y, hline, vline, style)
|
|
354
|
-
|
|
355
|
-
_draw_bubble_labels(ax, plot_df, label, x, y, style, format_funcs)
|
|
356
|
-
|
|
357
|
-
if title:
|
|
358
|
-
ax.set_title(title, color=style.font_color, fontsize=style.font_size * 2)
|
|
359
|
-
|
|
360
|
-
return ax
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
def fplot_bubble(
|
|
364
|
-
pd_df: pd.DataFrame,
|
|
365
|
-
label: str,
|
|
366
|
-
x: str,
|
|
367
|
-
y: str,
|
|
368
|
-
z: str,
|
|
369
|
-
title: Optional[str] = None,
|
|
370
|
-
style: StyleTemplate = BUBBLE_STYLE_TEMPLATE,
|
|
371
|
-
max_values: int = MAX_RESULTS,
|
|
372
|
-
center_to_mean: bool = False,
|
|
373
|
-
sort_by: Optional[str] = None,
|
|
374
|
-
ascending: bool = False,
|
|
375
|
-
hline: bool = False,
|
|
376
|
-
vline: bool = False,
|
|
377
|
-
figsize: Tuple[float, float] = FIG_SIZE,
|
|
378
|
-
save_path: Optional[str] = None,
|
|
379
|
-
savefig_kwargs: Optional[Dict[str, Any]] = None,
|
|
380
|
-
) -> Figure:
|
|
381
|
-
"""Create a new matplotlib Figure with a bubble chart.
|
|
382
|
-
|
|
383
|
-
Parameters
|
|
384
|
-
----------
|
|
385
|
-
pd_df : pd.DataFrame
|
|
386
|
-
DataFrame containing the data to plot.
|
|
387
|
-
label : str
|
|
388
|
-
Column name for bubble labels.
|
|
389
|
-
x : str
|
|
390
|
-
Column name for x-axis values.
|
|
391
|
-
y : str
|
|
392
|
-
Column name for y-axis values.
|
|
393
|
-
z : str
|
|
394
|
-
Column name for bubble sizes.
|
|
395
|
-
title : str, optional
|
|
396
|
-
Title for the chart. The default is ``None``.
|
|
397
|
-
style : StyleTemplate, optional
|
|
398
|
-
Plot styling. The default is `BUBBLE_STYLE_TEMPLATE`.
|
|
399
|
-
max_values : int, optional
|
|
400
|
-
Max number of rows to display. The default is `MAX_RESULTS`.
|
|
401
|
-
center_to_mean : bool, optional
|
|
402
|
-
Whether to center x around its mean. The default is `False`.
|
|
403
|
-
sort_by : str, optional
|
|
404
|
-
Column to sort by.
|
|
405
|
-
ascending : bool, optional
|
|
406
|
-
Sort order. The default is `False`.
|
|
407
|
-
hline : bool, optional
|
|
408
|
-
Draw horizontal line at mean y. The default is `False`.
|
|
409
|
-
vline : bool, optional
|
|
410
|
-
Draw vertical line at mean x. The default is `False`.
|
|
411
|
-
figsize : tuple[float, float], optional
|
|
412
|
-
Size of the figure. The default is FIG_SIZE.
|
|
413
|
-
|
|
414
|
-
Returns
|
|
415
|
-
-------
|
|
416
|
-
Figure
|
|
417
|
-
A matplotlib Figure object containing the bubble chart.
|
|
418
|
-
|
|
419
|
-
Raises
|
|
420
|
-
------
|
|
421
|
-
AttributeError
|
|
422
|
-
If required columns are not in the DataFrame.
|
|
423
|
-
|
|
424
|
-
Examples
|
|
425
|
-
--------
|
|
426
|
-
>>> import pandas as pd
|
|
427
|
-
>>> from MatplotLibAPI.Bubble import fplot_bubble
|
|
428
|
-
>>> data = {
|
|
429
|
-
... 'country': ['A', 'B', 'C', 'D'],
|
|
430
|
-
... 'gdp_per_capita': [45000, 42000, 52000, 48000],
|
|
431
|
-
... 'life_expectancy': [81, 78, 83, 82],
|
|
432
|
-
... 'population': [10, 20, 5, 30]
|
|
433
|
-
... }
|
|
434
|
-
>>> df = pd.DataFrame(data)
|
|
435
|
-
>>> fig = fplot_bubble(df, label='country', x='gdp_per_capita', y='life_expectancy', z='population')
|
|
436
|
-
"""
|
|
437
|
-
fig = cast(Figure, plt.figure(figsize=figsize))
|
|
438
|
-
fig.patch.set_facecolor(style.background_color)
|
|
439
|
-
ax = fig.add_subplot()
|
|
440
|
-
aplot_bubble(
|
|
441
|
-
pd_df=pd_df,
|
|
442
|
-
label=label,
|
|
443
|
-
x=x,
|
|
444
|
-
y=y,
|
|
445
|
-
z=z,
|
|
446
|
-
title=title,
|
|
447
|
-
style=style,
|
|
448
|
-
max_values=max_values,
|
|
449
|
-
center_to_mean=center_to_mean,
|
|
450
|
-
sort_by=sort_by,
|
|
451
|
-
ascending=ascending,
|
|
452
|
-
hline=hline,
|
|
453
|
-
vline=vline,
|
|
454
|
-
ax=ax,
|
|
455
|
-
)
|
|
456
|
-
if save_path:
|
|
457
|
-
fig.savefig(save_path, **(savefig_kwargs or {}))
|
|
458
|
-
return fig
|