-
Notifications
You must be signed in to change notification settings - Fork 850
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
391 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file added
BIN
+18.8 KB
docs/sources/user_guide/plotting/scatter_hist_files/scatter_hist_dataframe.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.5 KB
docs/sources/user_guide/plotting/scatter_hist_files/scatter_hist_numpyarray.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# 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, data): | ||
""" | ||
Scatter plot and individual feature histograms along axes. | ||
Parameters | ||
---------- | ||
x : str or int | ||
DataFrame column name of the x-axis values or | ||
integer for the numpy ndarray column index. | ||
y : str or int | ||
DataFrame column name of the y-axis values or | ||
integer for the numpy ndarray column index | ||
data : Pandas DataFrame object or NumPy ndarray. | ||
Returns | ||
--------- | ||
plot : matplotlib pyplot 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 isinstance(data, pd.DataFrame): | ||
for i in (x, y): | ||
assert (isinstance(i, str)) | ||
frame = True | ||
xlabel = x | ||
ylabel = y | ||
x = data[x] | ||
y = data[y] | ||
|
||
elif isinstance(data, np.ndarray): | ||
for i in (x, y): | ||
assert (isinstance(i, int)) | ||
frame = False | ||
x = data[:, x] | ||
y = data[:, y] | ||
|
||
else: | ||
raise ValueError('df must be pandas.DataFrame or numpy.ndarray object') | ||
|
||
fig = plt.figure(figsize=(5, 5)) | ||
ax = fig.add_axes(rect_scatter) | ||
if frame: | ||
ax.set_xlabel(xlabel) | ||
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, edgecolor='white', bins='auto') | ||
ax_histy.hist(y, edgecolor='white', orientation='horizontal', bins='auto') | ||
plot = ax.scatter(x, y) | ||
return plot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from mlxtend.data import iris_data | ||
from mlxtend.plotting import scatter_hist | ||
import pandas as pd | ||
import pytest | ||
|
||
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("sepal length [cm]", "sepal width [cm]", df) | ||
|
||
|
||
def test_pass_data_as_numpy_array(): | ||
scatter_hist(0, 1, X) | ||
|
||
|
||
def test_incorrect_x_or_y_data_as_dataframe(): | ||
with pytest.raises(AssertionError) as execinfo: | ||
scatter_hist(0, "sepal width [cm]", df) | ||
assert execinfo.value.message == 'Assertion failed' | ||
|
||
|
||
def test_incorrect_x_or_y_data_as_numpy_array(): | ||
with pytest.raises(AssertionError) as execinfo: | ||
scatter_hist("sepal length [cm]", 1, X) | ||
assert execinfo.value.message == 'Assertion failed' |