Create basic graph visualizations with SeaBorn
By: Rahul Agarwal
Tags: Education, Data Visualization, Towards Data Science, Data Science, Python

When it comes to data preparation and getting acquainted with data, the one step we normally skip is the data visualization.

While a part of it could be attributed to the lack of good visualization tools for the platforms we use, most of us also get lazy at times.

For most of our plotting needs, I would read up blogs, hack up with StackOverflow solutions and haggle with Matplotlib documentation each and every time I needed to make a simple graph.

This led me to think that a Blog post to create common Graph types in Python is in order.

But being the procrastinator that I am it always got pushed to the back of my head.

One thing that helped me in pursuit of my data visualization needs in Python was this awesome course about Data Visualization and applied plotting from the University of Michigan which is a part of a pretty good Data Science Specialization with Python in itself. Highly Recommended.

So I am finally writing this blog post with a basic purpose of creating a code base that provides me with ready to use codes which could be put into analysis in a fairly straight-forward manner.

Right. So here Goes.

Start by importing the libraries that we will need to use.

import matplotlib.pyplot as plt #sets up plotting under plt

import seaborn as sns #sets up styles and gives us more plotting options

import pandas as pd #lets us handle data as dataframes

We will be working with the Tips data that contains the following information.

tips = sns.load_dataset("tips")

tips.head()

Scatterplot With Regression Line

Now let us work on visualizing this data. We will use the regplot option in seaborn.

Now that required a bit of code but I feel that it looks much better than what either Matplotlib or ggPlot2 could have rendered. We got a lot of customization without too much code.

But that is not really what actually made me like Seaborn.

The plot type that actually got my attention was lmplot , which lets us use regplot in a faceted mode.

A side Note on Palettes:

You can build your own color palettes using color_palette() function. color_palette() will accept the name of any seaborn palette or matplotlib colormap(except jet, which you should never use). It can also take a list of colors specified in any valid matplotlib format (RGB tuples, hex color codes, or HTML color names). The return value is always a list of RGB tuples. This allows you to use your own color palettes in a graph.

Barplots

Histograms and Distribution Diagrams

They form another part of my workflow. Let us plot the normal Histogram using seaborn. For this, we will use the function. This function combines the matplotlib hist function (with automatic calculation of a good default bin size) with the seaborn kdeplot() function. It can also fit scipy.stats distributions and plot the estimated PDF over the data.

PairPlots

You need to see how variables vary with one another. What is the distribution of variables in the dataset? This is the graph to use with the function. Very helpful And Seaborn males it a joy to use. We will use Iris Dataset here for this example.

iris = sns.load_dataset("iris")

iris.head()

Hope you found this post useful and worth your time. You can find the iPython notebook at github

I tried to make this as simple as possible but You may always ask me or see the documentation for doubts.

If you have any more ideas on how to use Seaborn or which graphs should I add here, please suggest in the comments section.

I will definitely try to add to this post as I start using more visualizations and encounter other libraries as good as seaborn.

Also since this is my first visualization post, I would like to call out a good course about Data Visualization and applied plotting from the University of Michigan which is a part of a pretty good Data Science Specialization with Python in itself. Do check it out.