Line chart plotting using Seaborn in Python

This tutorial will teach you how to plot a line chart graph using two very useful Python libraries that are seaborn and matplotlib.

Seaborn is a data visualization library based on matplotlib and is used to create visually attractive and detailed graphs.

Installing seaborn and matplotlib

If you already have seaborn and matplotlib libraries installed on your machine, you can skip this step.

Install seaborn

  1. Open your machine’s terminal if you are using Linux or else open Command Prompt (CMD) if you are using Windows.
  2. Type the following line and press enter.
pip install seaborn --user

Install matplotlib

  1. Open your machine’s terminal if you are using Linux or else open Command Prompt (CMD) if you are using Windows.
  2. Type the following line and press enter.
pip install matplotlib --user

Importing the required libraries

First of all, we will import the libraries that are required

import matplotlib.pyplot as plt
import seaborn as sb

Here, we are using the ‘as’ keyword to create an alias name. So, say if we want to use any function of the seaborn library. we can simply write sb.<function_name> instead of writing seaborn.<function_name> every time.

pyplot is a module in matplotlib used to plot graphs easily and conveniently.

 

Now, we are going to take the x-axis and y-axis values. This can be done in multiple ways. We can extract the values from a file, we can give the values from the keyboard during runtime or we can even give them some predefined values.

Here, we are going to give the x-axis and y-axis values using the keyboard during runtime. We will be using the x-axis values as the parameters and y-axis values as their respective values.

Be careful to ensure that each x-axis values has a corresponding y-axis value.

Python program using seaborn for line chart plotting

Below is our complete Python program using the seaborn Python library:

#Importing the matplotlib.pyplot module and the seaborn library

import matplotlib.pyplot as plt
import seaborn as sns

#Taking input for x-axis and y-axis values

x_axis = input("Enter X-Axis Values seperated by a comma : ").split(",")
y_axis = list(map(float,input("Enter Y-Axis Values : ").split(",")))

#Taking input for x-axis and y-axis labels

x_label = input("Enter X-Axis Label : ")
y_label = input("Enter Y-Axis Label : ")

#Taking input for title of the graph
title = input("Enter Title to Your Graph : ")

graph = sns.lineplot(x_axis,y_axis,sort=False)     #Creating the graph
graph.set(xlabel=x_label,ylabel=y_label)      #Setting the x and y axes labels

plt.title(title)    #Setting the title of the graph

plt.show()  #Plotting and showing the graph

In the code above, the program will first take inputs for the x and y axes values. Next, it will take input for the x and y axes labels. Finally, you enter a title for your graph. Keep in mind that the number of x and y axes values should be the same otherwise, there will be an error. Sort is set to False so that there is no unnecessary sorting done by the function as long as you have entered the data in a sorted manner.

The sns.lineplot() function creates a line chart. It has other parameters too that can be used to further modify and make the graph look better.

The set() function is used to set various styles and options in the graph and the plt.title() function is used to set a title to the graph and finally, plt.show() is used to show the graph.

Output

Say, we want to create a graph, “Sales Curve” to represent the sales of a certain item on various days. The following will the output:

Line chart plotting using Seaborn in Python

You can plot very complex graphs quite easily using the seaborn library. This was just a basic tutorial and I encourage you to explore more about the seaborn library as it is one of the most useful and popular libraries in Python.

Also, read: The Python Seaborn module

Leave a Reply

Your email address will not be published. Required fields are marked *