plotify-charts 0.1.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.
- plotify/__init__.py +116 -0
- plotify/_inference.py +144 -0
- plotify/auto.py +360 -0
- plotify/base.py +224 -0
- plotify/categorical/__init__.py +37 -0
- plotify/categorical/bar_plot.py +121 -0
- plotify/categorical/circular_packing.py +107 -0
- plotify/categorical/dendrogram.py +113 -0
- plotify/categorical/doughnut_chart.py +109 -0
- plotify/categorical/lollipop_chart.py +168 -0
- plotify/categorical/parallel_coordinates.py +94 -0
- plotify/categorical/pie_chart.py +101 -0
- plotify/categorical/radar_chart.py +116 -0
- plotify/categorical/sunburst_diagram.py +117 -0
- plotify/categorical/treemap.py +121 -0
- plotify/categorical/venn_diagram.py +92 -0
- plotify/categorical/word_cloud.py +104 -0
- plotify/maps/__init__.py +21 -0
- plotify/maps/bubble_map.py +114 -0
- plotify/maps/cartogram.py +126 -0
- plotify/maps/choropleth_map.py +108 -0
- plotify/maps/connection_map.py +117 -0
- plotify/maps/hexbin_map.py +96 -0
- plotify/network/__init__.py +21 -0
- plotify/network/arc_diagram.py +124 -0
- plotify/network/chord_diagram.py +119 -0
- plotify/network/hierarchical_edge_bundling.py +141 -0
- plotify/network/network_diagram.py +167 -0
- plotify/network/sankey_diagram.py +258 -0
- plotify/num_cat/__init__.py +19 -0
- plotify/num_cat/box_plot_by_group.py +110 -0
- plotify/num_cat/grouped_bar_plot.py +110 -0
- plotify/num_cat/grouped_scatter.py +105 -0
- plotify/num_cat/stacked_bar_plot.py +117 -0
- plotify/numerical/__init__.py +21 -0
- plotify/numerical/boxplot.py +157 -0
- plotify/numerical/connected_scatter_plot.py +120 -0
- plotify/numerical/density_plot.py +184 -0
- plotify/numerical/scatter_plot.py +140 -0
- plotify/numerical/violin_plot.py +141 -0
- plotify/theme.py +297 -0
- plotify/timeseries/__init__.py +13 -0
- plotify/timeseries/area_chart.py +89 -0
- plotify/timeseries/line_chart.py +110 -0
- plotify/timeseries/stacked_area_chart.py +133 -0
- plotify/timeseries/stream_graph.py +116 -0
- plotify_charts-0.1.0.dist-info/LICENSE +21 -0
- plotify_charts-0.1.0.dist-info/METADATA +226 -0
- plotify_charts-0.1.0.dist-info/RECORD +50 -0
- plotify_charts-0.1.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""
|
|
2
|
+
plotify.categorical.doughnut_chart
|
|
3
|
+
==================================
|
|
4
|
+
|
|
5
|
+
Doughnut chart — a pie with a hole.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import matplotlib.pyplot as plt
|
|
9
|
+
import plotly.express as px
|
|
10
|
+
|
|
11
|
+
from plotify.base import BasePlot
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class DoughnutChart(BasePlot):
|
|
15
|
+
"""
|
|
16
|
+
Doughnut chart.
|
|
17
|
+
|
|
18
|
+
Seaborn backend uses :func:`matplotlib.pyplot.pie` with ``wedgeprops`` to
|
|
19
|
+
carve out the centre; Plotly backend uses :func:`plotly.express.pie`
|
|
20
|
+
with ``hole=``.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
df,
|
|
26
|
+
names,
|
|
27
|
+
values,
|
|
28
|
+
hole=0.4,
|
|
29
|
+
colors=None,
|
|
30
|
+
startangle=90,
|
|
31
|
+
autopct="%1.1f%%",
|
|
32
|
+
title=None,
|
|
33
|
+
backend="seaborn",
|
|
34
|
+
**kwargs,
|
|
35
|
+
):
|
|
36
|
+
"""
|
|
37
|
+
This is used to create a doughnut chart.
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
df : pandas.DataFrame
|
|
42
|
+
Input data.
|
|
43
|
+
names : str
|
|
44
|
+
Column with slice labels.
|
|
45
|
+
values : str
|
|
46
|
+
Column with slice values.
|
|
47
|
+
hole : float, default=0.4
|
|
48
|
+
Fraction of the radius to cut out of the centre (0 → full pie,
|
|
49
|
+
1 → no chart at all). Applied in both backends.
|
|
50
|
+
colors : list[str], optional
|
|
51
|
+
Colour for each slice (Seaborn only).
|
|
52
|
+
startangle : float, default=90
|
|
53
|
+
Start angle of the first slice (Seaborn only).
|
|
54
|
+
autopct : str, default="%1.1f%%"
|
|
55
|
+
Value-label format (Seaborn only).
|
|
56
|
+
title : str, optional
|
|
57
|
+
Plot title.
|
|
58
|
+
backend : {"seaborn", "plotly"}, default="seaborn"
|
|
59
|
+
Rendering backend.
|
|
60
|
+
**kwargs :
|
|
61
|
+
Forwarded to the underlying library.
|
|
62
|
+
|
|
63
|
+
Returns
|
|
64
|
+
-------
|
|
65
|
+
None
|
|
66
|
+
"""
|
|
67
|
+
if not 0 <= hole < 1:
|
|
68
|
+
raise ValueError("`hole` must be in [0, 1).")
|
|
69
|
+
|
|
70
|
+
self.__df = df
|
|
71
|
+
self.__names = names
|
|
72
|
+
self.__values = values
|
|
73
|
+
self.__hole = hole
|
|
74
|
+
self.__colors = colors
|
|
75
|
+
self.__startangle = startangle
|
|
76
|
+
self.__autopct = autopct
|
|
77
|
+
self.__kwargs = kwargs
|
|
78
|
+
|
|
79
|
+
super().__init__(backend=backend, title=title)
|
|
80
|
+
self._render()
|
|
81
|
+
|
|
82
|
+
def _plot_seaborn(self):
|
|
83
|
+
"""Render the doughnut using matplotlib's pie + wedgeprops trick."""
|
|
84
|
+
plt.pie(
|
|
85
|
+
self.__df[self.__values],
|
|
86
|
+
labels=self.__df[self.__names],
|
|
87
|
+
colors=self.__colors,
|
|
88
|
+
startangle=self.__startangle,
|
|
89
|
+
autopct=self.__autopct,
|
|
90
|
+
# The wedge-width trick: drawing each wedge with a finite inner
|
|
91
|
+
# radius creates the characteristic doughnut hole.
|
|
92
|
+
wedgeprops=dict(width=1 - self.__hole),
|
|
93
|
+
**self.__kwargs,
|
|
94
|
+
)
|
|
95
|
+
plt.axis("equal")
|
|
96
|
+
if self._title:
|
|
97
|
+
plt.title(self._title)
|
|
98
|
+
|
|
99
|
+
def _plot_plotly(self):
|
|
100
|
+
"""Render using :func:`plotly.express.pie` with ``hole`` set."""
|
|
101
|
+
fig = px.pie(
|
|
102
|
+
self.__df,
|
|
103
|
+
names=self.__names,
|
|
104
|
+
values=self.__values,
|
|
105
|
+
hole=self.__hole,
|
|
106
|
+
title=self._title,
|
|
107
|
+
**self.__kwargs,
|
|
108
|
+
)
|
|
109
|
+
self._fig = fig
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"""
|
|
2
|
+
plotify.categorical.lollipop_chart
|
|
3
|
+
==================================
|
|
4
|
+
|
|
5
|
+
Lollipop chart — a cleaner alternative to bar charts where each bar is
|
|
6
|
+
replaced by a thin stem ending in a dot.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import matplotlib.pyplot as plt
|
|
10
|
+
import plotly.graph_objects as go
|
|
11
|
+
|
|
12
|
+
from plotify.base import BasePlot
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class LollipopChart(BasePlot):
|
|
16
|
+
"""
|
|
17
|
+
Lollipop chart.
|
|
18
|
+
|
|
19
|
+
Parameters ``x`` (category) and ``y`` (numeric value) are required in
|
|
20
|
+
``df``. On the Seaborn backend this is drawn with :func:`matplotlib.pyplot.stem`
|
|
21
|
+
plus a scatter overlay; on the Plotly backend it uses ``go.Scatter``
|
|
22
|
+
markers with per-marker line shapes.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
df,
|
|
28
|
+
x,
|
|
29
|
+
y,
|
|
30
|
+
color=None,
|
|
31
|
+
marker_size=8,
|
|
32
|
+
linewidth=2,
|
|
33
|
+
orient="v",
|
|
34
|
+
title=None,
|
|
35
|
+
backend="seaborn",
|
|
36
|
+
**kwargs,
|
|
37
|
+
):
|
|
38
|
+
"""
|
|
39
|
+
This is used to create a lollipop chart.
|
|
40
|
+
|
|
41
|
+
Parameters
|
|
42
|
+
----------
|
|
43
|
+
df : pandas.DataFrame
|
|
44
|
+
Input data.
|
|
45
|
+
x : str
|
|
46
|
+
Column containing category labels.
|
|
47
|
+
y : str
|
|
48
|
+
Column containing numeric values.
|
|
49
|
+
color : str, optional
|
|
50
|
+
Colour for stems and markers.
|
|
51
|
+
marker_size : int, default=8
|
|
52
|
+
Size of the lollipop head.
|
|
53
|
+
linewidth : int, default=2
|
|
54
|
+
Thickness of the stem.
|
|
55
|
+
orient : {"v", "h"}, default="v"
|
|
56
|
+
Vertical or horizontal orientation.
|
|
57
|
+
title : str, optional
|
|
58
|
+
Plot title.
|
|
59
|
+
backend : {"seaborn", "plotly"}, default="seaborn"
|
|
60
|
+
Rendering backend.
|
|
61
|
+
**kwargs :
|
|
62
|
+
Forwarded to the underlying library.
|
|
63
|
+
|
|
64
|
+
Returns
|
|
65
|
+
-------
|
|
66
|
+
None
|
|
67
|
+
"""
|
|
68
|
+
self.__df = df
|
|
69
|
+
self.__x = x
|
|
70
|
+
self.__y = y
|
|
71
|
+
self.__color = color or "steelblue"
|
|
72
|
+
self.__marker_size = marker_size
|
|
73
|
+
self.__linewidth = linewidth
|
|
74
|
+
self.__orient = orient
|
|
75
|
+
self.__kwargs = kwargs
|
|
76
|
+
|
|
77
|
+
super().__init__(backend=backend, title=title)
|
|
78
|
+
self._render()
|
|
79
|
+
|
|
80
|
+
def _plot_seaborn(self):
|
|
81
|
+
"""Render using matplotlib's ``vlines`` / ``hlines`` + scatter."""
|
|
82
|
+
ax = plt.gca()
|
|
83
|
+
categories = self.__df[self.__x]
|
|
84
|
+
values = self.__df[self.__y]
|
|
85
|
+
|
|
86
|
+
# Draw the stems first, then the markers on top — matches the canonical
|
|
87
|
+
# "lollipop" look.
|
|
88
|
+
if self.__orient == "v":
|
|
89
|
+
ax.vlines(
|
|
90
|
+
x=categories,
|
|
91
|
+
ymin=0,
|
|
92
|
+
ymax=values,
|
|
93
|
+
color=self.__color,
|
|
94
|
+
linewidth=self.__linewidth,
|
|
95
|
+
)
|
|
96
|
+
ax.scatter(
|
|
97
|
+
categories, values, s=self.__marker_size**2, color=self.__color
|
|
98
|
+
)
|
|
99
|
+
else:
|
|
100
|
+
ax.hlines(
|
|
101
|
+
y=categories,
|
|
102
|
+
xmin=0,
|
|
103
|
+
xmax=values,
|
|
104
|
+
color=self.__color,
|
|
105
|
+
linewidth=self.__linewidth,
|
|
106
|
+
)
|
|
107
|
+
ax.scatter(
|
|
108
|
+
values, categories, s=self.__marker_size**2, color=self.__color
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
if self._title:
|
|
112
|
+
ax.set_title(self._title)
|
|
113
|
+
|
|
114
|
+
def _plot_plotly(self):
|
|
115
|
+
"""Render using Plotly ``go.Scatter`` with per-point line shapes."""
|
|
116
|
+
categories = self.__df[self.__x].tolist()
|
|
117
|
+
values = self.__df[self.__y].tolist()
|
|
118
|
+
|
|
119
|
+
fig = go.Figure()
|
|
120
|
+
|
|
121
|
+
# One line shape per category gives us the stem.
|
|
122
|
+
shapes = []
|
|
123
|
+
for cat, val in zip(categories, values):
|
|
124
|
+
if self.__orient == "v":
|
|
125
|
+
shapes.append(
|
|
126
|
+
dict(
|
|
127
|
+
type="line",
|
|
128
|
+
x0=cat,
|
|
129
|
+
x1=cat,
|
|
130
|
+
y0=0,
|
|
131
|
+
y1=val,
|
|
132
|
+
line=dict(color=self.__color, width=self.__linewidth),
|
|
133
|
+
)
|
|
134
|
+
)
|
|
135
|
+
else:
|
|
136
|
+
shapes.append(
|
|
137
|
+
dict(
|
|
138
|
+
type="line",
|
|
139
|
+
y0=cat,
|
|
140
|
+
y1=cat,
|
|
141
|
+
x0=0,
|
|
142
|
+
x1=val,
|
|
143
|
+
line=dict(color=self.__color, width=self.__linewidth),
|
|
144
|
+
)
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
# Scatter carries the lollipop heads.
|
|
148
|
+
if self.__orient == "v":
|
|
149
|
+
fig.add_trace(
|
|
150
|
+
go.Scatter(
|
|
151
|
+
x=categories,
|
|
152
|
+
y=values,
|
|
153
|
+
mode="markers",
|
|
154
|
+
marker=dict(size=self.__marker_size, color=self.__color),
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
else:
|
|
158
|
+
fig.add_trace(
|
|
159
|
+
go.Scatter(
|
|
160
|
+
x=values,
|
|
161
|
+
y=categories,
|
|
162
|
+
mode="markers",
|
|
163
|
+
marker=dict(size=self.__marker_size, color=self.__color),
|
|
164
|
+
)
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
fig.update_layout(shapes=shapes, title=self._title, showlegend=False)
|
|
168
|
+
self._fig = fig
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""
|
|
2
|
+
plotify.categorical.parallel_coordinates
|
|
3
|
+
========================================
|
|
4
|
+
|
|
5
|
+
Parallel coordinates plot — each vertical axis is one variable and each
|
|
6
|
+
polyline is one observation. Good for comparing samples across many
|
|
7
|
+
numeric dimensions at once.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import matplotlib.pyplot as plt
|
|
11
|
+
import pandas.plotting as pdp
|
|
12
|
+
import plotly.express as px
|
|
13
|
+
|
|
14
|
+
from plotify.base import BasePlot
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ParallelCoordinates(BasePlot):
|
|
18
|
+
"""
|
|
19
|
+
Parallel coordinates plot.
|
|
20
|
+
|
|
21
|
+
Wraps :func:`pandas.plotting.parallel_coordinates` on the Seaborn backend
|
|
22
|
+
and :func:`plotly.express.parallel_coordinates` on the Plotly backend.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
df,
|
|
28
|
+
class_column,
|
|
29
|
+
cols=None,
|
|
30
|
+
color=None,
|
|
31
|
+
title=None,
|
|
32
|
+
backend="seaborn",
|
|
33
|
+
**kwargs,
|
|
34
|
+
):
|
|
35
|
+
"""
|
|
36
|
+
This is used to create a parallel coordinates plot.
|
|
37
|
+
|
|
38
|
+
Parameters
|
|
39
|
+
----------
|
|
40
|
+
df : pandas.DataFrame
|
|
41
|
+
Input data.
|
|
42
|
+
class_column : str
|
|
43
|
+
Column name used to group/colour the polylines. On the Plotly
|
|
44
|
+
backend this must be numeric (Plotly uses a continuous colour
|
|
45
|
+
scale); on Seaborn it may be categorical or numeric.
|
|
46
|
+
cols : list[str], optional
|
|
47
|
+
Subset of columns to plot as axes. Defaults to all numeric columns
|
|
48
|
+
other than ``class_column``.
|
|
49
|
+
color : list or str, optional
|
|
50
|
+
Colour specification forwarded to the underlying function.
|
|
51
|
+
title : str, optional
|
|
52
|
+
Plot title.
|
|
53
|
+
backend : {"seaborn", "plotly"}, default="seaborn"
|
|
54
|
+
Rendering backend.
|
|
55
|
+
**kwargs :
|
|
56
|
+
Forwarded to the underlying library.
|
|
57
|
+
|
|
58
|
+
Returns
|
|
59
|
+
-------
|
|
60
|
+
None
|
|
61
|
+
"""
|
|
62
|
+
self.__df = df
|
|
63
|
+
self.__class_column = class_column
|
|
64
|
+
self.__cols = cols
|
|
65
|
+
self.__color = color
|
|
66
|
+
self.__kwargs = kwargs
|
|
67
|
+
|
|
68
|
+
super().__init__(backend=backend, title=title)
|
|
69
|
+
self._render()
|
|
70
|
+
|
|
71
|
+
def _plot_seaborn(self):
|
|
72
|
+
"""Render with pandas' own parallel_coordinates helper."""
|
|
73
|
+
ax = pdp.parallel_coordinates(
|
|
74
|
+
self.__df,
|
|
75
|
+
class_column=self.__class_column,
|
|
76
|
+
cols=self.__cols,
|
|
77
|
+
color=self.__color,
|
|
78
|
+
**self.__kwargs,
|
|
79
|
+
)
|
|
80
|
+
if self._title:
|
|
81
|
+
ax.set_title(self._title)
|
|
82
|
+
|
|
83
|
+
def _plot_plotly(self):
|
|
84
|
+
"""Render with :func:`plotly.express.parallel_coordinates`."""
|
|
85
|
+
# px requires a numeric ``color`` column. We use the caller's
|
|
86
|
+
# ``class_column`` directly.
|
|
87
|
+
fig = px.parallel_coordinates(
|
|
88
|
+
self.__df,
|
|
89
|
+
color=self.__class_column,
|
|
90
|
+
dimensions=self.__cols,
|
|
91
|
+
title=self._title,
|
|
92
|
+
**self.__kwargs,
|
|
93
|
+
)
|
|
94
|
+
self._fig = fig
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""
|
|
2
|
+
plotify.categorical.pie_chart
|
|
3
|
+
=============================
|
|
4
|
+
|
|
5
|
+
Pie chart.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import matplotlib.pyplot as plt
|
|
9
|
+
import plotly.express as px
|
|
10
|
+
|
|
11
|
+
from plotify.base import BasePlot
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class PieChart(BasePlot):
|
|
15
|
+
"""
|
|
16
|
+
Pie chart.
|
|
17
|
+
|
|
18
|
+
Wraps :func:`matplotlib.pyplot.pie` on the Seaborn backend and
|
|
19
|
+
:func:`plotly.express.pie` on the Plotly backend.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
df,
|
|
25
|
+
names,
|
|
26
|
+
values,
|
|
27
|
+
colors=None,
|
|
28
|
+
explode=None,
|
|
29
|
+
startangle=90,
|
|
30
|
+
autopct="%1.1f%%",
|
|
31
|
+
title=None,
|
|
32
|
+
backend="seaborn",
|
|
33
|
+
**kwargs,
|
|
34
|
+
):
|
|
35
|
+
"""
|
|
36
|
+
This is used to create a pie chart.
|
|
37
|
+
|
|
38
|
+
Parameters
|
|
39
|
+
----------
|
|
40
|
+
df : pandas.DataFrame
|
|
41
|
+
Input data.
|
|
42
|
+
names : str
|
|
43
|
+
Column containing slice labels.
|
|
44
|
+
values : str
|
|
45
|
+
Column containing slice values.
|
|
46
|
+
colors : list[str], optional
|
|
47
|
+
Colour for each slice (Seaborn only).
|
|
48
|
+
explode : list[float], optional
|
|
49
|
+
Fraction by which to offset each slice (Seaborn only).
|
|
50
|
+
startangle : float, default=90
|
|
51
|
+
Start angle for the first slice (Seaborn only).
|
|
52
|
+
autopct : str or callable, default="%1.1f%%"
|
|
53
|
+
Value-label format (Seaborn only).
|
|
54
|
+
title : str, optional
|
|
55
|
+
Plot title.
|
|
56
|
+
backend : {"seaborn", "plotly"}, default="seaborn"
|
|
57
|
+
Rendering backend.
|
|
58
|
+
**kwargs :
|
|
59
|
+
Forwarded to the underlying library.
|
|
60
|
+
|
|
61
|
+
Returns
|
|
62
|
+
-------
|
|
63
|
+
None
|
|
64
|
+
"""
|
|
65
|
+
self.__df = df
|
|
66
|
+
self.__names = names
|
|
67
|
+
self.__values = values
|
|
68
|
+
self.__colors = colors
|
|
69
|
+
self.__explode = explode
|
|
70
|
+
self.__startangle = startangle
|
|
71
|
+
self.__autopct = autopct
|
|
72
|
+
self.__kwargs = kwargs
|
|
73
|
+
|
|
74
|
+
super().__init__(backend=backend, title=title)
|
|
75
|
+
self._render()
|
|
76
|
+
|
|
77
|
+
def _plot_seaborn(self):
|
|
78
|
+
"""Render using :func:`matplotlib.pyplot.pie`."""
|
|
79
|
+
plt.pie(
|
|
80
|
+
self.__df[self.__values],
|
|
81
|
+
labels=self.__df[self.__names],
|
|
82
|
+
colors=self.__colors,
|
|
83
|
+
explode=self.__explode,
|
|
84
|
+
startangle=self.__startangle,
|
|
85
|
+
autopct=self.__autopct,
|
|
86
|
+
**self.__kwargs,
|
|
87
|
+
)
|
|
88
|
+
plt.axis("equal") # ensures the pie is a circle, not an ellipse
|
|
89
|
+
if self._title:
|
|
90
|
+
plt.title(self._title)
|
|
91
|
+
|
|
92
|
+
def _plot_plotly(self):
|
|
93
|
+
"""Render using :func:`plotly.express.pie`."""
|
|
94
|
+
fig = px.pie(
|
|
95
|
+
self.__df,
|
|
96
|
+
names=self.__names,
|
|
97
|
+
values=self.__values,
|
|
98
|
+
title=self._title,
|
|
99
|
+
**self.__kwargs,
|
|
100
|
+
)
|
|
101
|
+
self._fig = fig
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""
|
|
2
|
+
plotify.categorical.radar_chart
|
|
3
|
+
===============================
|
|
4
|
+
|
|
5
|
+
Radar (a.k.a. spider) chart — multiple quantitative axes radiating from a
|
|
6
|
+
common origin, one per category.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import matplotlib.pyplot as plt
|
|
10
|
+
import numpy as np
|
|
11
|
+
import plotly.graph_objects as go
|
|
12
|
+
|
|
13
|
+
from plotify.base import BasePlot
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class RadarChart(BasePlot):
|
|
17
|
+
"""
|
|
18
|
+
Radar / spider chart.
|
|
19
|
+
|
|
20
|
+
Expects each row of ``df`` to represent one "series" (e.g. a person, a
|
|
21
|
+
product) and each column (except ``group_col``) to be one axis of the
|
|
22
|
+
chart.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
df,
|
|
28
|
+
categories,
|
|
29
|
+
group_col=None,
|
|
30
|
+
fill=True,
|
|
31
|
+
title=None,
|
|
32
|
+
backend="seaborn",
|
|
33
|
+
**kwargs,
|
|
34
|
+
):
|
|
35
|
+
"""
|
|
36
|
+
This is used to create a radar chart.
|
|
37
|
+
|
|
38
|
+
Parameters
|
|
39
|
+
----------
|
|
40
|
+
df : pandas.DataFrame
|
|
41
|
+
Data — one row per series.
|
|
42
|
+
categories : list[str]
|
|
43
|
+
Column names to treat as axes of the radar.
|
|
44
|
+
group_col : str, optional
|
|
45
|
+
Column identifying the series label used in the legend. If
|
|
46
|
+
``None`` each row is labelled by its index.
|
|
47
|
+
fill : bool, default=True
|
|
48
|
+
If True, fill the polygon area; otherwise only the outline is drawn.
|
|
49
|
+
title : str, optional
|
|
50
|
+
Plot title.
|
|
51
|
+
backend : {"seaborn", "plotly"}, default="seaborn"
|
|
52
|
+
Rendering backend.
|
|
53
|
+
**kwargs :
|
|
54
|
+
Forwarded to the underlying library.
|
|
55
|
+
|
|
56
|
+
Returns
|
|
57
|
+
-------
|
|
58
|
+
None
|
|
59
|
+
"""
|
|
60
|
+
self.__df = df
|
|
61
|
+
self.__categories = list(categories)
|
|
62
|
+
self.__group_col = group_col
|
|
63
|
+
self.__fill = fill
|
|
64
|
+
self.__kwargs = kwargs
|
|
65
|
+
|
|
66
|
+
super().__init__(backend=backend, title=title)
|
|
67
|
+
self._render()
|
|
68
|
+
|
|
69
|
+
def _plot_seaborn(self):
|
|
70
|
+
"""Render onto a Matplotlib polar axis."""
|
|
71
|
+
# Angles for each axis, closed by repeating the first angle at the end.
|
|
72
|
+
n = len(self.__categories)
|
|
73
|
+
angles = np.linspace(0, 2 * np.pi, n, endpoint=False).tolist()
|
|
74
|
+
angles += angles[:1]
|
|
75
|
+
|
|
76
|
+
ax = plt.gcf().add_subplot(111, polar=True)
|
|
77
|
+
for idx, row in self.__df.iterrows():
|
|
78
|
+
values = [row[c] for c in self.__categories]
|
|
79
|
+
values += values[:1] # close the polygon
|
|
80
|
+
label = row[self.__group_col] if self.__group_col else str(idx)
|
|
81
|
+
ax.plot(angles, values, label=label)
|
|
82
|
+
if self.__fill:
|
|
83
|
+
ax.fill(angles, values, alpha=0.25)
|
|
84
|
+
|
|
85
|
+
ax.set_xticks(angles[:-1])
|
|
86
|
+
ax.set_xticklabels(self.__categories)
|
|
87
|
+
ax.legend(loc="upper right", bbox_to_anchor=(1.2, 1.1))
|
|
88
|
+
if self._title:
|
|
89
|
+
ax.set_title(self._title)
|
|
90
|
+
|
|
91
|
+
def _plot_plotly(self):
|
|
92
|
+
"""Render using :class:`plotly.graph_objects.Scatterpolar`."""
|
|
93
|
+
fig = go.Figure()
|
|
94
|
+
for idx, row in self.__df.iterrows():
|
|
95
|
+
values = [row[c] for c in self.__categories]
|
|
96
|
+
values += values[:1]
|
|
97
|
+
theta = self.__categories + self.__categories[:1]
|
|
98
|
+
label = row[self.__group_col] if self.__group_col else str(idx)
|
|
99
|
+
fig.add_trace(
|
|
100
|
+
go.Scatterpolar(
|
|
101
|
+
r=values,
|
|
102
|
+
theta=theta,
|
|
103
|
+
fill="toself" if self.__fill else None,
|
|
104
|
+
name=str(label),
|
|
105
|
+
)
|
|
106
|
+
)
|
|
107
|
+
fig.update_layout(
|
|
108
|
+
polar=dict(radialaxis=dict(visible=True)),
|
|
109
|
+
title=self._title,
|
|
110
|
+
showlegend=True,
|
|
111
|
+
)
|
|
112
|
+
self._fig = fig
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
# Spider chart and radar chart are the same plot under different names.
|
|
116
|
+
SpiderChart = RadarChart
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""
|
|
2
|
+
plotify.categorical.sunburst_diagram
|
|
3
|
+
====================================
|
|
4
|
+
|
|
5
|
+
Sunburst diagram — a radial treemap that shows hierarchical structure.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import matplotlib.pyplot as plt
|
|
9
|
+
import numpy as np
|
|
10
|
+
import plotly.express as px
|
|
11
|
+
|
|
12
|
+
from plotify.base import BasePlot
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class SunburstDiagram(BasePlot):
|
|
16
|
+
"""
|
|
17
|
+
Sunburst diagram.
|
|
18
|
+
|
|
19
|
+
Seaborn backend draws nested pies; Plotly backend uses
|
|
20
|
+
:func:`plotly.express.sunburst` which has first-class hierarchy support.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
df,
|
|
26
|
+
path,
|
|
27
|
+
values,
|
|
28
|
+
title=None,
|
|
29
|
+
backend="seaborn",
|
|
30
|
+
**kwargs,
|
|
31
|
+
):
|
|
32
|
+
"""
|
|
33
|
+
This is used to create a sunburst diagram.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
df : pandas.DataFrame
|
|
38
|
+
Input data.
|
|
39
|
+
path : list[str]
|
|
40
|
+
Ordered list of column names tracing the hierarchy root → leaf.
|
|
41
|
+
values : str
|
|
42
|
+
Column with numeric values at the leaf level.
|
|
43
|
+
title : str, optional
|
|
44
|
+
Plot title.
|
|
45
|
+
backend : {"seaborn", "plotly"}, default="seaborn"
|
|
46
|
+
Rendering backend.
|
|
47
|
+
**kwargs :
|
|
48
|
+
Forwarded to the underlying library.
|
|
49
|
+
|
|
50
|
+
Returns
|
|
51
|
+
-------
|
|
52
|
+
None
|
|
53
|
+
"""
|
|
54
|
+
if not path:
|
|
55
|
+
raise ValueError("`path` must contain at least one column name.")
|
|
56
|
+
|
|
57
|
+
self.__df = df
|
|
58
|
+
self.__path = list(path)
|
|
59
|
+
self.__values = values
|
|
60
|
+
self.__kwargs = kwargs
|
|
61
|
+
|
|
62
|
+
super().__init__(backend=backend, title=title)
|
|
63
|
+
self._render()
|
|
64
|
+
|
|
65
|
+
def _plot_seaborn(self):
|
|
66
|
+
"""Draw nested ring pies from outer level to inner level."""
|
|
67
|
+
fig, ax = plt.subplots()
|
|
68
|
+
ax.set_aspect("equal")
|
|
69
|
+
|
|
70
|
+
# Build a ring per level. Level 0 is the innermost ring.
|
|
71
|
+
# Width of each ring decreases with depth so the leaf ring is the widest.
|
|
72
|
+
n_levels = len(self.__path)
|
|
73
|
+
ring_width = 1.0 / n_levels
|
|
74
|
+
|
|
75
|
+
cmap = plt.get_cmap("tab20")
|
|
76
|
+
|
|
77
|
+
for level, col in enumerate(self.__path):
|
|
78
|
+
# Aggregate values at this depth.
|
|
79
|
+
level_df = (
|
|
80
|
+
self.__df.groupby(self.__path[: level + 1])[self.__values]
|
|
81
|
+
.sum()
|
|
82
|
+
.reset_index()
|
|
83
|
+
)
|
|
84
|
+
sizes = level_df[self.__values].values
|
|
85
|
+
# A wedge per group; colour by index for now.
|
|
86
|
+
colours = [cmap(i % 20) for i in range(len(sizes))]
|
|
87
|
+
|
|
88
|
+
radius = (level + 1) * ring_width
|
|
89
|
+
ax.pie(
|
|
90
|
+
sizes,
|
|
91
|
+
radius=radius,
|
|
92
|
+
colors=colours,
|
|
93
|
+
wedgeprops=dict(width=ring_width, edgecolor="white"),
|
|
94
|
+
labels=level_df[col].astype(str).tolist() if level == n_levels - 1 else None,
|
|
95
|
+
labeldistance=1.05,
|
|
96
|
+
startangle=90,
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
# Suppress the default 0..1 axes; the pies speak for themselves.
|
|
100
|
+
ax.set(aspect="equal")
|
|
101
|
+
plt.axis("off")
|
|
102
|
+
if self._title:
|
|
103
|
+
ax.set_title(self._title)
|
|
104
|
+
|
|
105
|
+
# Reference np to avoid lint removal warning; used implicitly by mpl internals.
|
|
106
|
+
_ = np.asarray([])
|
|
107
|
+
|
|
108
|
+
def _plot_plotly(self):
|
|
109
|
+
"""Render using :func:`plotly.express.sunburst`."""
|
|
110
|
+
fig = px.sunburst(
|
|
111
|
+
self.__df,
|
|
112
|
+
path=self.__path,
|
|
113
|
+
values=self.__values,
|
|
114
|
+
title=self._title,
|
|
115
|
+
**self.__kwargs,
|
|
116
|
+
)
|
|
117
|
+
self._fig = fig
|