 # Plot Multiple lines in Matplotlib

## Plot Multiple lines in Matplotlib

In this article, we will learn how to plot multiple lines using matplotlib in Python. Let’s discuss some concepts:

• Matplotlib: Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002.
• Line plot: Line plots can be created in Python with Matplotlib’s pyplot library. To build a line plot, first import Matplotlib. It is a standard convention to import Matplotlib’s pyplot library as plt. The plt alias will be familiar to other Python programmers.

Here we will discuss some examples to draw a line or multiple lines with different features. To do such work we must follow the steps given below:

• Import libraries.
• Create Data.
• Plot the lines over data.

## Plotting a single Horizontal Line

In this example, we will learn how to draw a horizontal line with the help of matplotlib. Here we will use two lists as data for two dimensions (x and y) and at last plot the line. For making a horizontal line we have to change the value of the x-axis continuously by taking the y-axis as constant.

 `# importing package` `import` `matplotlib.pyplot as plt` ` ` `# create data` `x ``=` `[``10``,``20``,``30``,``40``,``50``]` `y ``=` `[``30``,``30``,``30``,``30``,``30``]` ` ` `# plot line` `plt.plot(x, y)` `plt.show()`

Output: ## Plotting a single Vertical Line

In this example, we will learn how to draw a vertical line with the help of matplotlib. Here we will use two lists as data with two dimensions (x and y) and at last plot the line. For making a vertical line we have to change the value of the y-axis continuously by taking the x-axis as constant. So we change the axes to get a vertical line.

 `# importing package` `import` `matplotlib.pyplot as plt` ` ` `# create data` `x ``=` `[``10``,``20``,``30``,``40``,``50``]` `y ``=` `[``30``,``30``,``30``,``30``,``30``]` ` ` `# plot line` `plt.plot(y,x)` `plt.show()`

Output: ## Plotting a Horizontal and a Vertical Line

In this example, we will learn how to draw a horizontal line and a vertical line both in one graph with the help of matplotlib. Here we will use two list as data with two dimensions (x and y) and at last plot the line with respect to the dimensions. So, in this example we merge the above both graphs to make both lines together in a graph.

 `# importing package` `import` `matplotlib.pyplot as plt` ` ` `# create data` `x ``=` `[``10``,``20``,``30``,``40``,``50``]` `y ``=` `[``30``,``30``,``30``,``30``,``30``]` ` ` `# plot lines` `plt.plot(x, y, label ``=` `"line 1"``)` `plt.plot(y, x, label ``=` `"line 2"``)` `plt.legend()` `plt.show()`

Output: ## Plotting Multiple Lines

In this example, we will learn how to draw multiple lines with the help of matplotlib. Here we will use two lists as data with two dimensions (x and y) and at last plot the lines as different dimensions and functions over the same data.

To draw multiple lines we will use different functions which are as follows:

• y = x
• x = y
• y = sin(x)
• y = cos(x)
 `# importing package` `import` `matplotlib.pyplot as plt` `import` `numpy as np` ` ` `# create data` `x ``=` `[``1``,``2``,``3``,``4``,``5``]` `y ``=` `[``3``,``3``,``3``,``3``,``3``]` ` ` `# plot lines` `plt.plot(x, y, label ``=` `"line 1"``)` `plt.plot(y, x, label ``=` `"line 2"``)` `plt.plot(x, np.sin(x), label ``=` `"curve 1"``)` `plt.plot(x, np.cos(x), label ``=` `"curve 2"``)` `plt.legend()` `plt.show()`

Output: ## Plotting Multiple Lines with different Line styles

This example is similar to the above example and the enhancement is the different line styles. This can help in the modification of better visualization.

Here we will use different line styles which are as follows:

• –        : dashed
•       : double dashed
• -.       : dashed-dotted
•        : dotted
 `# importing package` `import` `matplotlib.pyplot as plt` `import` `numpy as np` ` ` `# create data` `x ``=` `[``1``,``2``,``3``,``4``,``5``]` `y ``=` `[``3``,``3``,``3``,``3``,``3``]` ` ` `# plot lines` `plt.plot(x, y, label ``=` `"line 1"``, linestyle``=``"-"``)` `plt.plot(y, x, label ``=` `"line 2"``, linestyle``=``"--"``)` `plt.plot(x, np.sin(x), label ``=` `"curve 1"``, linestyle``=``"-."``)` `plt.plot(x, np.cos(x), label ``=` `"curve 2"``, linestyle``=``":"``)` `plt.legend()` `plt.show()`

Output: Last Updated on October 28, 2021 by admin

## __call__ in Python__call__ in Python

__call__ in Python Python has a set of built-in methods and __call__ is one of them. The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function. When the instance is called as

## Python list pop() MethodPython list pop() Method

Python list | pop() Python list pop() is an inbuilt function in Python that removes and returns the last value from the List or the given index value. Syntax: list_name.pop(index) Parameter: index (optional) – The value at index is popped out and removed. If the

## Python program to interchange first and last elements in a listPython program to interchange first and last elements in a list

Python program to interchange first and last elements in a list   Given a list, write a Python program to swap first and last element of the list. Examples: Input : [12, 35, 9, 56, 24] Output : [24, 35,

## Statement, Indentation and Comment in PythonStatement, Indentation and Comment in Python

Statement, Indentation and Comment in Python Instructions written in the source code for execution are called statements. There are different types of statements in the Python programming language like Assignment statements, Conditional statements, Looping statements, etc. These all help the

## Ethical Hacking with PythonEthical Hacking with Python

Ethical Hacking with Python As a Computer Science Engineer who encrypts the world, one should know how Hacking activities are done. And we must stand front in protecting our world from cybercriminals. Being able to gain access to a system

## Python – Union of two or more ListsPython – Union of two or more Lists

Python | Union of two or more Lists Union of a list means, we must take all the elements from list A and list B (there can be more than two lists) and put them inside a single new list.

## Python | Maximum and minimum element’s position in a listPython | Maximum and minimum element’s position in a list

Python – Maximum and minimum element’s position in a list   Given a list of N integers, find the maximum and minimum element’s position in the list. Examples: Input : 3, 4, 1, 3, 4, 5 Output : The maximum

## Python – geometry method in TkinterPython – geometry method in Tkinter

Python | geometry method in Tkinter Tkinter is a Python module that is used to develop GUI (Graphical User Interface) applications. It comes along with Python, so you do not have to install it using the pip command. Tkinter provides many methods; one