Last active
February 6, 2022 22:07
-
-
Save cwindolf/0224091e7017ce9d558241e9d4a9d979 to your computer and use it in GitHub Desktop.
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
def tukey_scatter(x, y, iqrs=1.5, ax=None, **kwargs): | |
ax = ax or plt.gca() | |
x_25, x_75 = np.percentile(x, [25, 75]) | |
x_iqr = x_75 - x_25 | |
y_25, y_75 = np.percentile(x, [25, 75]) | |
y_iqr = y_75 - y_25 | |
inliers = np.flatnonzero( | |
(x_25 - iqrs * x_iqr < x) | |
& (x < x_75 + iqrs * x_iqr) | |
& (y_25 - iqrs * y_iqr < y) | |
& (y < y_75 + iqrs * y_iqr) | |
) | |
for k, v in kwargs.items(): | |
if isinstance(v, np.ndarray) and v.shape == x.shape: | |
kwargs[k] = v[inliers] | |
return ax.scatter(x[inliers], y[inliers], **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment