Skip to content

ENH: scatter_matrix new parameter nondiagonal to allow hexbin plots i… #56888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pandas/plotting/_matplotlib/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def scatter_matrix(
ax=None,
grid: bool = False,
diagonal: str = "hist",
nondiagonal: str = "scatter",
marker: str = ".",
density_kwds=None,
hist_kwds=None,
Expand Down Expand Up @@ -93,9 +94,14 @@ def scatter_matrix(
else:
common = (mask[a] & mask[b]).values

ax.scatter(
df[b][common], df[a][common], marker=marker, alpha=alpha, **kwds
)
if nondiagonal == "scatter":
ax.scatter(
df[b][common], df[a][common], marker=marker, alpha=alpha, **kwds
)
elif nondiagonal == "hexbin":
ax.hexbin(
df[b][common], df[a][common], alpha=alpha, **kwds
)

ax.set_xlim(boundaries_list[j])
ax.set_ylim(boundaries_list[i])
Expand Down
5 changes: 5 additions & 0 deletions pandas/plotting/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def scatter_matrix(
ax: Axes | None = None,
grid: bool = False,
diagonal: str = "hist",
nondiagonal: str = "scatter",
marker: str = ".",
density_kwds: Mapping[str, Any] | None = None,
hist_kwds: Mapping[str, Any] | None = None,
Expand All @@ -184,6 +185,9 @@ def scatter_matrix(
diagonal : {'hist', 'kde'}
Pick between 'kde' and 'hist' for either Kernel Density Estimation or
Histogram plot in the diagonal.
nondiagonal : {'scatter', 'hexbin'}
Pick between 'scatter' and 'hexbin' for either scatter or
2D hexagonal binning plot in the diagonal.
marker : str, optional
Matplotlib marker type, default '.'.
density_kwds : keywords
Expand Down Expand Up @@ -227,6 +231,7 @@ def scatter_matrix(
ax=ax,
grid=grid,
diagonal=diagonal,
nondiagonal=nondiagonal,
marker=marker,
density_kwds=density_kwds,
hist_kwds=hist_kwds,
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/plotting/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@ def test_scatter_matrix_axis_smaller(self, pass_axis):
_check_text_labels(axes0_labels, expected)
_check_ticks_props(axes, xlabelsize=8, xrot=90, ylabelsize=8, yrot=0)

@pytest.mark.parametrize("nondiagonal", ["scatter", "hexbin"])
def test_scatter_matrix_nondiagonal(self, nondiagonal):
pytest.importorskip("scipy")
scatter_matrix = plotting.scatter_matrix
df = DataFrame(np.random.default_rng(2).standard_normal((100, 3)))
_check_plot_works(
scatter_matrix,
frame=df,
nondiagonal=nondiagonal,
)

@pytest.mark.slow
def test_andrews_curves_no_warning(self, iris):
from pandas.plotting import andrews_curves
Expand Down