Stem Plot

Probability Distribution Stem

Poisson PMF visualized with stems.

Output
Probability Distribution Stem
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

COLORS = {
    'stem': '#8B5CF6',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
    'grid': '#F1F5F9',
}

# Poisson distribution
x = np.arange(0, 12)
y = stats.poisson.pmf(x, mu=4)

fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])

for xi, yi in zip(x, y):
    ax.plot([xi, xi], [0, yi], color=COLORS['stem'], linewidth=3, alpha=0.7, zorder=2)
    ax.scatter(xi, yi, s=150, c=COLORS['stem'], alpha=0.2, zorder=3)
    ax.scatter(xi, yi, s=80, c=COLORS['stem'], edgecolors='white', linewidths=2, zorder=4)

ax.axhline(0, color=COLORS['text'], linewidth=1, zorder=1)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color(COLORS['grid'])
ax.spines['bottom'].set_color(COLORS['grid'])

ax.yaxis.grid(True, color=COLORS['grid'], linewidth=1, zorder=0)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)

ax.set_xlim(-0.5, 11.5)
ax.set_ylim(0, 0.25)
ax.set_xlabel('k', fontsize=10, color=COLORS['text'], labelpad=10)
ax.set_ylabel('P(X = k)', fontsize=10, color=COLORS['text'], labelpad=10)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support