<jupyter_start><jupyter_text>Understanding SeabornIf you're familiar with the `R` language, you know that `ggplot` is a great resource to plot data and interpret it. Similarly, in `Python`, the equivalent library is `matplotlib`. However, the functionality and appearance of the plots using matplotlib are not high and as a result a better alternative is needed. This is where `seaborn` steps in and steals the show.Let's take a deep dive on the various plots that we can make using `seaborn` and see how easy it is to do so.>>><NAME>/// <EMAIL>  Import dataI'll work with the dataset about **California House Prices** dataset also available on [Kaggle](https://www.kaggle.com/camnugent/california-housing-prices).<jupyter_code>import pandas as pd

dataset = pd.read_csv("dataset.csv")
print("Dataset: {}".format(dataset.shape))
print("Columns: {}".format(dataset.columns))
dataset.head(5)<jupyter_output>Dataset: (20640, 10)
Columns: Index(['longitude', 'latitude', 'housing_median_age', 'total_rooms',
       'total_bedrooms', 'population', 'households', 'median_income',
       'median_house_value', 'ocean_proximity'],
      dtype='object')<jupyter_text>There are **20640 rows** with **10 columns**. We can leverage the datasets and the valuable information it hides to plot data and infer from it.  Seaborn libraryLet's import the `seaborn` library to get started.<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Scatter plotA scatter plot helps describe a good relation between two quanities. Here, let's see the relation between the number of rooms and the number of bedrooms.<jupyter_code>sns.scatterplot(x = 'total_rooms', y = 'total_bedrooms', data = dataset)<jupyter_output><empty_output><jupyter_text>We can see an almost linear relationship between `rooms` and `bedrooms`. However, the plot is not very intuitive. Let's make this more informative and descriptive.<jupyter_code>plt.figure(figsize = (12, 8))
sns.scatterplot(data = dataset,
               x = 'total_rooms', 
               y = 'total_bedrooms', 
               hue = 'ocean_proximity', 
               style = 'ocean_proximity')
plt.title("California Rooms vs Bedrooms")
plt.xlabel("Total rooms")
plt.ylabel("Total bedrooms")<jupyter_output><empty_output><jupyter_text>As you can see, we improved the plot so much. We added the size of each point based on the `median_house_value` and added a legend for it too. Next, the `hue` provides different colors based on the categorical column defining proximity to the ocean. The same column also helps us to define various markers using `style`.  Count PlotCount plots are a great tool to desribe categorical information. For example, in this dataset we could see how the dataset is ditributed based on `ocean_proximity`.<jupyter_code>sns.countplot(x = 'ocean_proximity', data = dataset)<jupyter_output><empty_output><jupyter_text>We can see how it already calculates each column value and presents it as a bar. `<1H OCEAN` has the highest value while `ISLAND` is almost negligible. However, the plot does not tell what the actual values are. Let's plot them and see. Also, something to note about the dataset is that it's not very descriptive. For example, the value `<1H OCEAN` does not describe what H means here and neither does the metadata for this dataset.<jupyter_code>plt.figure(figsize = (12, 8))
ocean_plot = sns.countplot(x = 'ocean_proximity', data = dataset)
for p in ocean_plot.patches:
    ocean_plot.annotate(p.get_height(), 
                        (p.get_x() + p.get_width() / 2.0, 
                         p.get_height()), 
                        ha = 'center', 
                        va = 'center', 
                        xytext = (0, 5),
                        textcoords = 'offset points')

plt.title("Count of houses based on their proximity to ocean")
plt.xlabel("Proximity to the ocean")
plt.ylabel("Count of houses")<jupyter_output><empty_output><jupyter_text>Well, now the plot looks so much better than before and even makes each bar more descriptive.  HistogramsHistograms look similar to bar plots but they are used to express continuous values into separate bins. Let's describe the median house prices in California and see what that looks like. The good thing about `seaborn` is that it has the density plot built into the histogram and is called the `dist` plot.<jupyter_code>plt.figure(figsize = (12, 8))
sns.distplot(a = dataset['median_house_value'], bins = 10, hist = True)
plt.title("Density and histogram plot for Median house value")
plt.xlabel("Median house value")
plt.ylabel("Value")<jupyter_output><empty_output><jupyter_text>The data looks a little skewed towards the left side and looks quite **normal**. However, in the end, there is an unusual spike around 500,000. The combined plots show that houses on `ISLAND` are the most expensive ones in the whole range. While for all others, the range is quite varied.  Violin plotViolin plots are similar to box plots but take into account the densities.<jupyter_code>plt.figure(figsize = (12, 8))
sns.violinplot(x = 'ocean_proximity', y = 'median_house_value', data = dataset)
plt.title("Box plots of house values based on ocean proximity")
plt.xlabel("Ocean proximity")
plt.ylabel("Median house value")<jupyter_output><empty_output><jupyter_text>From the plot above, we can see how the densities are distributed for each ocean proximity value.  HeatmapSometimes you just need to take a look at the correlation between features and labels. In such a case, a heatmap that not onlu mentions the values but also the color based on the correlation value gives a great description. Let's try to plot the heatmap for our dataset.<jupyter_code>plt.figure(figsize = (12, 8))
sns.heatmap(dataset.corr(), annot = True)<jupyter_output><empty_output><jupyter_text>By observing the heatmap, we can see that `median_income` is highly correlated to the `median_house_value`.  Joint plotA joint plot is a scatter plot with next level skills. It shows us the distribution of the two values we're plotting along with the scatter plot. Furthermore, it has a built in regression, which shows a regression line on the data.<jupyter_code>sns.jointplot(x = "total_rooms", y = "total_bedrooms", data=dataset, kind="reg", height = 8, color = 'g')
plt.xlabel("Total rooms")
plt.ylabel("Total bedrooms")<jupyter_output><empty_output><jupyter_text>Box plots with swarm plotsBox plots provide a good way to view how the data is spread. Furthermore, when we combine the same plot with `swarm` plots which plot non overlapping points, more information can be portraied easily.<jupyter_code>plt.figure(figsize = (12, 8))
sns.boxplot(x = 'ocean_proximity', y = 'median_house_value', data = dataset)
sns.swarmplot(x = 'ocean_proximity', y = 'median_house_value', data = dataset)
plt.title("Box plots of house values based on ocean proximity")
plt.xlabel("Ocean proximity")
plt.ylabel("Median house value")<jupyter_output><empty_output><jupyter_text>Pair plotThe pairplot is a unique plot provided in seaborn. When we show a pairplot for the whole dataset, it plots the relation between each column as a scatter plot and a histogram for each column's relation with itself. It's really great to showcase a lot of information in a single collection of plots.<jupyter_code>sns.pairplot(dataset)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08671536796536797, 'fraction_numerical': 0.006493506493506494, 'mean_word_length': 4.784820031298905, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 53, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '658593', 'license': 'Apache-2.0', 'n_tokens_mistral': 2250, 'n_tokens_neox': 2085, 'n_words': 878, 'path': 'Understanding Seaborn.ipynb', 'repo_name': 'shamiulshifat/Uunderstanding_Seaborn-Data-Visualization-', 'size': '7420'}