Data Visualization Basic Libraries
Tags: python, seaborn, matplotlib, datascience
Published: 2020-03-23 06:37:17

One of the advantages of using Python is user-friendly visualization packages. In this blog, I try to explain some essential plots for data visualization and I will share with you which plots I used for what before and what I found useful. Mainly, the plots which I will share in this article belongs to Matplotlib which is a library for Python using for visualization. Also, I would like to state about Seaborn, visualization library based on matplotlib, because I like Seaborn's colorful visuals. It is all about my usage preferences from my experiences.

![Alt Text] (https://media.giphy.com/media/Pn4vFaA3h38KA/giphy.gif)

### Scatter Plot  *(plt.scatter)*

First of all, we need to import *matplotlib.pyplot* like all other libraries, to use these plot types.

Scatter plots are very useful to understand the correlation between two variables easily. Also, I like to put markers for data points like 'stars','triangles' or 'sized-circles' according to the frequency of data. The most important feature which I like to use is scatter plot with a legend. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6eb0jd4kmto6pokrsv68.png)

Another feature that I like about scatter plot is adding a color bar with *plt.colorbar()*. It is very useful to see the colors and recognize different points. In the below graph, I used the 'viridis' as perceptually uniform sequential colormap scale but there are lots of alternative sequential, diverging, qualitative, miscellaneous color bars in 'matplotlib'.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wp2hi7d37hdzhglcfvkl.png)

All of them can be found in [matplotlib.org] (https://matplotlib.org/3.1.1/tutorials/colors/colormaps.html).

### Bar Plot *(plt.bar)*

I prefer bar plots to compare categorical data and differences between them. It is super easy to see the lengths of rectangular bars which proportional to data. Bars can be sorted according to y-axis values and the variables from best to worst easily can be seen. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d7spam4dlcz5vcbntjtl.png)

I like to color the bars also and compare two variables in same the graph with the bar plot. With this method, we can compare lots of variables with each other proportionally. Additionally, it is also good to arrange the width of the bars. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k22zpt77fyc83xfiuou4.png)

### Pie plot *(plt.pie)*

When I would like to see the percentages of a small number of variables in the same graph, I like to use pie plots. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sorxiyslcsixr836depa.png)

Also, nested pie-plot charts can be created for more complex data but I do not prefer complexity for interpreting data. Another good visual thing, which I like about the pie plot is we can separate one slice from the pie plot and show the importance of this part. 

### Histogram and Density Plot *(plt.hist)* 

Undoubtedly, one of the best ways to see the distribution of the data is histograms. It is super easy to see the normally distributed variables which are a very important concept for data scientists. So, histograms are very essential graphs for data scientists. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/axdo2pf0bw3cx1hd8ba5.png)

Although it is a great way to begin understanding a single variable distribution, histograms can fail when we try to compare distributions of one variable across multiple categories. Because, when histograms get together in the same area, readability will be the problem. The best way to do this is to create a new histogram for each category. Also, a side-by-side histograms or stacked bars can be useful. 

There is a good example in [towardsdatascience.com] (https://towardsdatascience.com/histograms-and-density-plots-in-python-f6bda88f5ac0) with NYC flight data about when histograms fail and how to solve this by using side-by-side histogram or stacked bars. 

When density plots are get together with the histograms of the distribution of data, the graph will be more useful also. I want to state here about Seaborn which is super easy to use and Seaborn can create a histogram and density curve on the same plot easily.

### Seaborn *(sns.pairplot , sns.heatmap , sns.distplot, sns.barplot)*

Seaborn is a really useful and colorful library in Python. Like 'matplotlib' , we need to import Seaborn also before using it. 

The most useful basic plots in Seaborn are pair plot, heat map, distribution plot. I also added a bar plot here because I like to use Seaborn's easy colorful bar plot. You do not need to give specific colors so, it is easier than 'matplotlib'. The other thing which I like about Seaborn is using fewer syntax.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/re1v0ajl0t3drnefu0bv.png)

There are many ready datasets in Seaborn. This is the other important thing which I like. As a data scientist candidate, it is very good to find ready data sets to play around visualizing them. In the graph below, I plotted the distribution and also density curves for ready data set 'iris' which can be downloaded as *sns.load_dataset('iris')* easily. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/y2jxkh95jcp1s0yvxeqt.png)

Another important thing for modeling is the correlation between independent variables. To analyze this correlation, Seaborn's sns.pairplot and sns.heatmap are useful functions. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1lgicpjgiw2dovj6zu42.png)

From the data set 'auto-mpg.csv', I choose some columns for simplification and just put my data set in pairplot as sns.pairplot(df). It is a super fast, easy and useful way of Seaborn. Then with the graph above, I can observe the correlations, linearity and also distribution of variables. So, sns.pairplot is a very important feature of Python.

It is also the same for heatmap.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3ybzelq0f1p4y9a7qurl.png)

Just a simple one-line code I can observe all correlations with target and multicollinearity between variables super easily. 

Lastly, there are many ways to visualize data in Python and also in other programming languages. But, these topics are really basic, useful and also easy to use for beginners. In addition to this, these are the fastest ways to observe datasets. As seen in this blog post, visualization library or tool always depends on the data and what you want to obtain.

*Cover image sourced by <a href="https://pixabay.com/users/ColiN00B-346653/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3068300">Colin Behrens</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3068300">Pixabay</a> and gif is from [giphy](giphy.com).*