Skip to content

Commit 7864edb

Browse files
authored
seaborn: improve the matrix module (#14464)
1 parent f15658b commit 7864edb

File tree

1 file changed

+81
-58
lines changed

1 file changed

+81
-58
lines changed

stubs/seaborn/seaborn/matrix.pyi

Lines changed: 81 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
11
from _typeshed import Incomplete
22
from collections.abc import Hashable, Iterable, Mapping, Sequence
3-
from typing import Literal
3+
from typing import Literal, TypedDict, type_check_only
44
from typing_extensions import Self, TypeAlias
55

66
import numpy as np
7+
import pandas as pd
78
from matplotlib.axes import Axes
8-
from matplotlib.colors import Colormap, ListedColormap
9+
from matplotlib.colors import Colormap, ListedColormap, Normalize
910
from matplotlib.gridspec import GridSpec
1011
from matplotlib.typing import ColorType
11-
from numpy._typing import _ArrayLike, _ArrayLikeInt_co
12+
from numpy._typing import _ArrayLikeInt_co
1213
from numpy.typing import ArrayLike, NDArray
13-
from pandas import DataFrame, Index, Series
1414

1515
from .axisgrid import Grid
1616

1717
# pandas._typing.ListLikeU is partially Unknown
18-
_ListLikeU: TypeAlias = Sequence[Incomplete] | np.ndarray[Incomplete, Incomplete] | Series[Incomplete] | Index[Incomplete]
18+
_ListLikeU: TypeAlias = Sequence[Incomplete] | NDArray[Incomplete] | pd.Series[Incomplete] | pd.Index[Incomplete]
19+
_ConvertibleToDataFrame: TypeAlias = (
20+
_ListLikeU
21+
| pd.DataFrame
22+
| dict[Incomplete, Incomplete]
23+
| Iterable[_ListLikeU | tuple[Hashable, _ListLikeU] | dict[Incomplete, Incomplete]]
24+
| None
25+
)
26+
_FlatOrNestedSequenceOfColors: TypeAlias = (
27+
Sequence[ColorType]
28+
| Sequence[Iterable[ColorType]]
29+
| NDArray[Incomplete]
30+
| pd.Index[Incomplete]
31+
| pd.Series[Incomplete]
32+
| pd.DataFrame
33+
)
1934

2035
__all__ = ["heatmap", "clustermap"]
2136

2237
def heatmap(
23-
data: DataFrame | _ArrayLike[Incomplete],
38+
data: pd.DataFrame | ArrayLike,
2439
*,
2540
vmin: float | None = None,
2641
vmax: float | None = None,
@@ -38,44 +53,64 @@ def heatmap(
3853
square: bool = False,
3954
xticklabels: Literal["auto"] | bool | int | Sequence[str] = "auto",
4055
yticklabels: Literal["auto"] | bool | int | Sequence[str] = "auto",
41-
mask: NDArray[np.bool_] | DataFrame | None = None,
56+
mask: NDArray[np.bool_] | pd.DataFrame | None = None,
4257
ax: Axes | None = None,
58+
# Kwargs below passed to matplotlib.axes.Axes.pcolormesh
59+
alpha: float | None = None,
60+
norm: str | Normalize | None = None,
61+
shading: Literal["flat", "nearest", "gouraud", "auto"] | None = None,
62+
antialiased: bool = False,
4363
**kwargs,
4464
) -> Axes: ...
65+
@type_check_only
66+
class _Dendogram(TypedDict):
67+
icoord: list[list[float]]
68+
dcoord: list[list[float]]
69+
ivl: list[str]
70+
leaves: list[int]
71+
color_list: list[str]
72+
leaves_color_list: list[str]
4573

4674
class _DendrogramPlotter:
4775
axis: int
48-
array: NDArray[Incomplete]
49-
data: DataFrame
76+
array: NDArray[np.floating]
77+
data: pd.DataFrame
5078
shape: tuple[int, int]
5179
metric: str
5280
method: str
5381
label: bool
5482
rotate: bool
55-
linkage: NDArray[Incomplete]
56-
dendrogram: dict[str, list[Incomplete]]
57-
xticks: list[float] | NDArray[Incomplete]
58-
yticks: list[float] | NDArray[Incomplete]
83+
linkage: NDArray[np.floating]
84+
dendrogram: _Dendogram
85+
xticks: list[float] | NDArray[np.floating]
86+
yticks: list[float] | NDArray[np.floating]
5987
xticklabels: list[str]
6088
yticklabels: list[str]
6189
ylabel: str
6290
xlabel: str
6391
dependent_coord: list[list[float]]
6492
independent_coord: list[list[float]]
6593
def __init__(
66-
self, data: DataFrame, linkage: NDArray[Incomplete] | None, metric: str, method: str, axis: int, label: bool, rotate: bool
94+
self,
95+
data: pd.DataFrame,
96+
linkage: NDArray[np.floating] | None,
97+
metric: str,
98+
method: str,
99+
axis: int,
100+
label: bool,
101+
rotate: bool,
67102
) -> None: ...
68103
@property
69-
def calculated_linkage(self) -> NDArray[Incomplete]: ...
70-
def calculate_dendrogram(self) -> dict[str, list[Incomplete]]: ...
104+
def calculated_linkage(self) -> NDArray[np.float64]: ...
105+
def calculate_dendrogram(self) -> _Dendogram: ...
71106
@property
72107
def reordered_ind(self) -> list[int]: ...
73108
def plot(self, ax: Axes, tree_kws: dict[str, Incomplete]) -> Self: ...
74109

75110
def dendrogram(
76-
data: DataFrame,
111+
data: pd.DataFrame,
77112
*,
78-
linkage: NDArray[Incomplete] | None = None,
113+
linkage: NDArray[np.floating] | None = None,
79114
axis: int = 1,
80115
label: bool = True,
81116
metric: str = "euclidean",
@@ -86,13 +121,13 @@ def dendrogram(
86121
) -> _DendrogramPlotter: ...
87122

88123
class ClusterGrid(Grid):
89-
data: DataFrame
90-
data2d: DataFrame
91-
mask: DataFrame
92-
row_colors: Incomplete
93-
row_color_labels: Incomplete
94-
col_colors: Incomplete
95-
col_color_labels: Incomplete
124+
data: pd.DataFrame
125+
data2d: pd.DataFrame
126+
mask: pd.DataFrame
127+
row_colors: list[list[tuple[float, float, float]]] | None
128+
row_color_labels: list[str] | None
129+
col_colors: list[list[tuple[float, float, float]]] | None
130+
col_color_labels: list[str] | None
96131
gs: GridSpec
97132
ax_row_dendrogram: Axes
98133
ax_col_dendrogram: Axes
@@ -101,53 +136,47 @@ class ClusterGrid(Grid):
101136
ax_heatmap: Axes
102137
ax_cbar: Axes | None
103138
cax: Axes | None
104-
cbar_pos: Incomplete
139+
cbar_pos: tuple[float, float, float, float] | None
105140
dendrogram_row: _DendrogramPlotter | None
106141
dendrogram_col: _DendrogramPlotter | None
107142
def __init__(
108143
self,
109-
data: (
110-
_ListLikeU
111-
| DataFrame
112-
| dict[Incomplete, Incomplete]
113-
| Iterable[_ListLikeU | tuple[Hashable, _ListLikeU] | dict[Incomplete, Incomplete]]
114-
| None
115-
),
144+
data: _ConvertibleToDataFrame,
116145
pivot_kws: Mapping[str, Incomplete] | None = None,
117146
z_score: int | None = None,
118147
standard_scale: int | None = None,
119148
figsize: tuple[float, float] | None = None,
120-
row_colors=None,
121-
col_colors=None,
122-
mask: NDArray[np.bool_] | DataFrame | None = None,
149+
row_colors: _FlatOrNestedSequenceOfColors | None = None,
150+
col_colors: _FlatOrNestedSequenceOfColors | None = None,
151+
mask: NDArray[np.bool_] | pd.DataFrame | None = None,
123152
dendrogram_ratio: float | tuple[float, float] | None = None,
124153
colors_ratio: float | tuple[float, float] | None = None,
125154
cbar_pos: tuple[float, float, float, float] | None = None,
126155
) -> None: ...
127156
def format_data(
128157
self,
129-
data: DataFrame,
158+
data: pd.DataFrame,
130159
pivot_kws: Mapping[str, Incomplete] | None,
131160
z_score: int | None = None,
132161
standard_scale: int | None = None,
133-
) -> DataFrame: ...
162+
) -> pd.DataFrame: ...
134163
@staticmethod
135-
def z_score(data2d: DataFrame, axis: int = 1) -> DataFrame: ...
164+
def z_score(data2d: pd.DataFrame, axis: int = 1) -> pd.DataFrame: ...
136165
@staticmethod
137-
def standard_scale(data2d: DataFrame, axis: int = 1) -> DataFrame: ...
138-
def dim_ratios(self, colors: Incomplete | None, dendrogram_ratio: float, colors_ratio: float) -> list[float]: ...
166+
def standard_scale(data2d: pd.DataFrame, axis: int = 1) -> pd.DataFrame: ...
167+
def dim_ratios(self, colors: ArrayLike | None, dendrogram_ratio: float, colors_ratio: float) -> list[float]: ...
139168
@staticmethod
140169
def color_list_to_matrix_and_cmap(
141-
colors: Sequence[ColorType], ind: _ArrayLikeInt_co, axis: int = 0
170+
colors: _FlatOrNestedSequenceOfColors, ind: _ArrayLikeInt_co, axis: int = 0
142171
) -> tuple[NDArray[np.int_], ListedColormap]: ...
143172
def plot_dendrograms(
144173
self,
145174
row_cluster: bool,
146175
col_cluster: bool,
147176
metric: str,
148177
method: str,
149-
row_linkage: NDArray[Incomplete] | None,
150-
col_linkage: NDArray[Incomplete] | None,
178+
row_linkage: NDArray[np.floating] | None,
179+
col_linkage: NDArray[np.floating] | None,
151180
tree_kws: dict[str, Incomplete] | None,
152181
) -> None: ...
153182
def plot_colors(self, xind: _ArrayLikeInt_co, yind: _ArrayLikeInt_co, **kws) -> None: ...
@@ -159,20 +188,14 @@ class ClusterGrid(Grid):
159188
colorbar_kws: dict[str, Incomplete] | None,
160189
row_cluster: bool,
161190
col_cluster: bool,
162-
row_linkage: NDArray[Incomplete] | None,
163-
col_linkage: NDArray[Incomplete] | None,
191+
row_linkage: NDArray[np.floating] | None,
192+
col_linkage: NDArray[np.floating] | None,
164193
tree_kws: dict[str, Incomplete] | None,
165194
**kws,
166195
) -> Self: ...
167196

168197
def clustermap(
169-
data: (
170-
_ListLikeU
171-
| DataFrame
172-
| dict[Incomplete, Incomplete]
173-
| Iterable[_ListLikeU | tuple[Hashable, _ListLikeU] | dict[Incomplete, Incomplete]]
174-
| None
175-
),
198+
data: _ConvertibleToDataFrame,
176199
*,
177200
pivot_kws: dict[str, Incomplete] | None = None,
178201
method: str = "average",
@@ -183,11 +206,11 @@ def clustermap(
183206
cbar_kws: dict[str, Incomplete] | None = None,
184207
row_cluster: bool = True,
185208
col_cluster: bool = True,
186-
row_linkage: NDArray[Incomplete] | None = None,
187-
col_linkage: NDArray[Incomplete] | None = None,
188-
row_colors=None,
189-
col_colors=None,
190-
mask: NDArray[np.bool_] | DataFrame | None = None,
209+
row_linkage: NDArray[np.floating] | None = None,
210+
col_linkage: NDArray[np.floating] | None = None,
211+
row_colors: _FlatOrNestedSequenceOfColors | None = None,
212+
col_colors: _FlatOrNestedSequenceOfColors | None = None,
213+
mask: NDArray[np.bool_] | pd.DataFrame | None = None,
191214
dendrogram_ratio: float | tuple[float, float] = 0.2,
192215
colors_ratio: float | tuple[float, float] = 0.03,
193216
cbar_pos: tuple[float, float, float, float] | None = (0.02, 0.8, 0.05, 0.18),

0 commit comments

Comments
 (0)