はじめに
matplotlibのstackplotで積み上げ面グラフを表示する方法を例を示して説明する。
コード

解説
モジュールのインポートなど
バージョン
データの生成
np.random.randint(1,5,6)で1〜5のランダムな整数を6個生成する。
積み上げ面グラフの表示
ax.stackplot(x, y1, y2, y3)のようにすることで、かんたんに積み上げ面グラフを作成できる。labelsを設定することでlegendで凡例を表示できる。

yをまとめて積み上げ面グラフ
np.vstack([y1, y2, y3])のようにしてyをひとつにすることでも積み上げ面グラフが作成できる。

baseline=’sym’とした場合
ax.stackplot()でbaseline=’sym’とすることで0を中心とした対象的な積み上げ面グラフとなる。

baseline=’wiggle’とした場合
ax.stackplot()でbaseline=’wiggle’とすることで二乗勾配の合計を最小化した積み上げ面グラフとなる。

参考
matplotlib.axes.Axes.stackplot — Matplotlib 3.10.0 documentation
Stackplots and streamgraphs — Matplotlib 3.10.0 documentation
コメント