This is a detailed tutorial on Plotting Line Graphs in Python. Learn to plot and customize, single-line and multi-line graphs using the `matplotlib`

module.

Table of Contents

## Plotting Line Graphs in Python

It’s quite easy to plot a line graph in Python using the `matplotlib`

module. If you do not have this module installed on your system, you can quickly install it using the following command.

pip install matplotlib

This is the most popular library that one can use in Python to create graphs and to draw different kinds of data visualisations.

### Plotting a Single-Line Graph

To plot a single line graph, all you need to do is to first import the `matplotlib`

module into your Python program and then you have to use the `pyplot.plot`

method of this module.

Let’s draw a 2-dimensional single-line graph with some random data. You need to follow the given steps to make this graph.

As you know to plot a line graph we simply have to join the different `(x, y)`

coordinates. Therefore, you first have to define all of the `x`

and `y`

coordinates. You have to prepare two different Python List for the purpose. One of these lists contains the values for the x-values, while the second list will contain the y-values.

Then we’ve to plot these values on the graph using the `plot()`

method. You’ve to pass the x-values and y-values list as the first and the second argument of this method. You can optionally give a label to the line that is going to be drawn using these values by specifying the third keyword-argument `label`

.

You can then given label to the x-axis and the y-axis using the `xlabel()`

and `ylabel()`

method respectively. In the end, you can give a title for this plot using the `title()`

method and the end to show this graph, we’ll use the `show()`

function.

**Example.**

import matplotlib.pyplot as p #x-values x = [0, 1, 2, 3, 4, 5] #y-values y = [0, 5, 4, 9, 13, 7] #Plotting the Line on Graph p.plot(x, y) #Giving Label to x-axis p.xlabel("X-AXIS LABEL") #Giving Label to y-axis p.ylabel("Y-AXIS LABEL") #Giving Title To Plotted Graph p.title("SINGLE LINE GRAPH EXAMPLE") #DISPLAY the plot p.show()

**Output.**

As you can see the above graph is following the exact coordinates that are created using the values present in the `x`

and `y`

lists. Also, you can observe the labels printed as we defined in the code.

### Plotting a Multi-Line Graph

To plot a single line graph we were using the `plot()`

method once and to plot a graph of `n`

number of times, we simply need to use the `plot()`

method, `n`

number of times.

**Example.**

In the following example, I am plotting a graph with 2 different lines drawn using two different sets of coordinates.

import matplotlib.pyplot as p #x1-values x1 = [0, 1, 2, 3, 4, 5] #y1-values y1 = [5, 4, 3, 0, 2, 1] #x2-values x2 = [0, 1, 2, 3, 4, 5] #y2-values y2 = [10, 8, 0, 3, 2, 5] #Plotting the Line 1 on Graph p.plot(x1, y1, label = "Line 1") #Plotting the Line 2 on Graph p.plot(x2, y2, label = "Line 2") #Giving Label to x-axis p.xlabel("X-AXIS LABEL") #Giving Label to y-axis p.ylabel("Y-AXIS LABEL") #Giving Title To Plotted Graph p.title("MULTI-LINE GRAPH EXAMPLE") #To Show LEGEND p.legend() #DISPLAY the plot p.show()

Here I’ve also specified the label arguments for the two different lines in the `plot()`

method. Also, I’ve used the `legend()`

method to show a legend specifying the line labels at the *Top-Right* of the graph. This way you can plot any number of lines on the same graph.

### Graph Customizations

The `matplotlib.pyplot.plot()`

method can be provided with a lot of different arguments to customize the line graph. Some of these keyword-specific arguments are described below.

**color.**To specify the color of the line. Example,`color = "red"`

**linestyle.**To specify the style of the line. Example,`line = "dashed"`

**linewidth.**To specify the width of the line. Example,`linewidth = 5`

**marker.**Different markers are available to highlight the coordinate points. Example,`marker = "o"`

**markerfacecolor.**To specify the face color of the marker. Example,`markerfacecolor = "pink"`

**markersize.**To specify the size of the marker. Example,`markersize = 10`

You can also specify the limits of the graph by using the `xlim()`

and `ylim()`

methods.

**Example.**

All of the above-specified customizations are illustrated in the following example plotting a two-liner graph.

import matplotlib.pyplot as p #x1-values x1 = [0, 1, 2, 3, 4, 5] #y1-values y1 = [5, 3, 4, 2, 0, 1] #x2-values x2 = [0, 1, 2, 3, 4, 5] #y2-values y2 = [10, 0, 8, 2, 5, 3] #Plotting the Line 1 on Graph p.plot(x1, y1, label = "Line 1", color = "red", linestyle = "dashed", linewidth = 5, marker="o", markerfacecolor = "pink", markersize = 10) #Plotting the Line 2 on Graph p.plot(x2, y2, label = "Line 2", color = "brown", linestyle = "dotted", linewidth = 2, marker="+", markerfacecolor = "gray", markersize = 15) #Giving Label to x-axis p.xlabel("X-AXIS LABEL") #Giving Label to y-axis p.ylabel("Y-AXIS LABEL") #Giving Title To Plotted Graph p.title("MULTI-LINE GRAPH EXAMPLE") #To Show LEGEND p.legend() #DISPLAY the plot p.show()

**Output.**

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!