<jupyter_start><jupyter_text>Seaborn Import required libraries.<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Read data to visualize Read the CSV file as a pandas dataframe.<jupyter_code>data = pd.read_csv('Data/data.txt', delimiter=',')
data.info()<jupyter_output><empty_output><jupyter_text>Continuous data Create a simple matplotlib scatter plot for $x$ and $y$.<jupyter_code>plt.scatter(x='x', y='y', data=data);<jupyter_output><empty_output><jupyter_text>Now use Seaborn to plot the same data, but also include the result of a linear regression.<jupyter_code>sns.regplot(x='x', y='y', data=data);<jupyter_output><empty_output><jupyter_text>If we have data where the $x$-values are integer, and we have multiple $y$-values for each $x$-value, adding some "jitter" to the $x$-values makes the plot more informative.<jupyter_code>sns.regplot(x='x1', y='y1', data=data, x_jitter=0.1);<jupyter_output><empty_output><jupyter_text>In the plot below, the mean value for $x$ is shown, as well as a confidence interval.<jupyter_code>sns.regplot(x='x1', y='y1', data=data, x_estimator=np.mean);<jupyter_output><empty_output><jupyter_text>A joint plot shows the distribution of the $x$ and $y$ data, as well as their relationship.<jupyter_code>sns.jointplot(x='x2', y='y2', data=data, kind='reg');<jupyter_output><empty_output><jupyter_text>Since we have a data set with a categorical attribute, the data set can be split per "label" category, showing a regression plot for each.<jupyter_code>sns.lmplot(x='x2', y='y2', data=data,
           col='label', col_wrap=2, size=3);<jupyter_output><empty_output><jupyter_text>For categorical data, it is also possible to plot the two "binary" categories in the same plot.<jupyter_code>sns.lmplot(x='x3', y='y3', data=data, hue='binary');<jupyter_output><empty_output><jupyter_text>In the plots below, we have a plot per category "label", but a hue per "binary" category as well.<jupyter_code>sns.lmplot(x='x3', y='y3', data=data, hue='binary',
           col='label', col_wrap=2, size=3);<jupyter_output><empty_output><jupyter_text>The plot below shows the mean values and confidence intervals for the $x$-values split per "binary" category.<jupyter_code>sns.lmplot(x='x1', y='y1', data=data, x_estimator=np.mean,
           col='binary', size=3);<jupyter_output><empty_output><jupyter_text>Categorical data Plotting as a function of the "label" categories.  Again, we add some jitter to gain some insight into the $y$-value distribution.<jupyter_code>sns.stripplot(x='label', y='y2', data=data, jitter=True);<jupyter_output><empty_output><jupyter_text>The data can be colored by its "binary" category.<jupyter_code>sns.stripplot(x='label', y='y2', data=data, hue='binary', jitter=True);<jupyter_output><empty_output><jupyter_text>A box plot of this data shows the quantiles for this data.<jupyter_code>sns.boxplot(x='label', y='y2', data=data);
sns.boxplot(x='label', y='y2', data=data, hue='binary');<jupyter_output><empty_output><jupyter_text>A violin plot gives more intuitive information on the distribution of the $y$-values.<jupyter_code>sns.violinplot(x='label', y='y2', data=data);<jupyter_output><empty_output><jupyter_text>As before, the data can be colored by "binary" category.<jupyter_code>sns.violinplot(x='label', y='y2', data=data, hue='binary', split=True);<jupyter_output><empty_output><jupyter_text>A count plot will simply give the counts of data points in each "label" category.<jupyter_code>sns.countplot(x='label', data=data);<jupyter_output><empty_output><jupyter_text>The count can be colored by the "binary" category as well.<jupyter_code>sns.countplot(x='label', data=data, hue='binary');<jupyter_output><empty_output><jupyter_text>Distributions Plotting the distribution of data in a pandas dataframe is straightforward. The number of bins can be set conveniently, and the rug options indicates the value of individual points in the distribution. A kernel density estimation is automatically computed and plotted as well.<jupyter_code>sns.distplot(data.x3, bins=20, rug=True);<jupyter_output><empty_output><jupyter_text>The `kde` options controls whether the kernel density estimation is plotted or not, the `hist` option is used to show (default) or hide the histogram, e.g.,<jupyter_code>sns.distplot(data.x3, hist=False, rug=True);<jupyter_output><empty_output><jupyter_text>Plotting bivariate distritions is easy as well.<jupyter_code>sns.jointplot(x='x2', y='y2', data=data, kind='kde');<jupyter_output><empty_output><jupyter_text>A facet grid can be used to display distributions or other types of plots as a function of parameters, e.g., the "binary" category.<jupyter_code>graph = sns.FacetGrid(data, col='binary')
graph.map(plt.hist, 'x2');<jupyter_output><empty_output><jupyter_text>Both matplotlib and Seaborn plots can be mapped over the facet grid.<jupyter_code>graph = sns.FacetGrid(data, col='label', col_wrap=2, size=3)
graph.map(sns.kdeplot, 'x2');
graph = sns.FacetGrid(data, col='label', hue='binary', col_wrap=2)
graph.map(plt.scatter, 'x2', 'y2', alpha=0.8)
graph.add_legend();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13288590604026845, 'fraction_numerical': 0.008053691275167786, 'mean_word_length': 7.931506849315069, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 93, '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': '23', 'dir': 'jupyter-structured-clean-dedup', 'id': '526294', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1814, 'n_words': 469, 'path': 'Python/Seaborn/seaborn.ipynb', 'repo_name': 'Gjacquenot/training-material', 'size': '5215'}