Skip to content

Commit

Permalink
feat: make contingency table cell size flexible by default
Browse files Browse the repository at this point in the history
Resolves #55
  • Loading branch information
mbelak-dtml committed Nov 1, 2023
1 parent 2502938 commit 71a8b0a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions edvart/report_sections/bivariate_analysis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import itertools
from enum import IntEnum
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, Union

import matplotlib.pyplot as plt
import nbformat.v4 as nbfv4
Expand Down Expand Up @@ -927,7 +927,7 @@ def contingency_table(
hide_zeros: bool = True,
scaling_func: Callable[[np.ndarray], np.ndarray] = np.cbrt,
colormap: Any = "Blues",
size_factor: float = 0.7,
size_factor: Union[float, Literal["auto"]] = "auto",
fontsize: float = 15,
) -> None:
"""
Expand All @@ -950,8 +950,9 @@ def contingency_table(
Cube root is used by default.
colormap : Any (default = "Blues")
Colormap compatible with matplotlib/seaborn.
size_factor : float (default = 0.7)
size_factor : float or "auto"
Size of each cell in the table.
If "auto", the cell size is automatically adjusted so that the numbers fit in the cells.
fontsize : float (default = 15)
Size of the font for axis labels.
"""
Expand Down Expand Up @@ -980,6 +981,12 @@ def contingency_table(
annot_kws={"fontsize": fontsize},
square=True,
)
if size_factor == "auto":
n_digits_max = np.floor(np.log10(table.max().max()))
size_factor = max(
0.72,
0.18 * (1 + n_digits_max),
)
ax.figure.set_size_inches(size_factor * len(table.columns), size_factor * len(table))

ax.set_xticklabels(ax.get_xticklabels(), fontsize=fontsize)
Expand Down

0 comments on commit 71a8b0a

Please sign in to comment.