Plotting graph using Seaborn | Python

This article will introduce you to graphing in python with Seaborn, which is the most popular statistical visualization library in Python.

Installation : Easiest way to install seaborn is to use pip. Type following command in terminal:

pip install seaborn

OR, you can download it from here and install it manually.



Plotting categorical scatter plots with Seaborn

Stripplot

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to illustrate
# Plotting categorical scatter 
# plots with Seaborn
  
# importing the required module
import matplotlib.pyplot as plt
import seaborn as sns
  
# x axis values
x =['sun', 'mon', 'fri', 'sat', 'tue', 'wed', 'thu']
  
# y axis values
y =[5, 6.7, 4, 6, 2, 4.9, 1.8]
  
# plotting strip plot with seaborn
ax = sns.stripplot(x, y);
  
# giving labels to x-axis and y-axis
ax.set(xlabel ='Days', ylabel ='Amount_spend')
  
# giving title to the plot
plt.title('My first graph');
  
# function to show plot
plt.show()

chevron_right


Output:

Explanation : This is the one of kind of scatter plot of categorical data with the help of seaborn.

  • Categorical data is represented in x-axis and values correspond to them represented through y-axis.
  • .striplot() function is used to define the type of the plot and to plot them on canvas using .
  • .set() function is use to set labels of x-axis and y-aixs.
  • .title() function is used to give title to the graph.
  • To view plot we use .show() function.

Stripplot using inbuilt data-set given in seaborn :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to illustrate
# Stripplot using inbuilt data-set 
# given in seaborn
  
# importing the required module
import matplotlib.pyplot as plt
import seaborn as sns
  
# use to set style of background of plot
sns.set(style ="whitegrid")
  
# loading data-set
iris = sns.load_dataset('iris');
  
# plotting strip plot with seaborn
# deciding the attributes of dataset on which plot should be made
ax = sns.stripplot(x = 'species', y = 'sepal_length', data = iris);
  
# giving title to the plot
plt.title('Graph')
  
# function to show plot
plt.show()

chevron_right


Output:

Explanation:

  • iris is the dataset already present in seaborn module for use.
  • We use .load_dataset() function in order to load the data.We can also load any other file by giving path and name of file in the argument.
  • .set(style=”whitegrid”) function here is also use to define the background of plot.We can use “darkgrid”
    instead of whitegrid if we want dark colored background.
  • In .stripplot() function we have define which attribute of the dataset to be on x-axis and which attribute of dataset should on y-axis.data = iris means attributes which we define earlier should be taken from the given data.
  • We can also draw this plot with matplotlib but problem with matplotlib is its default parameters. The reason why Seaborn is so great with DataFrames is, for example, labels from DataFrames are automatically propagated to plots or other data structures as you see in the above figure column name species comes on x-axis and column name stepal_length comes on y-aixs, that is not possible with matplotlib. We have to explicitly define the labels of x-axis and y-axis.

Swarmplot:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to illustrate
# plotting using Swarmplot
  
# importing the required module
import matplotlib.pyplot as plt
import seaborn as sns
  
# use to set style of background of plot
sns.set(style ="whitegrid")
  
# loading data-set
iris = sns.load_dataset('iris');
  
# plotting strip plot with seaborn
# deciding the attributes of dataset on which plot should be made
ax = sns.swarmplot(x = 'species', y = 'sepal_length', data = iris);
  
# giving title to the plot
plt.title('Graph')
  
# function to show plot
plt.show()

chevron_right


Output:

Explanation:
This is very much similar to striplot but the only difference is that is do not allow overlapping of markers.It cause jittering in the markers of the plot so that graph can easily be readed without information loss as seen in the above plot.

  • We use .swarmplot() function to plot swarn plot.
  • Another difference that we can notice in Seaborn and Matplotlib is that working with DataFrames doesn’t go quite as smoothly with Matplotlib, which can be annoying if we doing exploratory analysis with Pandas. And that’s exactly what Seaborn do easily, the plotting functions operate on DataFrames and arrays that contain a whole dataset.

NOTE : If we want we can also change the representation of data on a particular axis. For example :

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required module
import matplotlib.pyplot as plt
import seaborn as sns
  
# use to set style of background of plot
sns.set(style ="whitegrid")
  
# loading data-set
iris = sns.load_dataset('iris');
  
# plotting strip plot with seaborn
# deciding the attributes of dataset on which plot should be made
ax = sns.swarmplot(x = 'sepal_length', y = 'species', data = iris);
  
  
# giving title to the plot
plt.title('Graph')
  
# function to show plot
plt.show()

chevron_right


Output:

The same can be done in striplot. At last we can say that Seaborn is extended version of matplotlib which tries to make a well-defined set of hard things easy.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.