# python plot multiple lines in same figure with code examples

Python is a popular and versatile programming language that has been widely adopted across various fields, especially data science and machine learning. One of the most useful features of Python is its ability to plot different types of graphs and charts, thereby providing a graphical representation of data. In this article, we will discuss how to plot multiple lines in the same figure using Python and also provide code examples to help you get started.

Plotting Multiple Lines in One Figure

In Python, we can use the Matplotlib library to plot graphs and charts. Before we dive into the code examples, let's take a quick look at how to plot multiple lines in one figure using Matplotlib.

To plot multiple lines in one figure using Matplotlib, we need to do the following:

1. Import the Matplotlib library:
```import matplotlib.pyplot as plt
```
1. Create a figure:
```fig = plt.figure()
```
1. Add one or more subplots to the figure:
```ax = fig.add_subplot(111)
```
1. Plot the lines on the subplot(s):
```ax.plot(x1, y1, label="Line 1")
ax.plot(x2, y2, label="Line 2")
# Add more lines as needed
```
1. Set any desired properties, such as axis labels, titles, and legends:
```ax.set_xlabel("X label")
ax.set_ylabel("Y label")
ax.set_title("Title")
ax.legend()
```
1. Show the plot:
```plt.show()
```

Code Examples

Now that we have a general idea of how to plot multiple lines in one figure using Matplotlib, let's take a look at some code examples to see how it works in practice.

Example 1 – Plotting Two Lines on the Same Graph

In this example, we will plot two lines on the same graph. Each line will represent a different set of data.

```import matplotlib.pyplot as plt

# Data for Line 1
x1 = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]

# Data for Line 2
x2 = [1, 2, 3, 4, 5]
y2 = [1, 3, 5, 7, 9]

# Create a figure and subplot
fig = plt.figure()

# Plot the lines
ax.plot(x1, y1, label="Line 1")
ax.plot(x2, y2, label="Line 2")

# Set properties
ax.set_xlabel("X label")
ax.set_ylabel("Y label")
ax.set_title("Title")
ax.legend()

# Show the plot
plt.show()
```

Output: Example 2 – Plotting Two Lines with Different Styles

In this example, we will plot two lines with different line styles. Line 1 will be a solid line, while Line 2 will be a dashed line.

```import matplotlib.pyplot as plt

# Data for Line 1
x1 = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]

# Data for Line 2
x2 = [1, 2, 3, 4, 5]
y2 = [1, 3, 5, 7, 9]

# Create a figure and subplot
fig = plt.figure()

# Plot the lines
ax.plot(x1, y1, label="Line 1", linestyle="-")
ax.plot(x2, y2, label="Line 2", linestyle="--")

# Set properties
ax.set_xlabel("X label")
ax.set_ylabel("Y label")
ax.set_title("Title")
ax.legend()

# Show the plot
plt.show()
```

Output: Example 3 – Plotting Multiple Lines on Separate Subplots

In this example, we will plot multiple lines on separate subplots. Line 1 will be plotted on the first subplot, and Line 2 will be plotted on the second subplot.

```import matplotlib.pyplot as plt

# Data for Line 1
x1 = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]

# Data for Line 2
x2 = [1, 2, 3, 4, 5]
y2 = [1, 3, 5, 7, 9]

# Create a figure and subplots
fig, (ax1, ax2) = plt.subplots(1, 2)

# Plot the lines
ax1.plot(x1, y1)
ax2.plot(x2, y2)

# Set properties
ax1.set_xlabel("X label")
ax1.set_ylabel("Y label")
ax1.set_title("Line 1")

ax2.set_xlabel("X label")
ax2.set_ylabel("Y label")
ax2.set_title("Line 2")

# Show the plot
plt.show()
```

Output: Conclusion

In this article, we discussed how to plot multiple lines in the same figure using Python, specifically with the Matplotlib library. We provided code examples to demonstrate how to plot two lines on the same graph, plot two lines with different styles, and plot multiple lines on separate subplots.

Having the ability to plot multiple lines in the same figure is incredibly useful, as it provides a quick and easy way to compare multiple data sets and analyze trends. With these code examples, you now have the tools you need to get started plotting multiple lines in one figure using Python and Matplotlib.

Sure! Let's expand on some of the topics covered in the previous article.

Matplotlib Library

Matplotlib is a popular library for data visualization in Python. It provides a wide range of tools to create various types of plots such as line plots, scatter plots, histograms, and more. The library is highly customizable, which means that we can adjust almost any aspect of our graph, including color, font, label, and other properties we want to modify.

The library is compatible with various operating systems and can obtain huge popularity among data scientists, data analysts, and other professionals working with data in different industries such as finance, healthcare, marketing, and more.

Subplots

Subplots are a useful way to create multiple graphs in the same figure. In Python's Matplotlib library, we can create subplots using the `add_subplot()` method. The parameters of the method determine how many rows and columns of subplots we want, as well as which subplot we are currently working with.

By creating subplots, we can easily compare data sets side-by-side or analyze trends within the same graph. We can apply different styles and properties to subplots separately, which means that we can customize each subplot to our liking and use it to compare different sets of data.

Line Styles

In Python's Matplotlib library, we can use different line styles to distinguish lines from one another. Line styles include solid, dashed, dotted, and more.

To change the line style of a plotted line, we can use the `linestyle` parameter in the `plot()` method. Depending on the type of data we are working with, it might make sense to use different line styles for different lines.

Conclusion

Python is a powerful programming language that provides a wealth of tools for data analysis and visualization. One of the most popular libraries for data visualization is Matplotlib, which provides comprehensive tools for plotting different types of graphs, including line plots. By using subplots and line styles in our plots, we can create highly customized visualizations that help us analyze and understand data sets. As more and more industries rely on data and data analytics, understanding how to use Python for data visualization becomes increasingly important.

## Popular questions

1. What is the purpose of using subplots in Matplotlib?
Answer: Subplots are useful for creating multiple graphs in the same figure. By using subplots, we can easily compare data sets side-by-side or analyze trends within the same graph.

2. What is the first step in plotting multiple lines in the same figure using Matplotlib?
Answer: The first step is to import the Matplotlib library using the `import matplotlib.pyplot as plt` statement.

3. Can we customize different subplots separately in Matplotlib?
Answer: Yes, we can customize different subplots separately in Matplotlib. We can modify almost any aspect of our graph, including color, font, label, and other properties according to our needs.

4. How can we change the line style of a plotted line in Matplotlib?
Answer: To change the line style of a plotted line, we can use the `linestyle` parameter in the `plot()` method. Depending on the type of data we are working with, it might make sense to use different line styles for different lines.

5. What are the benefits of plotting multiple lines in one figure using Python and Matplotlib?
Answer: Plotting multiple lines in one figure is beneficial as it provides a quick and easy way to compare multiple data sets and analyze trends. With Python and Matplotlib, we have the tools we need to visualize data sets efficiently and accurately. It's also a useful skill for data scientists, data analysts, and other professionals who work with data in different industries.

### Tag

"Multiline Plotting" ##### Ahmed Galal
As a senior DevOps Engineer, I possess extensive experience in cloud-native technologies. With my knowledge of the latest DevOps tools and technologies, I can assist your organization in growing and thriving. I am passionate about learning about modern technologies on a daily basis. My area of expertise includes, but is not limited to, Linux, Solaris, and Windows Servers, as well as Docker, K8s (AKS), Jenkins, Azure DevOps, AWS, Azure, Git, GitHub, Terraform, Ansible, Prometheus, Grafana, and Bash.
Posts created 3294

## go to last row excel with code examples

Begin typing your search term above and press enter to search. Press ESC to cancel.