# Visualization with Seaborn

You must have used matplotlib for your visualization tasks earlier. Like in this article - Using Autoencoder to generate digits with Keras, we used matplotlib to show our results. Well, there can't be just one visualization tool out there.

**Seaborn**

Seaborn is a visualization tool or library for python that is useful to create statistical graphs. It enables you to develop highly "**attractive**" visualizations with its advanced tools.

It is built on matplotlib and pandas that means the inbuilt functions can work on data frames and arrays. I mentioned that it produces attractive graphs. By attractive I mean the graphs are highly informative because seaborn functions perform the semantic maps and aggregation internally. Its API focuses on the importance of different features or elements in your data which will further help you in developing the best performing network architecture.

**Why not just use Matplotlib?**

Though Seaborn and matplotlib are closely related, seaborn has its own advantages -

- Seaborn is (also) used for complex graphs where matplotlib cannot plot them subtly. Seaborn uses easy or fewer syntaxes than matplotlib to do the same.
- Seaborn is more integrated with Pandas Dataframes, which means it has a straightforward plotting mechanism.
- Seaborn works with datasets as a whole and has an API that uses just one argument to specify the kind of plot.

Now let's just jump into the technical part -

- Installation
- Plotting user-defined functions
- Seaborn Histogram
- Seaborn Scatter Plots (Categorical data Representation)
- Seaborn Barplot
- Seaborn Displot
- Seaborn Boxplot

**Note**: The codes are written and tested by the author. The outputs are the screenshots of Jupyter Notebook cells.

**1. Installation**

**Note**: Please only continue if you have python and pip, or Conda installed on your system. If you are lacking either of them, please download the packages.

Open the terminal and type either of these commands

```
# Install using pip
pip install seaborn
# Install using anaconda
conda install -c anaconda seaborn
```

**2. Plotting user-defined functions**

Let's define a random **np.sin()** function and plot it using seaborn. You can plot any defined function like a line chart** **using this method.

**Note**: Matplotlib is still required to plot the graph as seaborn is built on it.

```
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
def plot_sin(flip = 1):
x = np.linspace(0, 20, 100)
for i in range(1, 7):
plt.plot(x, np.sin(x + i * 2) * (7 - 2 * i) * flip)
sns.set()
plot_sin()
plt.show()
```

**2. Seaborn Histogram**

Histograms are a type of representation of data where a bar is used to observe the feature with respect to some entity. Seaborn has its own dataset to quickly start with.

We will use the built-in 'iris' dataset here.

```
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
df = sns.load_dataset('iris')
sns.distplot(df['sepal_length'],kde = False)
# Set your style of grid. Here we will not use grid, just a dark background
sns.set_style("dark")
plt.show()
```

You can also get the curve for the above plot by using the argument **kde=True**

You can see how easy it is to plot the histogram. You don't need to rearrange or tune your data like in matplotlib. Seaborn is taking the dataset as a whole and plotting the required observations.

**3. Seaborn Scatter plot (Categorical Data Representation)**

Let's use another dataset for this section. The dataset is called the 'tips' dataset which contains some information about staff in a restaurant.

```
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
tips = sns.load_dataset("tips")
# Here we are using style to distinguish between the classes
sns.relplot(x="total_bill", y="tip", hue="time", style='time', data=tips)
```

**4. Seaborn Barplot **

Plotting a bar chart or a bar graph comes under categorical data visualization and we are going to store those values on categorical variables. We can just assign the **kind **argument to 'bar'.

```
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
df = sns.load_dataset('iris')
sns.catplot(x="species", y="sepal_length", hue='species', kind="bar", data=df)
plt.show()
```

There are also horizontal bar plots, which are rectangular bars as well and you just need to use **barplot** function of seaborn python package.

**5. Seaborn Displot**

Displots (or distribution plots) are nothing but univariate histograms which means they look quite similar to each other. There is only one observation that is being observed and represented.

```
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
iris= sns.load_dataset("iris")
# Here bins=20 represents number of bars in the graph
sns.displot(iris, x="sepal_length", bins=20)
```

**Note**: sns.displot is not available for versions below v0.11. Please upgrade your version if you get an AttributeError.

**6. Seaborn a Boxplot**

This figure shows the three quartiles of a distribution plot. It is also very easy to plot it, you just need to assign the argument **kind **to the "box"

```
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
iris = sns.load_dataset("iris")
sns.catplot(x="species", y="sepal_length", kind="box", data=iris)
```

That's it for this article. Hope you are now comfortable with the basics of seaborn and can visualize your data better with it.

Thank you and Cheers :)