Skip to content
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

New Feature: Scatter Histogram #757

Merged
merged 17 commits into from
Mar 1, 2021
Merged
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
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ nav:
- user_guide/plotting/plot_linear_regression.md
- user_guide/plotting/plot_sequential_feature_selection.md
- user_guide/plotting/scatterplotmatrix.md
- user_guide/plotting/scatter_hist.md
- user_guide/plotting/stacked_barplot.md
- preprocessing:
- user_guide/preprocessing/CopyTransformer.md
Expand Down
3 changes: 2 additions & 1 deletion docs/sources/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The CHANGELOG for the current development version is available at
##### New Features

- Adds a second "balanced accuracy" interpretation ("balanced") to `accuracy_score` in addition to the existing "average" option to compute the scikit-learn-style balanced accuracy. ([#764](https://github.com/rasbt/mlxtend/pull/764))
- Adds new `scatter_hist` function to `mlxtend.plotting` for generating a scattered histogram. ([#757](https://github.com/rasbt/mlxtend/issues/757) via [Maitreyee Mhasaka](https://github.com/Maitreyee1))

##### Changes

Expand Down Expand Up @@ -834,4 +835,4 @@ imput arrays via `transform` and `fit_transform`

### Version 0.1.1 (2014-08-13)

- Simplified code for ColumnSelector.
- Simplified code for ColumnSelector.
1 change: 1 addition & 0 deletions docs/sources/USER_GUIDE_INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
- [plot_linear_regression](user_guide/plotting/plot_linear_regression.md)
- [plot_sequential_feature_selection](user_guide/plotting/plot_sequential_feature_selection.md)
- [scatterplotmatrix](user_guide/plotting/scatterplotmatrix.md)
- [scatter_hist](user_guide/plotting/scatter_hist.md)
- [stacked_barplot](user_guide/plotting/stacked_barplot.md)

## `preprocessing`
Expand Down
341 changes: 341 additions & 0 deletions docs/sources/user_guide/plotting/scatter_hist.ipynb

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions mlxtend/plotting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
from .ecdf import ecdf
from .scatterplotmatrix import scatterplotmatrix
from .pca_correlation_graph import plot_pca_correlation_graph


from .scatter_hist import scatter_hist
__all__ = ["plot_learning_curves",
"plot_decision_regions",
"plot_confusion_matrix",
Expand All @@ -34,4 +33,5 @@
"checkerboard_plot",
"ecdf",
"scatterplotmatrix",
"plot_pca_correlation_graph"]
"plot_pca_correlation_graph",
"scatter_hist"]
79 changes: 79 additions & 0 deletions mlxtend/plotting/scatter_hist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Sebastian Raschka 2014-2020
# mlxtend Machine Learning Library Extensions
# Author: Sebastian Raschka <sebastianraschka.com>
#
# License: BSD 3 clause
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


def scatter_hist(x, y, xlabel=None, ylabel=None, figsize=(5, 5)):
"""
Scatter plot and individual feature histograms along axes.

Parameters
----------
x : 1D array-like or Pandas Series
X-axis values.

y : 1D array-like or Pandas Series
Y-axis values.

xlabel : str (default: `None`)
Label for the X-axis values. If `x` is a pandas Series,
and `xlabel` is `None`, the label is inferred automatically.

ylabel : str (default: `None`)
Label for the X-axis values. If `y` is a pandas Series,
and `ylabel` is `None`, the label is inferred automatically.

figsize : tuple (default: `(5, 5)`)
Matplotlib figure size.

Returns
---------
plot : Matplotlib Figure object

"""
left, width = 0.1, 0.65
bottom, height = 0.1, 0.65
spacing = 0.001
rect_scatter = [left, bottom, width, height]
rect_histx = [left, bottom + height + spacing, width, 0.2]
rect_histy = [left + width + spacing, bottom, 0.2, height]

if hasattr(x, 'values'):
x_values = x.values
if xlabel is None:
xlabel = x.name
else:
x_values = x

if hasattr(y, 'values'):
y_values = y.values
if ylabel is None:
ylabel = y.name
else:
y_values = y


fig = plt.figure(figsize=figsize)
ax = fig.add_axes(rect_scatter)

if xlabel is not None:
ax.set_xlabel(xlabel)
if ylabel is not None:
ax.set_ylabel(ylabel)

ax_histx = fig.add_axes(rect_histx, sharex=ax)
ax_histy = fig.add_axes(rect_histy, sharey=ax)
ax_histx.tick_params(axis="x", labelbottom=False)
ax_histy.tick_params(axis="y", labelleft=False)
ax_histx.axis("off")
ax_histy.axis("off")
ax_histx.hist(x_values, edgecolor='white', bins='auto')
ax_histy.hist(y_values, edgecolor='white',
orientation='horizontal', bins='auto')
plot = ax.scatter(x_values, y_values)
return plot
19 changes: 19 additions & 0 deletions mlxtend/plotting/tests/test_scatter_hist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from mlxtend.data import iris_data
from mlxtend.plotting import scatter_hist
import pandas as pd


X, y = iris_data()
df = pd.DataFrame(X)
df.columns = (['sepal length [cm]',
'sepal width [cm]',
'petal length [cm]',
'petal width [cm]'])


def test_pass_data_as_dataframe():
scatter_hist(df["sepal length [cm]"], df["sepal width [cm]"])


def test_pass_data_as_numpy_array():
scatter_hist(X[:, 0], X[:, 1])