Plotting Using Seaborn | python

Installation: The easiest way to install Seaborn — this is to use pip. Enter the following command in terminal:

 pip install seaborn 

OR you can download it from here and set manually.

Plot plotting with Seaborn

Stripplot

# Python program for illustration
# Building categorical scatter
# Seaborn sites

 
# import of the required module

import matplotlib.pyplot as plt

import seaborn as sns

 
# os values and x

x = [ `sun` , ` mon` , ` fri` , `sat` , `tue` , `wed` , ` thu` ]

 
# Y-axis values ​​

y = [ 5 , 6.7 , 4 , 6 , 2 , 4.9 , 1.8 ]

 
# storyline with a plot sea ​​

ax = sns.stripplot ( x, y); 

 
# assign labels to the X and Y axes

ax. set (xlabel = ` Days` , ylabel = `Amount_spend` )

  
# give a title to the story

plt.title ( ` My first graph` ); 

 
# plot display function
plt.show ()

Output:

Explanation: This one kind of categorized data using seaborn.

  • Categorical data is represented on the X-axis, and the values ​​correspond to them, represented on the Y-axis.
  • Function. striplot () is used to define the type of plot and applied to the canvas using.
  • The .set () function is used to set labels X and Y axes.
  • The .title () function is used to assign a title to the graph.
  • To view the graph, we use the .show function () .

Stripplot using built-in kit yes data specified in seaborn:

# Python program for illustration
# Stripplot using built-in dataset
# given in sea birth

  
# import of the required module

import matplotlib .pyplot as plt

import seaborn as sns

 
# use to set the background style of the plot

sns. set (style = "whitegrid" )

  
# load dataset

iris = sns.load_dataset ( `iris` ); 

 
# plot line with a piece of the sea
# defining the attributes of the dataset on which the graph should be built

ax = sns.stripplot (x = ` species` , y = `sepal_length` , data = iris); 

 
# give the title to the plot

plt.title ( `Graph` )

 
# plot show function
plt.show ()

Output:

Explanation :

  • iris — this is a dataset already present in the seaborn module for use.
  • We use the .load_dataset () function to load the data. We can also upload any other file by specifying the path and filename in the argument.
  • The .set (style = ”whitegrid”) function is also used here to define the background of the plot. We can use "darkgrid"
    instead of whitegrid if we want a dark background.
  • In the .stripplot () function, we define which the dataset attribute should be on the x-axis and which dataset attribute should be on the y-axis.  data = iris means the attributes we defined earlier should be taken from the data.
  • We can also draw this plot using matplotlib, but the problem with matplotlib — these are the default options. The reason Seaborn is so good at DataFrames is, for example, because labels from DataFrames are automatically propagated to charts or other data structures, as you can see in the above figure. The View column name occurs along the x-axis, and the column name stepal_length occurs in y-aixs. , this is not possible with matplotlib. We must explicitly define the x-axis and y-axis labels.

Swarmplot:

# Python program to illustrate
# plotting with Swarmplot

 
# import the required module

import matplotlib. pyplot as plt

import seaborn as sns

 
# use to set the background style of the plot

sns. set (style = "whitegrid" )

  
# load dataset

iris = sns.load_dataset ( `iris` ); 

 
# plot strip with a piece of the sea
# defining the attributes of the dataset on which to plot the graph

ax = sns.swarmplot (x = ` species` , y = `sepal_length` , data = iris); 

 
# give the title to the plot

plt.title ( `Graph` )

 
# plot show function
plt.show ()

Output:

Explanation:
This is very similar to a stripplot, but the only difference is that markers must not be overlapped. This causes the plot markers to jitter, so the plot can be easily read without loss of information as seen in the plot above.

  • We use the .swarmplot () function to plot the Swarn plot.
  • Another difference that we can notice in Seaborn and Matplotlib is that working with DataFrames is not so smooth with Matplotlib, which can be annoying if we are doing exploratory analysis with Pandas. And that`s exactly what Seaborn makes easy, the plotting functions work with DataFrames and arrays that contain a whole set of data.

NOTE. If we wish, we can also change the presentation of data on a specific axis. For example:

# import the required module

import matplotlib.pyplot as plt

import seaborn as sns

 
# use to set the background style of the plot

sns. set (style = "whitegrid" )

 
# load dataset

iris = sns.load_dataset ( `iris` ); 

 
# plot line with a piece of the sea
# defining the attributes of the dataset on which to plot the graph

ax = sns.swarmplot (x = ` sepal_length` , y = `species` , data = iris); 

 

 
# give the title to the plot

plt.title ( `Graph` )

 
# display function plot
plt.show ()

Output:

The same can be done in stripplot. Finally, we can say that Seaborn — it is an extended version of matplotlib that tries to simplify a clear set of tricky things.