Skip to content

Seaborn¤

The seaborn submodule contains methods of producing visualizations using the Seaborn visualization library.

The seaborn submodule contains components for generating word clouds and cluster analysis plots.

Seaborn Clustermaps¤

lexos.visualization.seaborn.cluster.clustermap.ClusterMap ¤

ClusterMap.

Source code in lexos\visualization\seaborn\cluster\clustermap.py
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
class ClusterMap:
    """ClusterMap."""

    def __init__(
        self,
        dtm: Any,
        z_score: int = 1,
        labels: List[str] = None,
        pivot_kws: Dict[str, str] = None,
        method: str = "average",
        metric: str = "euclidean",
        standard_scale: int = None,
        figsize: tuple = (8, 8),
        cbar_kws: dict = None,
        row_cluster: bool = True,
        col_cluster: bool = True,
        row_linkage: bool = None,
        col_linkage: bool = None,
        row_colors: Union[list, pd.DataFrame, pd.Series] = None,
        col_colors: Union[list, pd.DataFrame, pd.Series] = None,
        mask: Union[np.ndarray, pd.DataFrame] = None,
        dendrogram_ratio: Union[float, Tuple[float]] = (0.1, 0.2),
        colors_ratio: float = 0.03,
        cbar_pos: Tuple[str] = (0.02, 0.32, 0.03, 0.2),
        tree_kws: dict = None,
        center: int = 0,
        cmap: str = "vlag",
        linewidths: float = 0.75,
        show: bool = False,
        title: str = None,
    ) -> figure.Figure:
        """Initialize the ClusterMap object.

        Args:
            dtm (Any): The data to cluster.
            z_score (int, optional): The z-score to use. Defaults to 1.
            labels (List[str], optional): The labels to use. Defaults to None.
            pivot_kws (Dict[str, str], optional): The pivot kwargs. Defaults to None.
            method (str, optional): The method to use. Defaults to "average".
            metric (str, optional): The metric to use. Defaults to "euclidean".
            standard_scale (int, optional): The standard scale to use. Defaults to None.
            figsize (tuple, optional): The figure size to use. Defaults to (8, 8).
            cbar_kws (dict, optional): The cbar kwargs. Defaults to None.
            row_cluster (bool, optional): Whether to cluster the rows. Defaults to True.
            col_cluster (bool, optional): Whether to cluster the columns. Defaults to True.
            row_linkage (bool, optional): Whether to use row linkage. Defaults to None.
            col_linkage (bool, optional): Whether to use column linkage. Defaults to None.
            row_colors (Union[list, pd.DataFrame, pd.Series], optional): The row colors. Defaults to None.
            col_colors (Union[list, pd.DataFrame, pd.Series], optional): The column colors. Defaults to None.
            mask (Union[np.ndarray, pd.DataFrame], optional): The mask to use. Defaults to None.
            dendrogram_ratio (Union[float, Tuple[float]], optional): The dendrogram ratio to use. Defaults to (.1, .2).
            colors_ratio (float, optional): The colors ratio to use. Defaults to 0.03.
            cbar_pos (Tuple[str], optional): The cbar position to use. Defaults to (.02, .32, .03, .2).
            tree_kws (dict, optional): The tree kwargs. Defaults to None.
            center (int, optional): The center to use. Defaults to 0.
            cmap (str, optional): The cmap to use. Defaults to "vlag".
            linewidths (float, optional): The linewidths to use. Defaults to .75.
            show (bool, optional): Whether to show the figure. Defaults to False.
            title (str, optional): The title to use. Defaults to None.

        Returns:
            matplotlib.figure.Figure: The figure.
        """
        self.dtm = dtm
        self.z_score = z_score
        self.labels = labels
        self.figsize = figsize
        self.show = show
        self.method = method
        self.metric = metric
        self.standard_scale = standard_scale
        self.title = title
        self.row_colors = row_colors
        self.col_colors = col_colors
        self.pivot_kws = pivot_kws
        self.cbar_kws = cbar_kws
        self.row_cluster = row_cluster
        self.col_cluster = col_cluster
        self.row_linkage = row_linkage
        self.col_linkage = col_linkage
        self.mask = mask
        self.dendrogram_ratio = dendrogram_ratio
        self.colors_ratio = colors_ratio
        self.cbar_pos = cbar_pos
        self.tree_kws = tree_kws
        self.center = center
        self.cmap = cmap
        self.linewidths = linewidths
        self.df = dtm.get_table()
        self.build()

    def build(self):
        """Build the clustermap."""
        # Get the labels if necessary
        if self.labels is None:
            # raise ValueError("Please provide labels.")
            self.labels = self.df.columns.values.tolist()[1:]

        # Convert palette to vectors drawn on the side of the matrix
        if self.row_colors is None or self.col_colors is None:
            column_pal = sns.husl_palette(8, s=0.45)
            column_lut = dict(zip(map(str, self.df), column_pal))
            column_colors = pd.Series(self.labels, index=self.df.columns[1:]).map(
                column_lut
            )
            if self.row_colors is None:
                self.row_colors = column_colors
            if self.col_colors is None:
                self.col_colors = column_colors

        # Perform the cluster
        g = sns.clustermap(
            self.df.corr(),
            cmap=self.cmap,
            method=self.method,
            metric=self.metric,
            figsize=self.figsize,
            row_colors=self.row_colors,
            col_colors=self.col_colors,
            row_cluster=self.row_cluster,
            col_cluster=self.col_cluster,
            center=self.center,
            linewidths=self.linewidths,
            z_score=self.z_score,
            pivot_kws=self.pivot_kws,
            standard_scale=self.standard_scale,
            cbar_kws=self.cbar_kws,
            row_linkage=self.row_linkage,
            col_linkage=self.col_linkage,
            mask=self.mask,
            dendrogram_ratio=self.dendrogram_ratio,
            colors_ratio=self.colors_ratio,
            cbar_pos=self.cbar_pos,
            tree_kws=self.tree_kws,
        )

        # Remove the dendrogram on the left
        g.ax_row_dendrogram.remove()

        # Add the title
        if self.title:
            g.fig.suptitle(self.title, y=1.05)

        # Save the fig variable
        self.fig = g.fig

        # Suppress the output
        if not self.show:
            plt.close()
            return self.fig

    def savefig(self, filename: str):
        """Show the figure if it is hidden.

        Args:
            filename (str): The name of the file to save.
        """
        self.fig.savefig(filename)

    def showfig(self):
        """Show the figure if it is hidden.

        This is a helper method. You can also reference the figure
        using `ClusterMap.fig`. This will generally display in a
        Jupyter notebook.
        """
        return self.fig

__init__(dtm, z_score=1, labels=None, pivot_kws=None, method='average', metric='euclidean', standard_scale=None, figsize=(8, 8), cbar_kws=None, row_cluster=True, col_cluster=True, row_linkage=None, col_linkage=None, row_colors=None, col_colors=None, mask=None, dendrogram_ratio=(0.1, 0.2), colors_ratio=0.03, cbar_pos=(0.02, 0.32, 0.03, 0.2), tree_kws=None, center=0, cmap='vlag', linewidths=0.75, show=False, title=None) ¤

Initialize the ClusterMap object.

Parameters:

Name Type Description Default
dtm Any

The data to cluster.

required
z_score int

The z-score to use. Defaults to 1.

1
labels List[str]

The labels to use. Defaults to None.

None
pivot_kws Dict[str, str]

The pivot kwargs. Defaults to None.

None
method str

The method to use. Defaults to "average".

'average'
metric str

The metric to use. Defaults to "euclidean".

'euclidean'
standard_scale int

The standard scale to use. Defaults to None.

None
figsize tuple

The figure size to use. Defaults to (8, 8).

(8, 8)
cbar_kws dict

The cbar kwargs. Defaults to None.

None
row_cluster bool

Whether to cluster the rows. Defaults to True.

True
col_cluster bool

Whether to cluster the columns. Defaults to True.

True
row_linkage bool

Whether to use row linkage. Defaults to None.

None
col_linkage bool

Whether to use column linkage. Defaults to None.

None
row_colors Union[list, pd.DataFrame, pd.Series]

The row colors. Defaults to None.

None
col_colors Union[list, pd.DataFrame, pd.Series]

The column colors. Defaults to None.

None
mask Union[np.ndarray, pd.DataFrame]

The mask to use. Defaults to None.

None
dendrogram_ratio Union[float, Tuple[float]]

The dendrogram ratio to use. Defaults to (.1, .2).

(0.1, 0.2)
colors_ratio float

The colors ratio to use. Defaults to 0.03.

0.03
cbar_pos Tuple[str]

The cbar position to use. Defaults to (.02, .32, .03, .2).

(0.02, 0.32, 0.03, 0.2)
tree_kws dict

The tree kwargs. Defaults to None.

None
center int

The center to use. Defaults to 0.

0
cmap str

The cmap to use. Defaults to "vlag".

'vlag'
linewidths float

The linewidths to use. Defaults to .75.

0.75
show bool

Whether to show the figure. Defaults to False.

False
title str

The title to use. Defaults to None.

None

Returns:

Type Description
figure.Figure

matplotlib.figure.Figure: The figure.

Source code in lexos\visualization\seaborn\cluster\clustermap.py
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def __init__(
    self,
    dtm: Any,
    z_score: int = 1,
    labels: List[str] = None,
    pivot_kws: Dict[str, str] = None,
    method: str = "average",
    metric: str = "euclidean",
    standard_scale: int = None,
    figsize: tuple = (8, 8),
    cbar_kws: dict = None,
    row_cluster: bool = True,
    col_cluster: bool = True,
    row_linkage: bool = None,
    col_linkage: bool = None,
    row_colors: Union[list, pd.DataFrame, pd.Series] = None,
    col_colors: Union[list, pd.DataFrame, pd.Series] = None,
    mask: Union[np.ndarray, pd.DataFrame] = None,
    dendrogram_ratio: Union[float, Tuple[float]] = (0.1, 0.2),
    colors_ratio: float = 0.03,
    cbar_pos: Tuple[str] = (0.02, 0.32, 0.03, 0.2),
    tree_kws: dict = None,
    center: int = 0,
    cmap: str = "vlag",
    linewidths: float = 0.75,
    show: bool = False,
    title: str = None,
) -> figure.Figure:
    """Initialize the ClusterMap object.

    Args:
        dtm (Any): The data to cluster.
        z_score (int, optional): The z-score to use. Defaults to 1.
        labels (List[str], optional): The labels to use. Defaults to None.
        pivot_kws (Dict[str, str], optional): The pivot kwargs. Defaults to None.
        method (str, optional): The method to use. Defaults to "average".
        metric (str, optional): The metric to use. Defaults to "euclidean".
        standard_scale (int, optional): The standard scale to use. Defaults to None.
        figsize (tuple, optional): The figure size to use. Defaults to (8, 8).
        cbar_kws (dict, optional): The cbar kwargs. Defaults to None.
        row_cluster (bool, optional): Whether to cluster the rows. Defaults to True.
        col_cluster (bool, optional): Whether to cluster the columns. Defaults to True.
        row_linkage (bool, optional): Whether to use row linkage. Defaults to None.
        col_linkage (bool, optional): Whether to use column linkage. Defaults to None.
        row_colors (Union[list, pd.DataFrame, pd.Series], optional): The row colors. Defaults to None.
        col_colors (Union[list, pd.DataFrame, pd.Series], optional): The column colors. Defaults to None.
        mask (Union[np.ndarray, pd.DataFrame], optional): The mask to use. Defaults to None.
        dendrogram_ratio (Union[float, Tuple[float]], optional): The dendrogram ratio to use. Defaults to (.1, .2).
        colors_ratio (float, optional): The colors ratio to use. Defaults to 0.03.
        cbar_pos (Tuple[str], optional): The cbar position to use. Defaults to (.02, .32, .03, .2).
        tree_kws (dict, optional): The tree kwargs. Defaults to None.
        center (int, optional): The center to use. Defaults to 0.
        cmap (str, optional): The cmap to use. Defaults to "vlag".
        linewidths (float, optional): The linewidths to use. Defaults to .75.
        show (bool, optional): Whether to show the figure. Defaults to False.
        title (str, optional): The title to use. Defaults to None.

    Returns:
        matplotlib.figure.Figure: The figure.
    """
    self.dtm = dtm
    self.z_score = z_score
    self.labels = labels
    self.figsize = figsize
    self.show = show
    self.method = method
    self.metric = metric
    self.standard_scale = standard_scale
    self.title = title
    self.row_colors = row_colors
    self.col_colors = col_colors
    self.pivot_kws = pivot_kws
    self.cbar_kws = cbar_kws
    self.row_cluster = row_cluster
    self.col_cluster = col_cluster
    self.row_linkage = row_linkage
    self.col_linkage = col_linkage
    self.mask = mask
    self.dendrogram_ratio = dendrogram_ratio
    self.colors_ratio = colors_ratio
    self.cbar_pos = cbar_pos
    self.tree_kws = tree_kws
    self.center = center
    self.cmap = cmap
    self.linewidths = linewidths
    self.df = dtm.get_table()
    self.build()

build() ¤

Build the clustermap.

Source code in lexos\visualization\seaborn\cluster\clustermap.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def build(self):
    """Build the clustermap."""
    # Get the labels if necessary
    if self.labels is None:
        # raise ValueError("Please provide labels.")
        self.labels = self.df.columns.values.tolist()[1:]

    # Convert palette to vectors drawn on the side of the matrix
    if self.row_colors is None or self.col_colors is None:
        column_pal = sns.husl_palette(8, s=0.45)
        column_lut = dict(zip(map(str, self.df), column_pal))
        column_colors = pd.Series(self.labels, index=self.df.columns[1:]).map(
            column_lut
        )
        if self.row_colors is None:
            self.row_colors = column_colors
        if self.col_colors is None:
            self.col_colors = column_colors

    # Perform the cluster
    g = sns.clustermap(
        self.df.corr(),
        cmap=self.cmap,
        method=self.method,
        metric=self.metric,
        figsize=self.figsize,
        row_colors=self.row_colors,
        col_colors=self.col_colors,
        row_cluster=self.row_cluster,
        col_cluster=self.col_cluster,
        center=self.center,
        linewidths=self.linewidths,
        z_score=self.z_score,
        pivot_kws=self.pivot_kws,
        standard_scale=self.standard_scale,
        cbar_kws=self.cbar_kws,
        row_linkage=self.row_linkage,
        col_linkage=self.col_linkage,
        mask=self.mask,
        dendrogram_ratio=self.dendrogram_ratio,
        colors_ratio=self.colors_ratio,
        cbar_pos=self.cbar_pos,
        tree_kws=self.tree_kws,
    )

    # Remove the dendrogram on the left
    g.ax_row_dendrogram.remove()

    # Add the title
    if self.title:
        g.fig.suptitle(self.title, y=1.05)

    # Save the fig variable
    self.fig = g.fig

    # Suppress the output
    if not self.show:
        plt.close()
        return self.fig

savefig(filename) ¤

Show the figure if it is hidden.

Parameters:

Name Type Description Default
filename str

The name of the file to save.

required
Source code in lexos\visualization\seaborn\cluster\clustermap.py
165
166
167
168
169
170
171
def savefig(self, filename: str):
    """Show the figure if it is hidden.

    Args:
        filename (str): The name of the file to save.
    """
    self.fig.savefig(filename)

showfig() ¤

Show the figure if it is hidden.

This is a helper method. You can also reference the figure using ClusterMap.fig. This will generally display in a Jupyter notebook.

Source code in lexos\visualization\seaborn\cluster\clustermap.py
173
174
175
176
177
178
179
180
def showfig(self):
    """Show the figure if it is hidden.

    This is a helper method. You can also reference the figure
    using `ClusterMap.fig`. This will generally display in a
    Jupyter notebook.
    """
    return self.fig