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,105 @@
|
|
|
1
|
+
"""
|
|
2
|
+
plotify.num_cat.grouped_scatter
|
|
3
|
+
===============================
|
|
4
|
+
|
|
5
|
+
Grouped scatter plot — scatter with colour (and optionally marker style)
|
|
6
|
+
encoding a categorical group.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import matplotlib.pyplot as plt
|
|
10
|
+
import plotly.express as px
|
|
11
|
+
import seaborn as sns
|
|
12
|
+
|
|
13
|
+
from plotify.base import BasePlot
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class GroupedScatter(BasePlot):
|
|
17
|
+
"""
|
|
18
|
+
Scatter plot coloured by a categorical group column.
|
|
19
|
+
|
|
20
|
+
Equivalent to :class:`plotify.numerical.ScatterPlot` with mandatory
|
|
21
|
+
``hue`` semantics — exposed as a separate class so numeric-only and
|
|
22
|
+
grouped use cases are discoverable from separate subpackages.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
df,
|
|
28
|
+
x,
|
|
29
|
+
y,
|
|
30
|
+
hue,
|
|
31
|
+
style=None,
|
|
32
|
+
size=None,
|
|
33
|
+
palette=None,
|
|
34
|
+
title=None,
|
|
35
|
+
backend="seaborn",
|
|
36
|
+
**kwargs,
|
|
37
|
+
):
|
|
38
|
+
"""
|
|
39
|
+
This is used to create a grouped scatter plot.
|
|
40
|
+
|
|
41
|
+
Parameters
|
|
42
|
+
----------
|
|
43
|
+
df : pandas.DataFrame
|
|
44
|
+
Input data.
|
|
45
|
+
x, y : str
|
|
46
|
+
Numeric columns mapped to the axes.
|
|
47
|
+
hue : str
|
|
48
|
+
Categorical column mapped to colour.
|
|
49
|
+
style : str, optional
|
|
50
|
+
Column mapped to marker shape (Seaborn only).
|
|
51
|
+
size : str, optional
|
|
52
|
+
Column mapped to marker size.
|
|
53
|
+
palette : str or list, optional
|
|
54
|
+
Colour palette.
|
|
55
|
+
title : str, optional
|
|
56
|
+
Plot title.
|
|
57
|
+
backend : {"seaborn", "plotly"}, default="seaborn"
|
|
58
|
+
Rendering backend.
|
|
59
|
+
**kwargs :
|
|
60
|
+
Forwarded to the underlying library.
|
|
61
|
+
|
|
62
|
+
Returns
|
|
63
|
+
-------
|
|
64
|
+
None
|
|
65
|
+
"""
|
|
66
|
+
self.__df = df
|
|
67
|
+
self.__x = x
|
|
68
|
+
self.__y = y
|
|
69
|
+
self.__hue = hue
|
|
70
|
+
self.__style = style
|
|
71
|
+
self.__size = size
|
|
72
|
+
self.__palette = palette
|
|
73
|
+
self.__kwargs = kwargs
|
|
74
|
+
|
|
75
|
+
super().__init__(backend=backend, title=title)
|
|
76
|
+
self._render()
|
|
77
|
+
|
|
78
|
+
def _plot_seaborn(self):
|
|
79
|
+
"""Render using :func:`seaborn.scatterplot` with hue/style mappings."""
|
|
80
|
+
sns.scatterplot(
|
|
81
|
+
data=self.__df,
|
|
82
|
+
x=self.__x,
|
|
83
|
+
y=self.__y,
|
|
84
|
+
hue=self.__hue,
|
|
85
|
+
style=self.__style,
|
|
86
|
+
size=self.__size,
|
|
87
|
+
palette=self.__palette,
|
|
88
|
+
**self.__kwargs,
|
|
89
|
+
)
|
|
90
|
+
if self._title:
|
|
91
|
+
plt.title(self._title)
|
|
92
|
+
|
|
93
|
+
def _plot_plotly(self):
|
|
94
|
+
"""Render using :func:`plotly.express.scatter`."""
|
|
95
|
+
fig = px.scatter(
|
|
96
|
+
self.__df,
|
|
97
|
+
x=self.__x,
|
|
98
|
+
y=self.__y,
|
|
99
|
+
color=self.__hue,
|
|
100
|
+
symbol=self.__style,
|
|
101
|
+
size=self.__size,
|
|
102
|
+
title=self._title,
|
|
103
|
+
**self.__kwargs,
|
|
104
|
+
)
|
|
105
|
+
self._fig = fig
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""
|
|
2
|
+
plotify.num_cat.stacked_bar_plot
|
|
3
|
+
================================
|
|
4
|
+
|
|
5
|
+
Stacked bar plot — bars subdivided by a second categorical variable.
|
|
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 StackedBarPlot(BasePlot):
|
|
15
|
+
"""
|
|
16
|
+
Stacked bar plot.
|
|
17
|
+
|
|
18
|
+
On the Seaborn backend this is drawn by pivoting the dataframe to wide
|
|
19
|
+
form and calling :meth:`pandas.DataFrame.plot.bar` with ``stacked=True``.
|
|
20
|
+
On the Plotly backend, :func:`plotly.express.bar` with
|
|
21
|
+
``barmode='stack'`` handles it directly.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
df,
|
|
27
|
+
x,
|
|
28
|
+
y,
|
|
29
|
+
hue,
|
|
30
|
+
normalize=False,
|
|
31
|
+
palette=None,
|
|
32
|
+
title=None,
|
|
33
|
+
backend="seaborn",
|
|
34
|
+
**kwargs,
|
|
35
|
+
):
|
|
36
|
+
"""
|
|
37
|
+
This is used to create a stacked bar plot.
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
df : pandas.DataFrame
|
|
42
|
+
Input data.
|
|
43
|
+
x : str
|
|
44
|
+
Primary categorical column (one stack per value).
|
|
45
|
+
y : str
|
|
46
|
+
Numeric value column.
|
|
47
|
+
hue : str
|
|
48
|
+
Sub-group column — each value becomes one segment of each stack.
|
|
49
|
+
normalize : bool, default=False
|
|
50
|
+
If True, scale each stack to sum to 1 (100% stacked bar).
|
|
51
|
+
palette : str or list, optional
|
|
52
|
+
Colour palette.
|
|
53
|
+
title : str, optional
|
|
54
|
+
Plot title.
|
|
55
|
+
backend : {"seaborn", "plotly"}, default="seaborn"
|
|
56
|
+
Rendering backend.
|
|
57
|
+
**kwargs :
|
|
58
|
+
Forwarded to the underlying library.
|
|
59
|
+
|
|
60
|
+
Returns
|
|
61
|
+
-------
|
|
62
|
+
None
|
|
63
|
+
"""
|
|
64
|
+
self.__df = df
|
|
65
|
+
self.__x = x
|
|
66
|
+
self.__y = y
|
|
67
|
+
self.__hue = hue
|
|
68
|
+
self.__normalize = normalize
|
|
69
|
+
self.__palette = palette
|
|
70
|
+
self.__kwargs = kwargs
|
|
71
|
+
|
|
72
|
+
super().__init__(backend=backend, title=title)
|
|
73
|
+
self._render()
|
|
74
|
+
|
|
75
|
+
def _plot_seaborn(self):
|
|
76
|
+
"""Pivot and use pandas' stacked bar plotting."""
|
|
77
|
+
# Aggregate to wide form — one column per hue level.
|
|
78
|
+
wide = (
|
|
79
|
+
self.__df.pivot_table(
|
|
80
|
+
index=self.__x,
|
|
81
|
+
columns=self.__hue,
|
|
82
|
+
values=self.__y,
|
|
83
|
+
aggfunc="sum",
|
|
84
|
+
)
|
|
85
|
+
.fillna(0)
|
|
86
|
+
)
|
|
87
|
+
if self.__normalize:
|
|
88
|
+
wide = wide.div(wide.sum(axis=1), axis=0)
|
|
89
|
+
|
|
90
|
+
# pandas .plot.bar wraps matplotlib and cleanly supports stacked bars.
|
|
91
|
+
ax = wide.plot.bar(
|
|
92
|
+
stacked=True,
|
|
93
|
+
color=self.__palette,
|
|
94
|
+
**self.__kwargs,
|
|
95
|
+
)
|
|
96
|
+
ax.set_ylabel(self.__y)
|
|
97
|
+
if self._title:
|
|
98
|
+
ax.set_title(self._title)
|
|
99
|
+
|
|
100
|
+
def _plot_plotly(self):
|
|
101
|
+
"""Render with :func:`plotly.express.bar` using ``barmode='stack'``."""
|
|
102
|
+
data = self.__df.copy()
|
|
103
|
+
if self.__normalize:
|
|
104
|
+
# Compute per-x totals, divide each row by its total.
|
|
105
|
+
totals = data.groupby(self.__x)[self.__y].transform("sum")
|
|
106
|
+
data[self.__y] = data[self.__y] / totals
|
|
107
|
+
|
|
108
|
+
fig = px.bar(
|
|
109
|
+
data,
|
|
110
|
+
x=self.__x,
|
|
111
|
+
y=self.__y,
|
|
112
|
+
color=self.__hue,
|
|
113
|
+
barmode="stack",
|
|
114
|
+
title=self._title,
|
|
115
|
+
**self.__kwargs,
|
|
116
|
+
)
|
|
117
|
+
self._fig = fig
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""
|
|
2
|
+
plotify.numerical
|
|
3
|
+
=================
|
|
4
|
+
|
|
5
|
+
Plots for purely numeric variables: Boxplot, DensityPlot, Violinplot,
|
|
6
|
+
ScatterPlot, ConnectedScatterPlot.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from plotify.numerical.boxplot import Boxplot
|
|
10
|
+
from plotify.numerical.connected_scatter_plot import ConnectedScatterPlot
|
|
11
|
+
from plotify.numerical.density_plot import DensityPlot
|
|
12
|
+
from plotify.numerical.scatter_plot import ScatterPlot
|
|
13
|
+
from plotify.numerical.violin_plot import Violinplot
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"Boxplot",
|
|
17
|
+
"ConnectedScatterPlot",
|
|
18
|
+
"DensityPlot",
|
|
19
|
+
"ScatterPlot",
|
|
20
|
+
"Violinplot",
|
|
21
|
+
]
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""
|
|
2
|
+
plotify.numerical.boxplot
|
|
3
|
+
=========================
|
|
4
|
+
|
|
5
|
+
Dual-backend Boxplot class. Wraps :func:`seaborn.boxplot` on the Seaborn
|
|
6
|
+
backend and :func:`plotly.express.box` on the Plotly backend.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import matplotlib.pyplot as plt
|
|
10
|
+
import plotly.express as px
|
|
11
|
+
import seaborn as sns
|
|
12
|
+
|
|
13
|
+
from plotify.base import BasePlot
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Boxplot(BasePlot):
|
|
17
|
+
"""
|
|
18
|
+
Create a boxplot using either Seaborn/Matplotlib or Plotly.
|
|
19
|
+
|
|
20
|
+
A boxplot summarises the distribution of a numeric variable using the
|
|
21
|
+
first, second (median) and third quartiles, plus whiskers that extend
|
|
22
|
+
to the furthest non-outlier observations.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
df,
|
|
28
|
+
x=None,
|
|
29
|
+
y=None,
|
|
30
|
+
hue=None,
|
|
31
|
+
order=None,
|
|
32
|
+
hue_order=None,
|
|
33
|
+
orient=None,
|
|
34
|
+
color=None,
|
|
35
|
+
palette=None,
|
|
36
|
+
saturation=0.75,
|
|
37
|
+
width=0.8,
|
|
38
|
+
dodge=True,
|
|
39
|
+
fliersize=5,
|
|
40
|
+
linewidth=None,
|
|
41
|
+
whis=1.5,
|
|
42
|
+
ax=None,
|
|
43
|
+
title=None,
|
|
44
|
+
backend="seaborn",
|
|
45
|
+
**kwargs,
|
|
46
|
+
):
|
|
47
|
+
"""
|
|
48
|
+
This is used to create boxplot figures using seaborn or plotly.
|
|
49
|
+
|
|
50
|
+
Parameters
|
|
51
|
+
----------
|
|
52
|
+
df : pandas.DataFrame
|
|
53
|
+
The dataframe containing the data to plot.
|
|
54
|
+
x, y : str, optional
|
|
55
|
+
The names of two columns in df. The box plots will be for the y
|
|
56
|
+
column grouped by x column values.
|
|
57
|
+
hue : str, optional
|
|
58
|
+
Column name for grouping the data by color/hue.
|
|
59
|
+
order, hue_order : list, optional
|
|
60
|
+
Order to plot the categorical levels in.
|
|
61
|
+
orient : str, optional
|
|
62
|
+
Orientation of the plot. ``'v'`` or ``'h'``. (Seaborn only.)
|
|
63
|
+
color : str, optional
|
|
64
|
+
Color for all of the elements. (Seaborn only.)
|
|
65
|
+
palette : str, optional
|
|
66
|
+
Colors to use for the different levels of the hue variable.
|
|
67
|
+
saturation : float, optional
|
|
68
|
+
Proportion of the original saturation to draw colors. (Seaborn only.)
|
|
69
|
+
width : float, optional
|
|
70
|
+
Width of a full element.
|
|
71
|
+
dodge : bool, optional
|
|
72
|
+
Whether elements should be shifted along the categorical axis
|
|
73
|
+
when hue nesting is used. (Seaborn only.)
|
|
74
|
+
fliersize : float, optional
|
|
75
|
+
Size of the markers used to indicate outlier observations.
|
|
76
|
+
(Seaborn only.)
|
|
77
|
+
linewidth : float, optional
|
|
78
|
+
Line width of the box outlines. (Seaborn only.)
|
|
79
|
+
whis : float, optional
|
|
80
|
+
Proportion of the IQR past the low and high quartiles to extend
|
|
81
|
+
the plot whiskers. (Seaborn only.)
|
|
82
|
+
ax : matplotlib.axes, optional
|
|
83
|
+
Pre-existing axes. (Seaborn only.)
|
|
84
|
+
title : str, optional
|
|
85
|
+
Title for the plot.
|
|
86
|
+
backend : {"seaborn", "plotly"}, default="seaborn"
|
|
87
|
+
Rendering backend.
|
|
88
|
+
**kwargs :
|
|
89
|
+
Additional keyword arguments forwarded to the underlying library
|
|
90
|
+
function (``sns.boxplot`` or ``plotly.express.box``).
|
|
91
|
+
|
|
92
|
+
Returns
|
|
93
|
+
-------
|
|
94
|
+
None
|
|
95
|
+
"""
|
|
96
|
+
# Store everything as private attributes so render methods can read them.
|
|
97
|
+
self.__df = df
|
|
98
|
+
self.__x = x
|
|
99
|
+
self.__y = y
|
|
100
|
+
self.__hue = hue
|
|
101
|
+
self.__order = order
|
|
102
|
+
self.__hue_order = hue_order
|
|
103
|
+
self.__orient = orient
|
|
104
|
+
self.__color = color
|
|
105
|
+
self.__palette = palette
|
|
106
|
+
self.__saturation = saturation
|
|
107
|
+
self.__width = width
|
|
108
|
+
self.__dodge = dodge
|
|
109
|
+
self.__fliersize = fliersize
|
|
110
|
+
self.__linewidth = linewidth
|
|
111
|
+
self.__whis = whis
|
|
112
|
+
self.__ax = ax
|
|
113
|
+
self.__kwargs = kwargs
|
|
114
|
+
|
|
115
|
+
super().__init__(backend=backend, title=title)
|
|
116
|
+
self._render()
|
|
117
|
+
|
|
118
|
+
def _plot_seaborn(self):
|
|
119
|
+
"""Render the boxplot using :func:`seaborn.boxplot`."""
|
|
120
|
+
sns.boxplot(
|
|
121
|
+
x=self.__x,
|
|
122
|
+
y=self.__y,
|
|
123
|
+
hue=self.__hue,
|
|
124
|
+
data=self.__df,
|
|
125
|
+
order=self.__order,
|
|
126
|
+
hue_order=self.__hue_order,
|
|
127
|
+
orient=self.__orient,
|
|
128
|
+
color=self.__color,
|
|
129
|
+
palette=self.__palette,
|
|
130
|
+
saturation=self.__saturation,
|
|
131
|
+
width=self.__width,
|
|
132
|
+
dodge=self.__dodge,
|
|
133
|
+
fliersize=self.__fliersize,
|
|
134
|
+
linewidth=self.__linewidth,
|
|
135
|
+
whis=self.__whis,
|
|
136
|
+
ax=self.__ax,
|
|
137
|
+
**self.__kwargs,
|
|
138
|
+
)
|
|
139
|
+
if self._title:
|
|
140
|
+
plt.title(self._title)
|
|
141
|
+
|
|
142
|
+
def _plot_plotly(self):
|
|
143
|
+
"""Render the boxplot using :func:`plotly.express.box`."""
|
|
144
|
+
# Plotly ignores most of the Seaborn-only cosmetic params; we forward
|
|
145
|
+
# the ones that map cleanly and let **kwargs carry anything extra.
|
|
146
|
+
fig = px.box(
|
|
147
|
+
self.__df,
|
|
148
|
+
x=self.__x,
|
|
149
|
+
y=self.__y,
|
|
150
|
+
color=self.__hue,
|
|
151
|
+
color_discrete_sequence=(
|
|
152
|
+
[self.__palette] if isinstance(self.__palette, str) else None
|
|
153
|
+
),
|
|
154
|
+
title=self._title,
|
|
155
|
+
**self.__kwargs,
|
|
156
|
+
)
|
|
157
|
+
self._fig = fig
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""
|
|
2
|
+
plotify.numerical.connected_scatter_plot
|
|
3
|
+
========================================
|
|
4
|
+
|
|
5
|
+
Dual-backend connected scatter plot — scatter points joined by line
|
|
6
|
+
segments, useful for showing the trajectory of paired observations.
|
|
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 ConnectedScatterPlot(BasePlot):
|
|
16
|
+
"""
|
|
17
|
+
Draw one or more connected scatter plots.
|
|
18
|
+
|
|
19
|
+
Each series may be described by a dict in ``plots`` containing at least
|
|
20
|
+
``'x'`` and ``'y'`` and optionally ``'linestyle'``, ``'marker'``,
|
|
21
|
+
``'color'``, ``'linewidth'``, ``'label'``.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
df,
|
|
27
|
+
x=None,
|
|
28
|
+
y=None,
|
|
29
|
+
plots=None,
|
|
30
|
+
ax=None,
|
|
31
|
+
title=None,
|
|
32
|
+
default_style=None,
|
|
33
|
+
backend="seaborn",
|
|
34
|
+
**kwargs,
|
|
35
|
+
):
|
|
36
|
+
"""
|
|
37
|
+
This is used to create one or more connected scatter plots.
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
df : pandas.DataFrame
|
|
42
|
+
The data to plot.
|
|
43
|
+
x, y : str, optional
|
|
44
|
+
Column names for a single basic plot.
|
|
45
|
+
plots : list[dict], optional
|
|
46
|
+
List of per-series parameter dicts.
|
|
47
|
+
ax : matplotlib.axes.Axes, optional
|
|
48
|
+
Pre-existing axes (seaborn backend only).
|
|
49
|
+
title : str, optional
|
|
50
|
+
Plot title.
|
|
51
|
+
default_style : dict, optional
|
|
52
|
+
Default styling merged into the single-plot spec when ``plots``
|
|
53
|
+
is not provided.
|
|
54
|
+
backend : {"seaborn", "plotly"}, default="seaborn"
|
|
55
|
+
Rendering backend.
|
|
56
|
+
**kwargs :
|
|
57
|
+
Extra style keys merged into the single-plot spec (seaborn
|
|
58
|
+
backend) or ignored (plotly).
|
|
59
|
+
|
|
60
|
+
Returns
|
|
61
|
+
-------
|
|
62
|
+
None
|
|
63
|
+
"""
|
|
64
|
+
self.__df = df
|
|
65
|
+
self.__plots = plots or []
|
|
66
|
+
self.__ax = ax
|
|
67
|
+
self.__default_style = default_style or {}
|
|
68
|
+
|
|
69
|
+
# Promote a single (x, y) call to a one-entry plots list so both
|
|
70
|
+
# render paths share the same loop structure.
|
|
71
|
+
if x and y:
|
|
72
|
+
self.__plots = [{"x": x, "y": y, **self.__default_style, **kwargs}]
|
|
73
|
+
|
|
74
|
+
super().__init__(backend=backend, title=title)
|
|
75
|
+
self._render()
|
|
76
|
+
|
|
77
|
+
def _plot_seaborn(self):
|
|
78
|
+
"""Render using :func:`matplotlib.axes.Axes.plot`."""
|
|
79
|
+
if self.__ax is None:
|
|
80
|
+
self.__ax = plt.gca()
|
|
81
|
+
|
|
82
|
+
for plot in self.__plots:
|
|
83
|
+
# Copy to avoid consuming the caller's dict on repeat renders.
|
|
84
|
+
spec = dict(plot)
|
|
85
|
+
x = spec.pop("x")
|
|
86
|
+
y = spec.pop("y")
|
|
87
|
+
self.__ax.plot(self.__df[x], self.__df[y], **spec)
|
|
88
|
+
|
|
89
|
+
if self._title:
|
|
90
|
+
self.__ax.set_title(self._title)
|
|
91
|
+
|
|
92
|
+
# Only draw a legend if at least one trace carried a label.
|
|
93
|
+
handles, labels = self.__ax.get_legend_handles_labels()
|
|
94
|
+
if labels:
|
|
95
|
+
self.__ax.legend()
|
|
96
|
+
|
|
97
|
+
def _plot_plotly(self):
|
|
98
|
+
"""Render using Plotly ``go.Scatter`` traces with ``mode='lines+markers'``."""
|
|
99
|
+
fig = go.Figure()
|
|
100
|
+
for plot in self.__plots:
|
|
101
|
+
spec = dict(plot)
|
|
102
|
+
x_col = spec.pop("x")
|
|
103
|
+
y_col = spec.pop("y")
|
|
104
|
+
fig.add_trace(
|
|
105
|
+
go.Scatter(
|
|
106
|
+
x=self.__df[x_col],
|
|
107
|
+
y=self.__df[y_col],
|
|
108
|
+
mode="lines+markers",
|
|
109
|
+
name=spec.get("label", y_col),
|
|
110
|
+
line=dict(
|
|
111
|
+
color=spec.get("color"),
|
|
112
|
+
width=spec.get("linewidth"),
|
|
113
|
+
dash=spec.get("linestyle"),
|
|
114
|
+
),
|
|
115
|
+
marker=dict(symbol=spec.get("marker", "circle")),
|
|
116
|
+
)
|
|
117
|
+
)
|
|
118
|
+
if self._title:
|
|
119
|
+
fig.update_layout(title=self._title)
|
|
120
|
+
self._fig = fig
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"""
|
|
2
|
+
plotify.numerical.density_plot
|
|
3
|
+
==============================
|
|
4
|
+
|
|
5
|
+
Dual-backend kernel-density / density-contour plot.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import matplotlib.pyplot as plt
|
|
9
|
+
import plotly.express as px
|
|
10
|
+
import seaborn as sns
|
|
11
|
+
|
|
12
|
+
from plotify.base import BasePlot
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class DensityPlot(BasePlot):
|
|
16
|
+
"""
|
|
17
|
+
Kernel density estimate (1D) or density contour (2D) plot.
|
|
18
|
+
|
|
19
|
+
On the Seaborn backend this wraps :func:`seaborn.kdeplot`. On the Plotly
|
|
20
|
+
backend it uses :func:`plotly.express.density_contour` (bivariate) or
|
|
21
|
+
:func:`plotly.express.histogram` with ``marginal="violin"`` for univariate
|
|
22
|
+
cases.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
df,
|
|
28
|
+
x=None,
|
|
29
|
+
y=None,
|
|
30
|
+
hue=None,
|
|
31
|
+
weights=None,
|
|
32
|
+
palette=None,
|
|
33
|
+
hue_order=None,
|
|
34
|
+
hue_norm=None,
|
|
35
|
+
fill=False,
|
|
36
|
+
color=None,
|
|
37
|
+
multiple="layer",
|
|
38
|
+
common_norm=True,
|
|
39
|
+
common_grid=False,
|
|
40
|
+
cumulative=False,
|
|
41
|
+
bw_method="scott",
|
|
42
|
+
bw_adjust=1,
|
|
43
|
+
warn_singular=True,
|
|
44
|
+
log_scale=None,
|
|
45
|
+
levels=10,
|
|
46
|
+
thresh=0.05,
|
|
47
|
+
gridsize=200,
|
|
48
|
+
cut=3,
|
|
49
|
+
clip=None,
|
|
50
|
+
legend=True,
|
|
51
|
+
cbar=False,
|
|
52
|
+
cbar_ax=None,
|
|
53
|
+
cbar_kws=None,
|
|
54
|
+
ax=None,
|
|
55
|
+
title=None,
|
|
56
|
+
backend="seaborn",
|
|
57
|
+
**kwargs,
|
|
58
|
+
):
|
|
59
|
+
"""
|
|
60
|
+
This is used to create density plot figures.
|
|
61
|
+
|
|
62
|
+
Parameters mirror :func:`seaborn.kdeplot`. See that function's
|
|
63
|
+
documentation for exhaustive descriptions. The ``backend`` parameter
|
|
64
|
+
selects between Seaborn/Matplotlib and Plotly.
|
|
65
|
+
|
|
66
|
+
Parameters
|
|
67
|
+
----------
|
|
68
|
+
df : pandas.DataFrame
|
|
69
|
+
Data for the plot.
|
|
70
|
+
x, y : str, optional
|
|
71
|
+
Variables to map to the x and y axes. Pass ``y`` as well to obtain
|
|
72
|
+
a bivariate density.
|
|
73
|
+
hue : str, optional
|
|
74
|
+
Variable to map to color.
|
|
75
|
+
backend : {"seaborn", "plotly"}, default="seaborn"
|
|
76
|
+
Rendering backend.
|
|
77
|
+
**kwargs :
|
|
78
|
+
Extra keyword arguments forwarded to the underlying library.
|
|
79
|
+
|
|
80
|
+
Returns
|
|
81
|
+
-------
|
|
82
|
+
None
|
|
83
|
+
"""
|
|
84
|
+
self.__df = df
|
|
85
|
+
self.__x = x
|
|
86
|
+
self.__y = y
|
|
87
|
+
self.__hue = hue
|
|
88
|
+
self.__weights = weights
|
|
89
|
+
self.__palette = palette
|
|
90
|
+
self.__hue_order = hue_order
|
|
91
|
+
self.__hue_norm = hue_norm
|
|
92
|
+
self.__fill = fill
|
|
93
|
+
self.__color = color
|
|
94
|
+
self.__multiple = multiple
|
|
95
|
+
self.__common_norm = common_norm
|
|
96
|
+
self.__common_grid = common_grid
|
|
97
|
+
self.__cumulative = cumulative
|
|
98
|
+
self.__bw_method = bw_method
|
|
99
|
+
self.__bw_adjust = bw_adjust
|
|
100
|
+
self.__warn_singular = warn_singular
|
|
101
|
+
self.__log_scale = log_scale
|
|
102
|
+
self.__levels = levels
|
|
103
|
+
self.__thresh = thresh
|
|
104
|
+
self.__gridsize = gridsize
|
|
105
|
+
self.__cut = cut
|
|
106
|
+
self.__clip = clip
|
|
107
|
+
self.__legend = legend
|
|
108
|
+
self.__cbar = cbar
|
|
109
|
+
self.__cbar_ax = cbar_ax
|
|
110
|
+
self.__cbar_kws = cbar_kws
|
|
111
|
+
self.__ax = ax
|
|
112
|
+
self.__kwargs = kwargs
|
|
113
|
+
|
|
114
|
+
super().__init__(backend=backend, title=title)
|
|
115
|
+
self._render()
|
|
116
|
+
|
|
117
|
+
def _plot_seaborn(self):
|
|
118
|
+
"""Render using :func:`seaborn.kdeplot`."""
|
|
119
|
+
sns.kdeplot(
|
|
120
|
+
data=self.__df,
|
|
121
|
+
x=self.__x,
|
|
122
|
+
y=self.__y,
|
|
123
|
+
hue=self.__hue,
|
|
124
|
+
weights=self.__weights,
|
|
125
|
+
palette=self.__palette,
|
|
126
|
+
hue_order=self.__hue_order,
|
|
127
|
+
hue_norm=self.__hue_norm,
|
|
128
|
+
fill=self.__fill,
|
|
129
|
+
color=self.__color,
|
|
130
|
+
multiple=self.__multiple,
|
|
131
|
+
common_norm=self.__common_norm,
|
|
132
|
+
common_grid=self.__common_grid,
|
|
133
|
+
cumulative=self.__cumulative,
|
|
134
|
+
bw_method=self.__bw_method,
|
|
135
|
+
bw_adjust=self.__bw_adjust,
|
|
136
|
+
warn_singular=self.__warn_singular,
|
|
137
|
+
log_scale=self.__log_scale,
|
|
138
|
+
levels=self.__levels,
|
|
139
|
+
thresh=self.__thresh,
|
|
140
|
+
gridsize=self.__gridsize,
|
|
141
|
+
cut=self.__cut,
|
|
142
|
+
clip=self.__clip,
|
|
143
|
+
legend=self.__legend,
|
|
144
|
+
cbar=self.__cbar,
|
|
145
|
+
cbar_ax=self.__cbar_ax,
|
|
146
|
+
cbar_kws=self.__cbar_kws,
|
|
147
|
+
ax=self.__ax,
|
|
148
|
+
**self.__kwargs,
|
|
149
|
+
)
|
|
150
|
+
if self._title:
|
|
151
|
+
plt.title(self._title)
|
|
152
|
+
|
|
153
|
+
def _plot_plotly(self):
|
|
154
|
+
"""Render using Plotly Express.
|
|
155
|
+
|
|
156
|
+
Picks ``density_contour`` when both x and y are supplied, falls back
|
|
157
|
+
to a KDE-style ``histogram`` with marginal violin otherwise.
|
|
158
|
+
"""
|
|
159
|
+
if self.__x is not None and self.__y is not None:
|
|
160
|
+
# Bivariate: a density contour is the closest Plotly analogue.
|
|
161
|
+
fig = px.density_contour(
|
|
162
|
+
self.__df,
|
|
163
|
+
x=self.__x,
|
|
164
|
+
y=self.__y,
|
|
165
|
+
color=self.__hue,
|
|
166
|
+
title=self._title,
|
|
167
|
+
**self.__kwargs,
|
|
168
|
+
)
|
|
169
|
+
if self.__fill:
|
|
170
|
+
fig.update_traces(contours_coloring="fill")
|
|
171
|
+
else:
|
|
172
|
+
# Univariate: Plotly does not have a first-class KDE; approximate
|
|
173
|
+
# with a normalised histogram and a violin marginal.
|
|
174
|
+
axis = self.__x if self.__x is not None else self.__y
|
|
175
|
+
fig = px.histogram(
|
|
176
|
+
self.__df,
|
|
177
|
+
x=axis,
|
|
178
|
+
color=self.__hue,
|
|
179
|
+
histnorm="probability density",
|
|
180
|
+
marginal="violin",
|
|
181
|
+
title=self._title,
|
|
182
|
+
**self.__kwargs,
|
|
183
|
+
)
|
|
184
|
+
self._fig = fig
|