Linear Regression Plot
Ad Impressions vs Click-Through Rate
Digital marketing effectiveness analysis
Output
Python
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
np.random.seed(410)
BG_COLOR = '#0a0a0f'
TEXT_COLOR = 'white'
n = 75
impressions = np.random.uniform(10, 500, n)
ctr = 4 - 0.005 * impressions + np.random.normal(0, 0.5, n)
ctr = np.clip(ctr, 0.5, 5)
df = pd.DataFrame({'Impressions (K)': impressions, 'CTR (%)': ctr})
fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)
sns.regplot(
data=df,
x='Impressions (K)',
y='CTR (%)',
scatter_kws={'color': '#F527B0', 'alpha': 0.7, 's': 55, 'edgecolor': 'white', 'linewidths': 0.5},
line_kws={'color': '#27D3F5', 'linewidth': 2.5, 'linestyle': '-'},
ci=95,
ax=ax
)
for collection in ax.collections[1:]:
collection.set_facecolor('#27D3F5')
collection.set_alpha(0.15)
ax.axhline(2.0, color='#F5B027', linestyle='--', alpha=0.5, linewidth=1)
ax.text(480, 2.1, 'Industry Avg', color='#F5B027', fontsize=9, ha='right')
corr = np.corrcoef(impressions, ctr)[0, 1]
ax.text(0.05, 0.05, 'r = %.3f' % corr, transform=ax.transAxes, fontsize=12,
color='#27D3F5', fontweight='bold', va='bottom',
bbox=dict(boxstyle='round,pad=0.3', facecolor='#1a1a2e', edgecolor='#333'))
ax.set_xlabel('Ad Impressions (K)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Click-Through Rate (%)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Ad Impressions vs CTR', fontsize=14, color=TEXT_COLOR, fontweight='bold', pad=15)
ax.tick_params(colors='#888', labelsize=10)
for spine in ax.spines.values():
spine.set_color('#333')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Linear Regression Plot examples
☕