Create Two Y Axes Bar Plot in Matplotlib

Working on a data visualization project for a U.S.-based retail company, I needed to compare two different metrics, monthly sales revenue and customer satisfaction scores, on the same chart.

The challenge was that both datasets had completely different scales. The sales numbers were in thousands, while the satisfaction scores were on a scale of 1 to 10.

In this tutorial, I’ll show you step-by-step how to create a dual Y-axis bar chart in Matplotlib using Python. I’ll also share a few variations and practical tips based on my Python experience working on real-world projects.

What is a Two-Y-Axis Bar Plot in Python?

A two-Y-axis bar plot (also called a dual-axis plot) allows you to visualize two datasets with different scales on the same X-axis.

In Python’s Matplotlib library, we can easily create such plots using the twinx() function, which creates a secondary Y-axis that shares the same X-axis as the first.

Use a Dual Y-Axis Bar Chart

I often use this type of chart when comparing metrics that have different units or magnitudes.

For example, in a U.S. business context, you might want to visualize monthly revenue (in USD) and customer satisfaction (in percentage) together. A dual-axis chart helps display both without distorting their scale.

Method 1 – Create a Two-Y-Axis Bar Plot Using twinx() in Matplotlib

This is the most common and easiest way to create a two-Y-axis bar plot in Python. Here’s how I usually do it step-by-step.

Step 1: Import Required Python Libraries

Before we begin, we need to import the necessary Python libraries. I’ll use matplotlib.pyplot for plotting and numpy for numerical operations.

import matplotlib.pyplot as plt
import numpy as np

This step ensures we have the required tools to create and customize the dual Y-axis bar chart.

Step 2: Prepare the Data

Let’s create a small dataset representing monthly sales (in thousands of dollars) and customer satisfaction scores (out of 10) for a U.S.-based retail store.

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [120, 150, 170, 130, 180, 200]  # in $1000s
satisfaction = [7.5, 8.0, 8.2, 7.8, 8.5, 9.0]
x = np.arange(len(months))

Here, the x variable stores the numeric positions for our X-axis labels.

Step 3: Create the Primary Y-Axis (Sales Data)

Now, I’ll create the first bar chart for sales data.

fig, ax1 = plt.subplots(figsize=(10, 6))
bar_width = 0.4

bars1 = ax1.bar(x - bar_width/2, sales, width=bar_width, color='skyblue', label='Sales ($1000s)')
ax1.set_xlabel('Month')
ax1.set_ylabel('Sales ($1000s)', color='blue')
ax1.tick_params(axis='y', labelcolor='blue')
ax1.set_xticks(x)
ax1.set_xticklabels(months)

This code creates a bar chart showing monthly sales and labels the Y-axis in blue for clarity.

Step 4: Add the Secondary Y-Axis (Satisfaction Data)

Now comes the key part, adding a secondary Y-axis using twinx().

ax2 = ax1.twinx()
bars2 = ax2.bar(x + bar_width/2, satisfaction, width=bar_width, color='orange', label='Satisfaction Score')
ax2.set_ylabel('Customer Satisfaction (1–10)', color='orange')
ax2.tick_params(axis='y', labelcolor='orange')

The twinx() function creates a new Y-axis on the right side of the plot that shares the same X-axis. This allows both bar charts to align perfectly.

Step 5: Add Title and Legends

Adding a descriptive title and legend helps make the chart more readable.

plt.title('Monthly Sales vs Customer Satisfaction (Two Y Axes Bar Plot in Python)')
fig.legend(loc='upper left', bbox_to_anchor=(0.1, 0.9))
plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Create Two Y Axes Bar Plot Matplotlib

This completes the first dual Y-axis bar plot. You’ll now see a clean, professional chart comparing sales and satisfaction levels side by side.

Method 2 – Customize the Dual Y-Axis Bar Plot

Once you’ve created the basic chart, you can easily customize it for better presentation. I often adjust bar transparency, add gridlines, and annotate key values to make the chart more insightful.

Here’s an enhanced version of the previous plot:

import matplotlib.pyplot as plt
import numpy as np

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [120, 150, 170, 130, 180, 200]
satisfaction = [7.5, 8.0, 8.2, 7.8, 8.5, 9.0]
x = np.arange(len(months))
bar_width = 0.4

fig, ax1 = plt.subplots(figsize=(10, 6))

bars1 = ax1.bar(x - bar_width/2, sales, width=bar_width, color='steelblue', alpha=0.7, label='Sales ($1000s)')
ax1.set_xlabel('Month')
ax1.set_ylabel('Sales ($1000s)', color='steelblue')
ax1.tick_params(axis='y', labelcolor='steelblue')
ax1.grid(axis='y', linestyle='--', alpha=0.5)

ax2 = ax1.twinx()
bars2 = ax2.bar(x + bar_width/2, satisfaction, width=bar_width, color='darkorange', alpha=0.7, label='Satisfaction Score')
ax2.set_ylabel('Customer Satisfaction (1–10)', color='darkorange')
ax2.tick_params(axis='y', labelcolor='darkorange')

plt.title('Monthly Sales vs Customer Satisfaction – Dual Y Axes Bar Plot in Python')
fig.legend(loc='upper left', bbox_to_anchor=(0.1, 0.9))
plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Matplotlib Create Two Y Axes Bar Plot

This version looks cleaner and more visually appealing, making it ready for presentations or business reports.

Method 3 – Combine Bar and Line Plot with Two Y Axes

Sometimes, using two bar charts can make the graph look cluttered. In such cases, I prefer combining a bar plot for one variable and a line plot for the other. This approach improves readability.

Here’s how you can do it in Python:

import matplotlib.pyplot as plt
import numpy as np

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [120, 150, 170, 130, 180, 200]
satisfaction = [7.5, 8.0, 8.2, 7.8, 8.5, 9.0]
x = np.arange(len(months))

fig, ax1 = plt.subplots(figsize=(10, 6))
ax1.bar(x, sales, color='cornflowerblue', label='Sales ($1000s)')
ax1.set_xlabel('Month')
ax1.set_ylabel('Sales ($1000s)', color='blue')
ax1.tick_params(axis='y', labelcolor='blue')

ax2 = ax1.twinx()
ax2.plot(x, satisfaction, color='red', marker='o', linewidth=2, label='Satisfaction Score')
ax2.set_ylabel('Customer Satisfaction (1–10)', color='red')
ax2.tick_params(axis='y', labelcolor='red')

plt.title('Sales vs Satisfaction (Bar and Line Plot with Two Y Axes in Python)')
fig.legend(loc='upper left', bbox_to_anchor=(0.1, 0.9))
plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Create Two Y Axes Matplotlib Bar Plot

This method is ideal when one dataset (like satisfaction scores) has fewer variations and can be easily represented as a line.

Best Practices for Two Y Axes in Python Charts

Over the years, I’ve learned a few important lessons when using two Y axes in Python visualizations:

  • Use contrasting colors for clarity between datasets.
  • Label both Y axes clearly to avoid confusion.
  • Avoid overloading the chart with too many series.
  • Add annotations when presenting to non-technical audiences.
  • Ensure proportional scaling so the chart doesn’t mislead viewers.

These small touches make your Python visualizations more professional and insightful.

When I first started using Matplotlib for business analytics, I often struggled to balance readability and accuracy. But with dual Y-axis bar plots, I could finally present multiple metrics side by side without sacrificing clarity.

Whether you’re analyzing U.S. retail sales, comparing production and energy costs, or visualizing marketing performance, this technique is incredibly useful.

With Python and Matplotlib, you get full control to customize and fine-tune every aspect of your chart.

You may also like to read:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.