Skip to content

Commit

Permalink
Merge pull request #166 from Swish78/dynamic-figsize-adjustment
Browse files Browse the repository at this point in the history
Fix issue #30
  • Loading branch information
shakedzy authored Oct 3, 2024
2 parents ea466ad + 2b17541 commit fbf525d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 0.7.8
* `nominal.associations` now attempts to set the figure-size automatically based on output (issue [#30](https://github.com/shakedzy/dython/issues/30), by **[@Swish78](https://github.com/Swish78)**)

## 0.7.7
* _Drop support for Python 3.8 as it reaches its end-of-life date_
* Fix issue [#160](https://github.com/shakedzy/dython/issues/160)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.7.7
0.7.8
2 changes: 1 addition & 1 deletion dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pytest[testing]>=8.3.2
pytest>=8.3.2
hypothesis>=6.111.0
black>=24.8.0
pre-commit>=3.8.0
Expand Down
5 changes: 3 additions & 2 deletions docs/modules/nominal.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,12 @@ continuous features using:

Matplotlib Axis on which the heat-map will be plotted

- **`figsize`** : `(int,int)` or `None`
- **`figsize`** : `(float, float)` or `None`

_Default: None_

A Matplotlib figure-size tuple. If `None`, falls back to Matplotlib's default. Only used if `ax=None`.
A Matplotlib figure-size tuple. If `None`, will attempt to set the size automatically.
Only used if `ax=None`.

- **`annot`** : `Boolean`

Expand Down
30 changes: 18 additions & 12 deletions dython/nominal.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def associations(
nan_strategy: str = _REPLACE,
nan_replace_value: Any = _DEFAULT_REPLACE_VALUE,
ax: Optional[plt.Axes] = None,
figsize: Optional[Tuple[int, int]] = None,
figsize: Optional[Tuple[float, float]] = None,
annot: bool = True,
fmt: str = ".2f",
cmap: Optional[Colormap] = None,
Expand Down Expand Up @@ -439,16 +439,16 @@ def associations(
'(con)' based on their type (nominal or continuous), as provided
by nominal_columns
nom_nom_assoc : callable / string, default = 'cramer'
If callable, a function which recieves two `pd.Series` and returns a single number.
If callable, a function which receives two `pd.Series` and returns a single number.
If string, name of nominal-nominal (categorical-categorical) association to use.
Options are 'cramer' for Cramer's V or `theil` for Theil's U. If 'theil',
heat-map columns are the provided information (U = U(row|col)).
num_num_assoc : callable / string, default = 'pearson'
If callable, a function which recieves two `pd.Series` and returns a single number.
If callable, a function which receives two `pd.Series` and returns a single number.
If string, name of numerical-numerical association to use. Options are 'pearson'
for Pearson's R, 'spearman' for Spearman's R, 'kendall' for Kendall's Tau.
nom_num_assoc : callable / string, default = 'correlation_ratio'
If callable, a function which recieves two `pd.Series` and returns a single number.
If callable, a function which receives two `pd.Series` and returns a single number.
If string, name of nominal-numerical association to use. Options are 'correlation_ratio'
for correlation ratio.
symmetric_nom_nom : Boolean, default = True
Expand All @@ -458,19 +458,19 @@ def associations(
Relevant only if `num_num_assoc` is a callable. Declare whether the function is symmetric (f(x,y) = f(y,x)).
If False, heat-map values should be interpreted as f(row,col)
display_rows : list / string, default = 'all'
Choose which of the dataset's features will be displyed in the output's
Choose which of the dataset's features will be displayed in the output's
correlations table rows. If string, can either be a single feature's name or 'all'.
Only used if `hide_rows` is `None`.
display_columns : list / string, default = 'all'
Choose which of the dataset's features will be displyed in the output's
Choose which of the dataset's features will be displayed in the output's
correlations table columns. If string, can either be a single feature's name or 'all'.
Only used if `hide_columns` is `None`.
hide_rows : list / string, default = None
Choose which of the dataset's features will not be displyed in the output's
Choose which of the dataset's features will not be displayed in the output's
correlations table rows. If string, must be a single feature's name. If `None`,
`display_rows` is used.
hide_columns : list / string, default = None
Choose which of the dataset's features will not be displyed in the output's
Choose which of the dataset's features will not be displayed in the output's
correlations table columns. If string, must be a single feature's name. If `None`,
`display_columns` is used.
cramers_v_bias_correction : Boolean, default = True
Expand All @@ -488,9 +488,9 @@ def associations(
nan_strategy is set to 'replace'
ax : matplotlib ax, default = None
Matplotlib Axis on which the heat-map will be plotted
figsize : (int,int) or None, default = None
A Matplotlib figure-size tuple. If `None`, falls back to Matplotlib's
default. Only used if `ax=None`.
figsize : (float, float) or None, default = None
A Matplotlib figure-size tuple. If `None`, will attempt to set the size automatically.
Only used if `ax=None`.
annot : Boolean, default = True
Plot number annotations on the heat-map
fmt : string, default = '.2f'
Expand Down Expand Up @@ -564,7 +564,7 @@ def associations(
pass # will be handled pair-by-pair during calculations
else:
raise ValueError(
"Argument nan_stragety [{:s}] is not a valid choice.".format(
"Argument nan_strategy [{:s}] is not a valid choice.".format(
nan_strategy
)
)
Expand Down Expand Up @@ -614,6 +614,12 @@ def associations(
)
displayed_features_set = set.union(set(display_rows), set(display_columns))

# Adjusting figsize based on the number of features
if figsize is None:
BASE_SIZE = 1.5 # Size multiplier per feature
num_features = len(displayed_features_set)
figsize = (BASE_SIZE * num_features, BASE_SIZE * num_features)

# convert timestamp columns to numerical columns, so correlation can be performed
datetime_dtypes = [
str(x) for x in df.dtypes if str(x).startswith("datetime64")
Expand Down

0 comments on commit fbf525d

Please sign in to comment.