-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathheatmap_plot.py
More file actions
103 lines (86 loc) · 3.04 KB
/
heatmap_plot.py
File metadata and controls
103 lines (86 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
# Copyright 2024 Flower Labs GmbH. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Label distribution heatmap plotting."""
from typing import Any, Optional, Union
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import colors as mcolors
from matplotlib import pyplot as plt
from matplotlib.axes import Axes
def _plot_heatmap( # pylint: disable=R0913, R0917
dataframe: pd.DataFrame,
axis: Optional[Axes],
figsize: Optional[tuple[float, float]],
title: str,
colormap: Optional[Union[str, mcolors.Colormap]],
partition_id_axis: str,
size_unit: str,
legend: bool,
legend_title: Optional[str],
plot_kwargs: Optional[dict[str, Any]],
legend_kwargs: Optional[dict[str, Any]],
) -> Axes:
if axis is None:
if figsize is None:
figsize = _initialize_figsize(
partition_id_axis=partition_id_axis,
num_partitions=dataframe.shape[0],
num_labels=dataframe.shape[1],
)
_, axis = plt.subplots(figsize=figsize)
# Handle plot_kwargs
if plot_kwargs is None:
plot_kwargs = {}
# Handle optional parameters
if colormap is not None:
plot_kwargs["cmap"] = colormap
elif "cmap" not in plot_kwargs:
plot_kwargs["cmap"] = sns.light_palette("seagreen", as_cmap=True)
if "fmt" not in plot_kwargs:
plot_kwargs["fmt"] = ",d" if size_unit == "absolute" else "0.2f"
if legend_kwargs is None:
legend_kwargs = {}
if legend:
plot_kwargs["cbar"] = True
if legend_title is not None:
legend_kwargs["label"] = legend_title
else:
legend_kwargs["label"] = _initialize_cbar_title(size_unit)
else:
plot_kwargs["cbar"] = False
if partition_id_axis == "x":
dataframe = dataframe.T
sns.heatmap(
dataframe,
ax=axis,
**plot_kwargs,
cbar_kws=legend_kwargs,
)
axis.set_title(title)
return axis
def _initialize_figsize(
partition_id_axis: str,
num_partitions: int,
num_labels: int,
) -> tuple[float, float]:
figsize = (0.0, 0.0)
if partition_id_axis == "x":
figsize = (3 * np.sqrt(num_partitions), np.sqrt(num_labels))
elif partition_id_axis == "y":
figsize = (3 * np.sqrt(num_labels), np.sqrt(num_partitions))
return figsize
def _initialize_cbar_title(size_unit: str) -> Optional[str]:
return "Count" if size_unit == "absolute" else "Percent %"