<jupyter_start><jupyter_text>Plotting Outliers - Examples<jupyter_code>import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
import pandas as pd
from outlier_plotting.sns import handle_outliers<jupyter_output><empty_output><jupyter_text>Test Data<jupyter_code>np.random.seed(42)
df = pd.DataFrame()

df['value'] = np.hstack([-1000, -100, -10, np.random.normal(0, 1, 50), 10, 100, 1000])
df['type'] = np.random.choice(['cat', 'dog', 'bass'], len(df))
df['size'] = np.random.choice([1, 2, 3], len(df))
df['weight'] = np.random.choice([10, 20], len(df))
df['color'] = np.random.choice(['red', 'brown'], len(df))

df.head()<jupyter_output><empty_output><jupyter_text>Functionality `handle_outliers` remove outliers from the plot and show them as text boxes. It can be used with most `seaborn` plotting function that works with long-form data.Please not that only inliers are passed into the plotting function, consequently density estimates, and functionals are only computed on that subset and **not** representative of the whole data.Notable exceptions are:- `countplot`- `lineplot`- `scatterplot`<jupyter_code>plt.title('Showing Outliers')
sns.boxplot(data=df, y = 'type', x='value')
plt.show()

plt.title('With Outlier Handling')
handle_outliers(data=df, y = 'type', x='value', plotter=sns.boxplot)
plt.show()<jupyter_output><empty_output><jupyter_text>Different Plotters<jupyter_code>for plotter in (sns.pointplot, sns.barplot, sns.swarmplot, sns.stripplot, sns.violinplot, sns.boxplot, 
                sns.boxenplot):
    plt.title(plotter.__name__)
    handle_outliers(data=df, plotter = plotter, x = 'type', y='value')
    plt.show()
handle_outliers(data=df['value'], plotter=sns.kdeplot);<jupyter_output><empty_output><jupyter_text>Data Formats<jupyter_code>handle_outliers(data=df['value'].values)
plt.show()

handle_outliers(x=df['value'])
plt.show()

handle_outliers(y=df.value, x=df.weight);<jupyter_output><empty_output><jupyter_text>Outlier Detection A multiple of the IQR is used to detect outliers.<jupyter_code>plt.title('Normal inlier_range')
handle_outliers(data=df, y = 'type', x='value')
plt.show()

plt.title('High inlier_range')
handle_outliers(data=df, y = 'type', x='value', inlier_range = 10)
plt.show()

plt.title('Low inlier_range')
handle_outliers(data=df, y = 'type', x='value', inlier_range = 0);<jupyter_output><empty_output><jupyter_text>Padding and Margin Due to the impossibility of determining the size of `matplotlib` text, liberal margins have to be added.Some padding (5% by default) is added between the plot and text.<jupyter_code>plt.title('Normal margin and padding')
handle_outliers(data=df, y = 'type', x='value')
plt.show()

plt.title('No margin')
handle_outliers(data=df, y = 'type', x='value', margin = 0)
plt.show()

plt.title('High margin')
handle_outliers(data=df, y = 'type', x='value', margin = .5)
plt.show()

plt.title('No padding')
handle_outliers(data=df, y = 'type', x='value', padding = 0)
plt.show()

plt.title('High padding')
handle_outliers(data=df, y = 'type', x='value', padding = .5)
plt.show()<jupyter_output><empty_output><jupyter_text>Hue Due to the aforementioned impossibility of determining the size of `matplotlib` text, it is currently not possible to appropriately plot data with `hue`s.Rendering text in different colors (which would be the most elegant solution) cannot be done without creating multiple `text`s, which would collide since there extents are unknown.<jupyter_code>handle_outliers(data=df, plotter = sns.violinplot, x = 'type', y='value', hue='size')
plt.show()
handle_outliers(data=df, plotter = sns.violinplot, hue = 'type', y='value', x='size', inlier_range=-.2, margin=.7)
plt.legend(loc='upper center');<jupyter_output><empty_output><jupyter_text>catplot<jupyter_code>handle_outliers(data=df, x='type', y='value', hue='size', col='weight', row='color', plotter=sns.catplot)
plt.show()

handle_outliers(data=df, x='type', y='value', hue='size', col='weight', plotter=sns.catplot,
               kind='violin')
plt.show()

handle_outliers(data=df, x='weight', y='value', hue='size', row='type', plotter=sns.catplot, kind='box')
plt.show()<jupyter_output><empty_output><jupyter_text>relplot<jupyter_code>handle_outliers(data=df, y='value', size='size', col='type',  x='color', hue = 'weight', plotter=sns.relplot,
               kind='line')
plt.show()

handle_outliers(data=df, y='value', x='color', plotter=sns.relplot);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15830721003134796, 'fraction_numerical': 0.009180474697716077, 'mean_word_length': 7.005376344086022, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '584739', 'license': 'MIT', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1570, 'n_words': 368, 'path': 'examples.ipynb', 'repo_name': 'wendli01/outlier_plotting', 'size': '4466'}