信頼帯付き Q–Q プロット

元の変数と対数変換した変数について、95% 包絡線付きの正規 Q–Q プロットを描きます。

信頼帯付き Q–Q プロット

Q–Q プロット(分位点–分位点プロット)は、並べ替えて標準化したデータを、正規分布から得られる分位点に対してプロットしたものです。正規分布に従うデータは対角線上に並び、弓なりに曲がったパターンは歪みを意味します。網掛けの帯は、正規順序統計量のベータ分布から構成した各点ごとの 95% 包絡線で、その外側にある点は偶然では説明しにくいほど極端な値です。

舞台は Warin(2021、JMIR Medical Informatics)による、コロナウイルスに関する世界の研究をメタデータに基づいて分析した論文です。ここでの数値は説明のために シミュレーション したもの——コロナウイルスに関する n = 300 本の論文の被引用数で、対数正規の平均の周りのポアソン計数として生成——であり、論文のデータや結果ではありません。左のパネルは元の被引用数、右のパネルは numpy.log1p で計算した log(1 + 被引用数) です。分位点は scipy.stats.norm.ppf、包絡線は scipy.stats.beta.ppf で求め、各パネルにシャピロ・ウィルク検定の結果を示しています。

コードおよび図中のテキストは英語です。

qq-plot.py · 45 行
ダウンロード GitHub
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

# Simulated data, set in the context of Warin (2021) [warin2021corona]

# Data: citations per paper, raw and log(1 + x)
rng = np.random.default_rng(2021)
citations = rng.poisson(rng.lognormal(2.5, 1.0, 300))
samples = {"Raw citations": citations, "log(1 + citations)": np.log1p(citations)}
palette = ["#FF9500", "#007AFF"]

fig, axes = plt.subplots(1, 2, figsize=(10, 5), sharex=True, sharey=True)
for ax, (name, x), c in zip(axes, samples.items(), palette):
    n = len(x)
    z = np.sort((x - x.mean()) / x.std(ddof=1))      # standardised sample quantiles
    pp = (np.arange(1, n + 1) - 0.5) / n              # plotting positions
    theo = stats.norm.ppf(pp)                         # theoretical normal quantiles

    # 95% pointwise envelope: order statistic i of a uniform sample is Beta(i, n-i+1)
    i = np.arange(1, n + 1)
    lo = stats.norm.ppf(stats.beta.ppf(0.025, i, n - i + 1))
    hi = stats.norm.ppf(stats.beta.ppf(0.975, i, n - i + 1))
    outside = (z < lo) | (z > hi)

    ax.fill_between(theo, lo, hi, color="#8E8E93", alpha=0.18, lw=0, label="95% envelope")
    ax.plot([-3.5, 3.5], [-3.5, 3.5], color="#8E8E93", lw=1, ls="--")
    ax.scatter(theo[~outside], z[~outside], s=16, color=c, alpha=0.7, lw=0)
    ax.scatter(theo[outside], z[outside], s=22, color="#FF3B30", lw=0,
               label=f"outside band ({outside.sum()})")

    W, p = stats.shapiro(x)
    ax.set_title(name, pad=20, fontsize=12)
    ax.text(0, 1.02, f"Shapiro–Wilk W = {W:.3f}, p = {p:.1e} · n = {n} papers (simulated)",
            transform=ax.transAxes, fontsize=9.5, color="#6E6E73")
    ax.legend(loc="upper left")
    ax.grid(axis="x")
    ax.set_xlabel("Theoretical normal quantiles")

axes[0].set_ylabel("Standardised sample quantiles")
axes[0].set_xlim(-3.2, 3.2)
axes[0].set_ylim(-3.3, 8.3)  # room for the most-cited papers
fig.suptitle("Citation counts are heavily right-skewed; log(1 + x) brings them close to normal",
             x=0.01, ha="left", fontsize=14)
plt.show()

図にはオプションの PyStatlas スタイル を使用しています。同じ見た目にするには plt.style.use("pystatlas.mplstyle") を追加してください。

データの出典

  • Simulated data. Simulated data with a fixed random seed, created for this example.
    No real respondents, documents or measurements: the numbers only illustrate how the chart and its statistics work.
  • Warin (2021), global research on coronaviruses. Warin, T. (2021). Global research on coronaviruses: Metadata-based analysis for public health policies. JMIR Medical Informatics, 9(11), e31510. https://doi.org/10.2196/31510
    The example is set in the context of this paper; unless its description says otherwise, the numbers are simulated and are not the paper's data or results.

これらの例は統計手法を説明するためのものです。項目に別の記載がない限り数値はシミュレーションであり、図のタイトルはこのシミュレーションデータで図が示す内容を述べたものです。引用した論文の知見でも、実質的な問いに対する著者の立場でもありません。結果、背景、データについては、ここに挙げた論文を参照してください。

引用方法

PyStatlas は Thierry Warin が作成・運営しています。研究や業務に役立った場合は、ぜひ引用してください。

Warin, T. (2026). Q–Q plot with confidence band. In PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences (Version 1.1.0) [Computer software]. https://warin.ca/pystatlas/distributions/qq-plot/

@misc{warin2026statlas,
  author       = {Warin, Thierry},
  title        = {PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences},
  year         = {2026},
  version      = {1.1.0},
  howpublished = {\url{https://warin.ca/pystatlas/}},
  note         = {Source code: https://github.com/warint/pystatlas}
}

分布のその他のグラフ