幹葉図とは、ヒストグラムに似た図です。
4-6. 幹葉表示 | 統計学の時間 | 統計WEB
この図をPython で描画できるか調べたところ、dionresearch/stemgraphic: stemgraphic python package for visualization of data and text というライブラリで、描けそうでしたので、試しに使ってみました。
結果を記載します。


図の使いどころ

統計検定の問題で初めて見ました。使用方法についての説明は、Wikipedia の記載がわかりやすいです。
幹葉表示 - Wikipedia

また、人文・社会科学の統計学 - 東京大学出版会 には以下のように記載されています。引用します。

幹葉表示 stem and leaf representation とは、個別の観測値の適当なケタまでを「幹」としてその次のケタの数を「葉」として簡易なヒストグラムの形に整理していくものである。

個人的には、PC が使えない環境下で人力でヒストグラムを描く際、数を数える段階で使えるかなと思います。
Python で試してみていますが、幹葉表示 とヒストグラムどちらかを選択するなら、ヒストグラムを選択するかと思います。


インストール

pip でインストールできます。

  • コマンド

    python3 -m pip install -U stemgraphic  
    

  • OUTPUT

    Installing collected packages: docopt, seaborn, stemgraphic
    Successfully installed docopt-0.6.2 seaborn-0.9.0 stemgraphic-0.7.2
    


動かしてみる

stemgraphic quickstart with numbers — stemgraphic 0.6.1 documentation を参考に実際に動かしてみます。
Sample は、数値、カテゴリデータ、テキストを扱う3種類あります。
今回は、数値を扱うSampleを動かしてみます。

数値データを幹葉表示する

%matplotlib inline
import pandas as pd
from stemgraphic import stem_graphic
# Sample データの読みこみ
df = pd.read_csv('https://raw.githubusercontent.com/dionresearch/stemgraphic/master/datasets/iris.csv')
df.describe()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
sepal_length sepal_width petal_length petal_width
count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.054000 3.758667 1.198667
std 0.828066 0.433594 1.764420 0.763161
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000
stem_graphic(df['petal_width'])

png

ヒートマップ表示

ヒートマップを描画するメソッドが提供されています。

from stemgraphic.graphic import heatmap
heatmap(df['petal_width'])
(       0  1  2  3  4  5  6  7  8  9
 stem                               
   1    6  0  0  0  0  0  0  0  0  0
   2   28  0  0  0  0  0  0  0  0  0
   3    7  0  0  0  0  0  0  0  0  0
   4    7  0  0  0  0  0  0  0  0  0
   5    1  0  0  0  0  0  0  0  0  0
   6    1  0  0  0  0  0  0  0  0  0
   7    0  0  0  0  0  0  0  0  0  0
   8    0  0  0  0  0  0  0  0  0  0
   9    0  0  0  0  0  0  0  0  0  0
  10    7  0  0  0  0  0  0  0  0  0
  11    3  0  0  0  0  0  0  0  0  0
  12    5  0  0  0  0  0  0  0  0  0
  13   13  0  0  0  0  0  0  0  0  0
  14    8  0  0  0  0  0  0  0  0  0
  15   12  0  0  0  0  0  0  0  0  0
  16    4  0  0  0  0  0  0  0  0  0
  17    2  0  0  0  0  0  0  0  0  0
  18   12  0  0  0  0  0  0  0  0  0
  19    5  0  0  0  0  0  0  0  0  0
  20    6  0  0  0  0  0  0  0  0  0
  21    6  0  0  0  0  0  0  0  0  0
  22    3  0  0  0  0  0  0  0  0  0
  23    8  0  0  0  0  0  0  0  0  0
  24    3  0  0  0  0  0  0  0  0  0
  25    3  0  0  0  0  0  0  0  0  0,
 0.1,
 <matplotlib.axes._subplots.AxesSubplot at 0x10a4215c0>)

png

データのばらつき、範囲を調整すると、もう少しきれいに描画できるのかもしれません。

濃度曲線を描画する

濃度曲線を描画するメソッドが提供されています。

from stemgraphic.graphic import density_plot
density_plot(df['petal_width'])
(<matplotlib.figure.Figure at 0x10d15c438>,
 <matplotlib.axes._subplots.AxesSubplot at 0x10cca66d8>)

png

メソッドの引数を指定することで、グラフの描画する種類を切り替えることができます。

# hist= True でヒストグラムを追加、rug=True ラグを追加
from stemgraphic.graphic import density_plot
density_plot(df['petal_width'], hist=True, limit_var=True, rug=True)
(<matplotlib.figure.Figure at 0x10ef79518>,
 <matplotlib.axes._subplots.AxesSubplot at 0x10a1ab898>)

png

以下に、サンプル notebook が Github 上に存在します。
stemgraphic/notebooks at master · dionresearch/stemgraphic
カテゴリデータ、テキストのサンプルも存在するので、後日試してみようかと思います。

以上です。

コメント