Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Seaborn Introduction

Seaborn Bar Plot

Seaborn Distplot

Seaborn Lineplot

Seaborn Scatterplot

Seaborn Boxplot

Seaborn Catplot

Seaborn Regplot

Seaborn Histogram

Seaborn Kdeplot

Seaborn Countplot

Seaborn Pairplot

Seaborn Heatmap

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Seaborn bar plot

The dataset


total_bill sex time tip size day smoker
0 500.0 male lunch 30 2 sat no
1 648.0 male dinner 35 3 sat yes
2 75.0 female lunch 10 1 sat no
3 159.0 female dinner 12 4 sat yes
4 250.0 male lunch 3 3 mon no
5 222.0 female lunch 25 3 mon yes
6 356.0 male dinner 30 4 mon no
7 99.0 female lunch 5 1 mon no
8 150.0 male dinner 4 4 mon no
9 225.0 female lunch 38 4 mon no
10 478.0 female lunch 28 3 tue yes
11 320.0 male dinner 14 3 tue yes
12 300.0 male lunch 32 2 tue yes
13 520.0 male lunch 33 3 wed yes
14 367.0 male dinner 25 3 tue yes
15 277.0 female lunch 40 4 wed yes
16 100.0 female dinner 6 1 wed no
17 99.0 male dinner 40 1 wed yes
18 199.0 female lunch 35 3 wed no
19 288.0 female dinner 21 2 wed yes
20 120.0 male dinner 18 1 thu yes
21 168.0 male lunch 15 4 thu yes
22 120.0 female dinner 10 3 thu yes
23 284.0 male lunch 8 2 thu yes

How to create a bar plot?

To create a bar plot write module_name.barplot(). Now in the bracket pass the parameters.

Mandatory parameters:

The first parameter is: for x axis value: x="column_name"

The second parameter is: for y axis value: y="column_name"

The third parameter is: for variable name where we stored dataset: data=variable_name.

The fourth parameter is: hue(hue=" column name"). Hue means, for which your x-axis and y-axis values are. Suppose for sex column, you want to see the total bill in days. So use the total bill column on the y-axis and day in the x-axis and sex column in hue.

Extra parameters:

errwidth: value integer. In the bar plot, you will see a straight line on the top of each bar. To edit that line width use this parameter.

errcolor= "color name" . In the bar plot, you will see a straight line on the top of each bar. To edit that line color use this parameter.

ci= int value. In the bar plot, you will see a straight line on the top of each bar. To edit that line size use this parameter.

saturation=value between 0.0-1.0.

palette value name. There is some fixed name for this parameter. You can use those names for different pre-build colors. Example:Accent_r, hot, inferno_r, etc.

linestyle:" --,-.,-,: etc". Used to give the borderline style of bars.

linewidth:"int value". Used to give the borderline width of bars.

alpha:0.0-1.0.

How to give title,labels,legend,grid?
Title
For title write module_name.title().
In the bracket write the title as a string. For font size write fontsize:value. Between title name and fontsize parameter give a coma.

xlabel
For label at x-axis write module_name.ylabel().
In the bracket write label name as string. For font size write fontsize:value. Between label name and fontsize parameter give a coma.

ylabel
For label at y-axis write module_name. ylabel().
In the bracket write label name as string. For fontsize write fontsize:value. Between label name and fontsize parameter give a coma.

grid
For grid first write module_name.grid().
Some parameters of the grid are
1.color: used to set the color of the grid.Example: color="green".
2.linestyle: used to set grid line style.Example: linestyle="--".
3.linewidth: used to set line width of the grid.example: linewidth=2.

set()

This is used to set grid background. To use this write module_name.set().

figure()

This is used to set the size of the plot. To use this write module_name.figure(). In the bracket write figsize=(width,height)

Example 1:

Input
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df=pd.read_csv("E\\CSV4.csv")
sns.barplot(x="day",y="total_bill",data=df,hue="sex",alpha=0.6,linewidth="4",linestyle=":")
plt.title("Bar plot",fontsize=15)
plt.xlabel("Days",fontsize=12)
plt.ylabel("Total bill",fontsize=15)
plt.show()

Example 2:

Input
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df=pd.read_csv("E\\CSV4.csv")
sns.barplot(x="day",y="total_bill",data=df,hue="sex",errwidth=7,errcolor="g",saturation=0.5,palette="hot",color="r",ci=10)
plt.show()

Example 3:

Input
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df=pd.read_csv("E\\CSV4.csv")
sns.barplot(x="day",y="total_bill",data=df,hue="sex")
plt.show()

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.