Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to combine multiple graphs in Python
Python's Matplotlib library allows you to combine multiple graphs in a single figure to create comprehensive visualizations. You can use subplots to display different charts vertically or horizontally, and dual axes to overlay different data types on the same plot.
Preparing Sample Data
First, let's prepare sample data for mobile phone sales across different years ?
import matplotlib.pyplot as plt
# Sample mobile phone sales data (in millions)
mobile_brands = ['iPhone', 'Galaxy', 'Pixel']
# Sales data: (Year, iPhone, Galaxy, Pixel)
units_sold = (
('2016', 12, 8, 6),
('2017', 14, 10, 7),
('2018', 16, 12, 8),
('2019', 18, 14, 10),
('2020', 20, 16, 5),
)
# Split the data into separate arrays
Years, iPhone_Sales, Galaxy_Sales, Pixel_Sales = zip(*units_sold)
Position = list(range(len(units_sold)))
Method 1: Using Subplots
Create multiple graphs in separate subplot areas ?
import matplotlib.pyplot as plt
# Sample data
units_sold = (
('2016', 12, 8, 6),
('2017', 14, 10, 7),
('2018', 16, 12, 8),
('2019', 18, 14, 10),
('2020', 20, 16, 5),
)
Years, iPhone_Sales, Galaxy_Sales, Pixel_Sales = zip(*units_sold)
Position = list(range(len(units_sold)))
# Create figure with subplots
plt.figure(figsize=(10, 8))
# First subplot - iPhone sales (bar chart)
plt.subplot(2, 1, 1)
plt.bar(Position, iPhone_Sales, color='green', alpha=0.7)
plt.ylabel('iPhone Sales (Millions)')
plt.title('iPhone Sales Over Years')
plt.xticks(Position, Years)
# Second subplot - Pixel sales (line chart)
plt.subplot(2, 1, 2)
plt.plot(Position, Pixel_Sales, 'o-', color='orange', linewidth=2)
plt.ylabel('Pixel Sales (Millions)')
plt.title('Pixel Sales Over Years')
plt.xticks(Position, Years)
plt.ylim(0, max(Pixel_Sales) + 2)
plt.tight_layout()
plt.show()
Method 2: Using Dual Y-Axes
Overlay different data types on the same plot with separate y-axes ?
import matplotlib.pyplot as plt
# Sample data
units_sold = (
('2016', 12, 8, 6),
('2017', 14, 10, 7),
('2018', 16, 12, 8),
('2019', 18, 14, 10),
('2020', 20, 16, 5),
)
Years, iPhone_Sales, Galaxy_Sales, Pixel_Sales = zip(*units_sold)
Position = list(range(len(units_sold)))
# Create figure
fig, ax1 = plt.subplots(figsize=(10, 6))
# First y-axis - iPhone sales (bar chart)
ax1.bar(Position, iPhone_Sales, color='green', alpha=0.7, label='iPhone')
ax1.set_ylabel('iPhone Sales (Millions)', color='green')
ax1.set_xlabel('Years')
ax1.set_xticks(Position)
ax1.set_xticklabels(Years)
# Create second y-axis
ax2 = ax1.twinx()
ax2.plot(Position, Galaxy_Sales, 'o-', color='blue', linewidth=2, label='Galaxy')
ax2.set_ylabel('Galaxy Sales (Millions)', color='blue')
plt.title('Combined iPhone and Galaxy Sales')
plt.tight_layout()
plt.show()
Method 3: Multiple Lines on Same Plot
Display multiple data series on the same axes for easy comparison ?
import matplotlib.pyplot as plt
# Sample data
units_sold = (
('2016', 12, 8, 6),
('2017', 14, 10, 7),
('2018', 16, 12, 8),
('2019', 18, 14, 10),
('2020', 20, 16, 5),
)
Years, iPhone_Sales, Galaxy_Sales, Pixel_Sales = zip(*units_sold)
Position = list(range(len(units_sold)))
# Create single plot with multiple lines
plt.figure(figsize=(10, 6))
plt.plot(Position, iPhone_Sales, 'o-', label='iPhone', linewidth=2, color='green')
plt.plot(Position, Galaxy_Sales, 's-', label='Galaxy', linewidth=2, color='blue')
plt.plot(Position, Pixel_Sales, '^-', label='Pixel', linewidth=2, color='orange')
plt.xlabel('Years')
plt.ylabel('Sales (Millions)')
plt.title('Mobile Phone Sales Comparison')
plt.xticks(Position, Years)
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Complete Example with Saving
Here's a comprehensive example that combines different visualization techniques ?
import matplotlib.pyplot as plt
# Sample data
units_sold = (
('2016', 12, 8, 6),
('2017', 14, 10, 7),
('2018', 16, 12, 8),
('2019', 18, 14, 10),
('2020', 20, 16, 5),
)
Years, iPhone_Sales, Galaxy_Sales, Pixel_Sales = zip(*units_sold)
Position = list(range(len(units_sold)))
# Create figure with multiple subplots
fig = plt.figure(figsize=(12, 10))
# Subplot 1: Bar chart with dual axis
plt.subplot(2, 2, 1)
ax1 = plt.gca()
ax1.bar(Position, iPhone_Sales, color='green', alpha=0.7)
ax1.set_ylabel('iPhone Sales', color='green')
ax1.set_xticks(Position)
ax1.set_xticklabels(Years)
ax2 = ax1.twinx()
ax2.plot(Position, Galaxy_Sales, 'o-', color='blue')
ax2.set_ylabel('Galaxy Sales', color='blue')
plt.title('iPhone (Bar) + Galaxy (Line)')
# Subplot 2: Multiple lines
plt.subplot(2, 2, 2)
plt.plot(Position, iPhone_Sales, 'o-', label='iPhone', color='green')
plt.plot(Position, Galaxy_Sales, 's-', label='Galaxy', color='blue')
plt.plot(Position, Pixel_Sales, '^-', label='Pixel', color='orange')
plt.ylabel('Sales (Millions)')
plt.xticks(Position, Years)
plt.legend()
plt.title('All Brands Comparison')
# Subplot 3: Stacked bar chart
plt.subplot(2, 2, 3)
plt.bar(Position, iPhone_Sales, label='iPhone', color='green')
plt.bar(Position, Galaxy_Sales, bottom=iPhone_Sales, label='Galaxy', color='blue')
plt.ylabel('Cumulative Sales')
plt.xticks(Position, Years)
plt.legend()
plt.title('Stacked Sales')
# Subplot 4: Individual brand focus
plt.subplot(2, 2, 4)
plt.plot(Position, Pixel_Sales, 'o-', color='orange', linewidth=3)
plt.fill_between(Position, Pixel_Sales, alpha=0.3, color='orange')
plt.ylabel('Pixel Sales')
plt.xticks(Position, Years)
plt.title('Pixel Sales Trend')
plt.tight_layout()
plt.savefig('/assets/combined_graphs.png', dpi=150, bbox_inches='tight')
plt.show()
Comparison
| Method | Best For | Advantages |
|---|---|---|
| Subplots | Different chart types | Clear separation, easy to read |
| Dual Y-Axes | Different scales/units | Direct comparison, space efficient |
| Same Plot | Similar data ranges | Immediate comparison, simple |
Conclusion
Use subplots when displaying different chart types or when data ranges vary significantly. Use dual y-axes to overlay different metrics on the same timeline. Choose the method that best highlights the relationships in your data.
