I’ve been working with Python and Matplotlib for over a decade, and one thing I often do is visualize multiple datasets on separate plots within the same figure. Whether you’re comparing sales trends across different US states or analyzing temperature variations in multiple cities, plotting multiple lines in subplots is an essential skill.
Matplotlib makes this task easy, but the many ways to create and customize subplots can sometimes be confusing or complex to navigate. In this article, I’ll share my firsthand experience and guide you through the most effective methods to plot multiple lines in subplots using Matplotlib.
Let’s get right in!
What Are Subplots in Matplotlib?
Subplots are individual plots placed inside a single figure. They allow you to display multiple charts side-by-side or stacked vertically, making it easier to compare different datasets visually.
For example, if you want to compare monthly sales data for California and Texas, instead of plotting them on the same graph, you can use subplots to have separate plots for each state, but in the same figure window.
Method 1: Use plt.subplots() to Create Multiple Line Plots
The most common and flexible way to create subplots is using the plt.subplots() function. It returns a figure and an array of axes objects, which you can use to plot your data.
Here’s a simple example where I plot two lines in two subplots, one for California’s monthly sales and another for Texas.
import matplotlib.pyplot as plt
# Sample monthly sales data for California and Texas
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
california_sales = [25000, 27000, 30000, 28000, 32000, 35000]
texas_sales = [22000, 24000, 26000, 25000, 27000, 30000]
# Create subplots: 2 rows, 1 column
fig, axes = plt.subplots(2, 1, figsize=(8, 6))
# Plot California sales
axes[0].plot(months, california_sales, marker='o', color='blue', label='California')
axes[0].set_title('Monthly Sales in California')
axes[0].set_ylabel('Sales ($)')
axes[0].legend()
axes[0].grid(True)
# Plot Texas sales
axes[1].plot(months, texas_sales, marker='s', color='green', label='Texas')
axes[1].set_title('Monthly Sales in Texas')
axes[1].set_xlabel('Month')
axes[1].set_ylabel('Sales ($)')
axes[1].legend()
axes[1].grid(True)
plt.tight_layout()
plt.show()You can see the output in the screenshot below.

Why I Like This Method
- It gives you full control over each subplot.
- Easy to customize titles, labels, and legends independently.
- The
figsizeparameter helps adjust the overall figure size to fit your needs.
Method 2: Plot Multiple Lines on Each Subplot
Sometimes, you want to compare multiple lines within each subplot. For example, plotting sales for two products in California and Texas separately.
Here’s how I do it:
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
california_product_a = [15000, 16000, 17000, 16500, 18000, 19000]
california_product_b = [10000, 11000, 13000, 11500, 14000, 16000]
texas_product_a = [12000, 13000, 14000, 13500, 15000, 16000]
texas_product_b = [10000, 11000, 12000, 11500, 12000, 14000]
fig, axes = plt.subplots(2, 1, figsize=(8, 6))
# California products
axes[0].plot(months, california_product_a, marker='o', label='Product A')
axes[0].plot(months, california_product_b, marker='x', label='Product B')
axes[0].set_title('California Sales by Product')
axes[0].set_ylabel('Sales ($)')
axes[0].legend()
axes[0].grid(True)
# Texas products
axes[1].plot(months, texas_product_a, marker='o', label='Product A')
axes[1].plot(months, texas_product_b, marker='x', label='Product B')
axes[1].set_title('Texas Sales by Product')
axes[1].set_xlabel('Month')
axes[1].set_ylabel('Sales ($)')
axes[1].legend()
axes[1].grid(True)
plt.tight_layout()
plt.show()You can see the output in the screenshot below.

This approach helps me compare multiple related datasets clearly within each subplot.
Method 3: Use plt.subplot() for Quick Plots
If you want a quick way to create subplots without managing axes arrays, plt.subplot() is handy. It lets you specify the grid and the position of each subplot.
Here’s an example plotting three lines in three subplots horizontally:
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
new_york_sales = [20000, 21000, 22000, 21500, 23000, 25000]
florida_sales = [18000, 19000, 20000, 19500, 21000, 23000]
illinois_sales = [16000, 17000, 18000, 17500, 19000, 21000]
plt.figure(figsize=(12, 4))
# New York
plt.subplot(1, 3, 1)
plt.plot(months, new_york_sales, marker='o', color='red')
plt.title('NY Sales')
plt.ylabel('Sales ($)')
plt.grid(True)
# Florida
plt.subplot(1, 3, 2)
plt.plot(months, florida_sales, marker='s', color='orange')
plt.title('Florida Sales')
plt.grid(True)
# Illinois
plt.subplot(1, 3, 3)
plt.plot(months, illinois_sales, marker='^', color='purple')
plt.title('Illinois Sales')
plt.grid(True)
plt.tight_layout()
plt.show()You can see the output in the screenshot below.

This method is straightforward for a small number of subplots, but less flexible when you want to customize many axes.
Tips for Better Subplots
- Use
plt.tight_layout()to automatically adjust spacing and avoid label overlap. - Add grids for easier readability.
- Customize markers and colors to distinguish lines clearly.
- Label your axes and add legends for clarity.
- Adjust
figsizeto make sure your plots are not cramped or too spread out.
I hope this guide helps you confidently plot multiple lines in subplots using Matplotlib. Whether you’re analyzing business data or scientific results, these methods make your visualizations clear and professional.
If you want to explore even more customization, Matplotlib offers features like shared axes, inset plots, and interactive widgets, but mastering these basics will get you a long way.
You may read:
- Multiple Lines on Line Plot or Time Series with Matplotlib
- Matplotlib Plotting Multiple Lines in 3D
- Matplotlib Plot Multiple Lines with Same Color

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.