Line Chart Plotting in Python using Matplotlib

In this tutorial, we’ll talk about how to draw a line chart plot using the famous charting library of Python i.e Matplotlib.

First of all, we need to install the matplotlib in our system. The best way to install it is by using pip.

Type the following command in your terminal to install it.

pip install matplotlib

Now, Import the library by writing the following python code.

import matplotlib.pyplot as plt

After importing the matplotlib library, let’s begin making some awesome line chart plots.

Plotting of line chart using Matplotlib Python library

Let us start making a simple line chart in matplotlib.

As we know that line charts are used to represent the relationship between two variables on different axes i.e X and Y.

First, we need to declare some X-axis points and some corresponding Y-axis points. See the following code declaring two lists (X and Y).

X = [1,2,3,4,5]
Y = [2,4,6,8,10]

After declaring the points of the X-axis and Y-axis, we are going to use the matplotlib library to plot the line plot for these points.

See the following code

# Importing the library
import matplotlib.pyplot as plt
X = [1,2,3,4,5] # X-axis points
Y = [2,4,6,8,10] # Y-axis points
plt.plot(X,Y) # Plotting the line plot
plt.show() #Displaying the plot

Output

line chart with matplotlib in Python

As we can see in the above output, there is no label on the x-axis and on the y-axis. Since labeling is necessary to denote the meaning of the plot, so we need to add some labels to the plot. Follow the code to understand how labeling is done.

import matplotlib.pyplot as plt

X = [1,2,3,4,5]
Y = [2,4,6,8,10]

plt.plot(X,Y)

# Labeling the X-axis 
plt.xlabel('X-axis') 

# Labeling the Y-axis 
plt.ylabel('Y-axis') 

# Give a title to the graph
plt.title('Simple Line Plot') 

plt.show()

Output

line chart with matplotlib in Python

As the title of the above plot, this graph is very simple and is also not looking very good. Let make some more real meaningful graphs with more awesome formatting to make it look better and attractive.

Two plots on the same graph

First of all, let’s see how to make two plots on the same graph.

import matplotlib.pyplot as plt 
 
# Declaring the points for first line plot
X1 = [1,2,3,4,5] 
Y1 = [2,4,6,8,10] 
# plotting the first plot
plt.plot(X1, Y1, label = "plot 1") 

# Declaring the points for second line plot
X2 = [1,2,3,4,5] 
Y2 = [1,4,9,16,25]

# plotting the second plot 
plt.plot(X2, Y2, label = "plot 2") 
  
# Labeling the X-axis 
plt.xlabel('X-axis') 

# Labeling the Y-axis 
plt.ylabel('Y-axis') 

# Give a title to the graph
plt.title('Two plots on the same graph') 
  
# Show a legend on the plot 
plt.legend() 
 
plt.show() 

Output

line chart

Note: A rectangular box at the top left corner of the graph is called legend. This box gives information about the different plots in the graph with different colors and line types. This is done with the help of legend() function.

Also, read:

More advanced plot with matplotlib

Here is one advance plot which uses all of the above steps and an extra library used to declare x and y points for the plot.

# Importing matplotlib and numpy module
import matplotlib.pyplot as plt 
import numpy as np
# Declaring x points for sine graph
x1 = np.arange(0,4*np.pi,0.1)   # start,stop,step
y1 = np.sin(x1)
plt.plot(x1,y1,label='sin(X)')

# Declaring x points for cos graph
x2 = np.arange(0,4*np.pi,0.1)   # start,stop,step
y2 = np.cos(x1)
plt.plot(x2,y2,label='cos(X)')

plt.xlabel('X-axis') 

# Labeling the Y-axis 
plt.ylabel('Y-axis') 

# Give a title to the graph
plt.title('Sin(x) and Cos(x) on the same graph') 
  
# Show a legend on the plot 
plt.legend() 
 
plt.show() 

Output

Advance matplotlib plot

Editing and customizing the plots

Not so good looking plots make visualization boring and hence making it difficult to understand the hidden relationships in the graph. Here are some of the customization options available in matplotlib.

Let us consider the above graph. Here we are going to apply some formatting to the sine and cos plots.

See and try to understand the following code.

# Importing matplotlib and numpy module
import matplotlib.pyplot as plt 
import numpy as np

# Setting the figure size
f=plt.figure(figsize=(20,10))

# Declaring x points for sine graph
x1 = np.arange(0,4*np.pi,0.1)   # start,stop,step
y1 = np.sin(x1)

# Some formatting for sine graph
plt.plot(x1,y1,label='sin(X)',color='orange', linestyle='dashed', linewidth = 3, 
         marker='o', markerfacecolor='blue', markersize=8)

# Declaring x points for cos graph
x2 = np.arange(0,4*np.pi,0.1)   # start,stop,step
y2 = np.cos(x1)

# Some formatting for cos graph
plt.plot(x2,y2,label='cos(X)',color='green', linestyle=':', linewidth = 2, 
         marker='^', markerfacecolor='pink', markersize=7)

plt.xlabel('X-axis') 

# Labeling the Y-axis 
plt.ylabel('Y-axis') 

# Give a title to the graph
plt.title('Sin(x) and Cos(x) on the same graph') 
  
# Show a legend on the plot 
plt.legend() 
 
plt.show() 

Code explanation

  • plt.figure() function is used to set the figure size of the output image.
  • Some customization is applied to sine(x) graph such as color is set to green, linestyle is of dashed(–) style and a maker type is also set to¬† ‘o’(dot) with color = blue.
  • Similarly, the plot of cos(x) is formatted with different options.

Output

plot in Python using matplotlib

See the plot() documentation for a complete list of line styles and format strings.

In this tutorial, we have seen how to make some simple and advance line chart plots using matplotlib and finally, we also have seen some of the basic and advance formatting and customization of line plots.

I hope you liked the article. Comment if you have any doubts or suggestions regarding this article.

Leave a Reply

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