Line & Scatter
Dual Y-Axis Comparison
Two different scales plotted on left and right Y axes.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# === STYLE CONFIG ===
COLORS = {
'left': '#6366F1', # Indigo
'right': '#F97316', # Orange
'background': '#FAFBFC',
'text_muted': '#64748B',
'grid': '#E2E8F0',
}
# === DATA ===
x = np.linspace(0, 10, 100)
y1 = 50 + 30 * np.sin(x) # Range: 20-80
y2 = 1000 + 800 * np.cos(0.5 * x) # Range: 200-1800
# === FIGURE ===
fig, ax1 = plt.subplots(figsize=(10, 6), dpi=100)
ax1.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])
# === LEFT AXIS ===
line1, = ax1.plot(x, y1, color=COLORS['left'], linewidth=2.5, label='Metric A')
ax1.set_xlabel('Time', fontsize=10, color=COLORS['text_muted'], labelpad=10)
ax1.set_ylabel('Metric A', fontsize=10, color=COLORS['left'], labelpad=10)
ax1.tick_params(axis='y', colors=COLORS['left'], labelsize=9, length=0, pad=8)
ax1.tick_params(axis='x', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax1.set_ylim(0, 100)
# === RIGHT AXIS ===
ax2 = ax1.twinx()
line2, = ax2.plot(x, y2, color=COLORS['right'], linewidth=2.5, label='Metric B')
ax2.set_ylabel('Metric B', fontsize=10, color=COLORS['right'], labelpad=10)
ax2.tick_params(axis='y', colors=COLORS['right'], labelsize=9, length=0, pad=8)
ax2.set_ylim(0, 2000)
# === STYLING ===
ax1.spines['top'].set_visible(False)
ax1.spines['bottom'].set_color(COLORS['grid'])
ax1.spines['left'].set_color(COLORS['left'])
ax1.spines['right'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax2.spines['right'].set_color(COLORS['right'])
ax2.spines['bottom'].set_visible(False)
ax1.yaxis.grid(True, color=COLORS['grid'], linewidth=0.5, alpha=0.5)
ax1.set_axisbelow(True)
# Combined legend
lines = [line1, line2]
labels = [l.get_label() for l in lines]
ax1.legend(lines, labels, loc='upper center', bbox_to_anchor=(0.5, -0.12),
ncol=2, frameon=False, fontsize=9, labelcolor=COLORS['text_muted'])
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Line & Scatter examples
☕