Seaborn Data Visualization with Python

According to an article by  Yufeng G -Google Cloud, a machine learning project consists of 7 steps: Data Collection, Data Preparation, Choosing Model, Training Model and Evaluating Model.

In this series of 7 steps, today we will discuss Data Visualization which is one of the key practices under Step 2 of Data Preparation. It helps you to analyse your data better as it gets bigger and complex.

Matplotlib has been the go-to library for data visualizations, however, it becomes quite frustrating to write several lines of code to get attractive graphs. This is where Seaborn comes in.

 

Introduction to Seaborn

Seaborn is a popular data visualization library in Python to create well-designed data visualizations of the data. This library is created on top of the Matplotlib library. And with fewer lines of code, it generates charts that have an aesthetic visual impact.

“If matplotlib “tries to make easy things easy and hard things possible”, seaborn tries to make a well-defined set of hard things easy too”

Important Features of Seaborn:

  • When working with Pandas, it helps to visualize dataframes better than matplotlib
  • Built-in themes, choices of colour palettes
  • Fitting and Visualizing Linear Regression Models on dependent variables

 Installing Seaborn

Seaborn has the following dependencies so make sure you have these installed beforehand:

For installing with pip command:

pip install seaborn

I will be using Google’s Colab that provides Jupyter Notebook environment on the cloud. It is a free service and also has GPU functionality for faster processing of deep learning models.

Importing libraries and dataset

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
%matplotlib inline is used to display charts inside the notebook.
We will be using different example datasets provided by Seaborn for visualization. Use sns. get_dataset_names() to get list of datasets.
datasets = sns.get_dataset_names()

 Let’s Visualize Using Seaborn!

Based on the goals of the visualization, we will classify it into 3 categories:

  • Trends: Visualizing patterns of change of a variable over time ( LinePlots )
  • Relationships: Visualizing relationships between variables ( Bar Charts, ScatterPlot, Heatmap, Swarmplot , Regplot )
  • Distribution: Visualizing distribution of values of a variable ( Histograms, KDE plots, Joint Plots, Boxplot )

Visualizing Trends

Line Charts are best to show patterns over time. To plot this we will be using Flights example dataset which has the following data:

Seaborn Flights Dataset

#Loading Dataset
df=sns.load_dataset( "flights")
#Converting datatype to float 
df.year= df.year.astype(float)
#Setting the size of  chart
plt.figure(figsize= (16,5))
#(Showing trends of   passengers over months)
sns.lineplot(x="month", y="passengers", data=df)
plt.show()

We can see that passengers count starts to increase during the months May-August. As they are the months that fall under the vacation period of various schools and colleges.

Lineplot Seaborn

Visualizing Relationships

1) Bar Charts: Best for comparing values belonging to different items

Using the same data we can compare value: passengers among different variables: months

plt.figure(figsize= (14,5))
sns.barplot(x="month",  y="passengers",data=df)
plt.show()

Seaborn Barplot

2) Heatmap: Helps you find patterns in data with the help of colour-coded schemes

Pivot is used to convert dataframe as index=’month’, column=’year’ and values=’passengers’. The Heatmap created below maps passenger count to each month of each year.

df=df.pivot('month',  'year', 'passengers') 
plt.figure(figsize= (8,5) ) 
#annot displays values,    fmt=d is int format 
sns.heatmap(df,  annot=True, fmt='d')

Seaborn Heatmap

You can also change the colormap as follows:

sns.heatmap(df,  annot=True, fmt='d', cmap='YlGnBu')

Seaborn Heatmap2

3) ScatterPlot: Shows Relationship between two continuous variables, if the plot is colour-coded then it will show a relation with the third variable as well.

sns.scatterplot( x="passengers", y="month", data=df)

Seaborn Scatterplot

Now we will add the third variable in the scatterplot as a color-code using hue parameter.

#Adding third variable  as Hue
sns.scatterplot( x="passengers", y="month", hue="year", data=df)

Seaborn Scatterplot1

We can also add custom color palettes of our choice:

#Adding Color Palette
sns.scatterplot( x="passengers", y="month", hue="year", palette='inferno_r', data=df)

Seaborn Scatterplot

4) Regplot and Lmplot:  Regplot is used to add a regression line over the scatterplot to check any linear relationship among variables. Lmplot is used to add multiple regression lines if scatterplot has multiple groups.

Here we will be using Tips dataset which contains information about a restaurant that includes features as shown below. The goal here is analysing how the amount of tip given by customers changes based on other features.

tips= sns.load_dataset( 'tips')
tips.head()

seaborn tips data

Implementing regplot on tip and total_bill

sns.regplot( x='total_bill', y='tip', data=tips)

The plot shows that the tip is positively correlated with the amount of bill.seaborn regplot

Implementing lmplot by adding third category smoker as  hue

sns.lmplot( x="total_bill", y="tip", hue="smoker", data=tips)

We see that customers who don’t smoke tend to pay more tip than those who smoke

seaborn lmplot

5)Swarmplot: It is used to map the relationship between categorical variable and continuous variable.

sns.swarmplot( x="day", y="tip", data=tips)

Seaborn swarmplot

Now we will add third variable time which will show us which time period  of the day receives more tips.

sns.swarmplot( x="day", y="tip", hue='time', data=tips)

Seaborn Swarmplot

This shows that the restaurant receives most of its tips during Lunch at weekdays and during Dinner at weekends. We can replace time with smoker category to analyse who pays more tip each day.

#dodge is used to plot  each category seperate
sns.swarmplot( x="day", y="tip", hue='smoker', dodge=True, data=tips)

Seaborn Swarmplot

Next, we will extend this analysis to see a variation of swarm plot which is violin plot, which visualizes data in same manner but looks better as a visualisation. We will plot both of them for comparison.

sns.violinplot( x="day", y="tip", data=tips, inner=None)
sns.swarmplot( x="day", y="tip", data=tips, color='white')

Seaborn violinplot

Visualizing Distributions

1)Histograms: Helps you to analyse the distribution of single numerical variables

sns.distplot( tips['tip'], color='red')

Here we see that most of the tips have high probability between $2 to $4.

Seaborn Distplot

 

2)Jointplot: It is used to analyse distributions of bivariate numeric variables.

sns.jointplot( x=tips.total_bill, y=tips.tip, kind='kde')

Here we see that tips of $2 to $4 on bills of $10 to $15 have high probability.

Seaborn Jointplot

3)KDEplot: Its a smooth visualization version of histograms for a single numeric variable and two variables.

Here we will take new dataset called iris-dataset, which provides information on features about various species of iris plant.

iris= sns.load_dataset( "iris")
iris.head()

iris dataset

#Obtaining Plants from  dataset
setosa = iris.loc[ iris.species == "setosa" ]
virginica = iris.loc[ iris.species == "virginica" ]

#Plotting KDE 
sns.kdeplot( setosa.sepal_length, setosa.sepal_width, label='setosa')

sns.kdeplot( virginica.sepal_length, virginica.sepal_width, label='virginica')

#labels are used to  plot legend
plt.legend()

Here we see the distribution of two iris plants based on their sepal_width and sepal_length.

Seaborn KDE plot

4)BoxplotIt is used to obtain summary of variable and compare them, we can obtain values of three quartiles, minimum and maximum value.

#FacetGrid is used to  plot multiple charts
g=sns.FacetGrid( iris, col="species")
g.map(sns.boxplot, "sepal_length")

Here we can see the distribution of sepal-length of three iris species.

Seaborn Boxplot

Conclusion

Today we have learnt various plots for visualizing data based on trends, relationships and distributions using seaborn. We also learnt that how changing the color palette of a plot brings out its aesthetic value. If you want to explore seaborn more extensively I would recommend you to look at their documentation

Here is the link to my Colab Notebook where you can find the whole implementation. If you liked this tutorial and would want to read more of such content do comment and share this post. Happy Learning!

Android 11 Beta: Checkout the Top New Features

On 10th June, Google released Android 11-Public Beta version for the developers. Don’t worry!  Normal users can also use and experiment these features. To use them you must have any of the Pixel 2, 3, 3a, or 4 smartphones. According to Google, this version focuses on 3 key factors: People, Privacy and Controls. 

Major New/Updated Features in Android 11

  • Notifications: A lot of changes have been done here. Notifications will now be segregated into Conversations, Alerting and Silent Notifications. Conversations section will contain all your chats/messages across various social media apps. Any chat across apps can now stay on screen as Chat Bubbles which you might have used in Facebook Messenger. You can also prioritize a chat, which will appear even when your phone is in DND mode. The latter two sections will contain notifications from System apps or 3rd party apps. Another new feature is the Notification History if you have missed any notifications this feature will help you access it. You can also mute-notifications now while recording a video on mobile.Below is the screen layout of notifications.Android-11 notifications
  • Device and Media Controls: Now by just long-pressing the power button all the important device controls will appear on the screen. The new thing here is that Google has added controls for smart-home devices present in Kitchen, Bedroom etc. You can turn lights on /off, change the AC temperature to name a few. The media controls have now been shifted up in Quick Settings as compared to inside Notification section. You can switch between different media like music or podcast by just swiping the media player controls. On expanding you will see a button from where you can choose whether to play the media on headphones, speaker or just on the phone.Media and Device Control
  • HomeScreen:  Here you can now replace your dock with apps that you frequently use. When you Multi-task and open the recent screens you can use 3 features here: Screenshot, Select and Share, screenshot captures the screen, the select feature will find all the text on the screen that can be copied on a clipboard and share feature will screenshot and let you share the image on any app that you want.HomeScreen
  • Permissions: Previously when Android apps asked you for any permission there were only 3 options: Accept, Accept while using, Deny. Now one more option is added that is Only Once. Also now if an app is constantly asking you for permission and you deny it multiple times then Android will tell that app to stop asking that particular permission in the future. Also it will automatically revoke permissions like location, camera, storage from apps that haven’t been used for a long time.
  • Voice Access capabilities: Earlier you had to specify a number for a action that you wanted to perform in an app. Now you can perform all activities just by Voice. Check out an example of this feature by Verge reporter Dieter Bohn.

Minor New/Updated Features in Android 11

  • In-built Screen Recording:  Till now we were using 3rd party apps like A-Z Screen Recorder but now Android 11 will have its own native screen recording feature. Android 10 Beta had this feature however it was later removed. Let’s see if it sticks around this time.
  • Dark Mode Theme:  Android 11 now allows you to schedule dark mode on a specific time or android automatically shifts between dark and light mode based on the timezone your device is present in.
  • Motion Gesture: You can now play/pause music with air gestures
  • AirPlane Mode doesn’t kill Bluetooth: Earlier turning on Airplane mode would automatically turn off Bluetooth which is not the case now. Bluetooth stays active even during Airplane Mode.
  • Fast File-Sharing feature:  Similar to iPhone’s Airdrop, Google’s FastShare will allow users to share files between devices. However its limited to Pixel Phones currently.

Conclusion

There has been no announcement regarding release of Android 11. But as per its past releases, I expect it to be around August-September. If you want to download Android 11 you can sign up for the Beta-program and enroll your device, follow the procedure and then restart your phone.

How To Create A Catalog Of Items To Sell On WhatsApp Business

 

On November 7, WhatsApp came up with a new feature for all the Business account users. Now small businessmen can create a catalogue of items that they wish to sell, instead of forwarding pictures of items to each and every friend on WhatsApp.

The buyer can now directly visit the businessmen’s catalogue and discover items that they want to but from that list.

 

So in order to create a Catalog follow the below steps:

Step 1) Make Sure you are using a Business Account, if yes then go to Settings.

Step 2) Now Tap on ‘Business Settings

Step 3)Select Catalog, now if you are unable to find it update your app to latest version.

Step 4)Click on ‘Add Product or Service‘. Now you can add pictures of the items and provide necessary information about it like name, description , price of the product and so on.

Step 5) Now go to Attachment section in the chat and Select Catalog Option to share it with your friends and buyers.

Here is an example of a Catalog of Cakes  uploaded by a woman Sandra:

Currently, this feature is available for Whatsapp Business Users on both Android and iOS residing in the following countries: India, Brazil, Germany, Indonesia, Mexico, the U.K. and the U.S.