【PHP】ヒストグラム作成ツール

PHP

PHP-Histogram

以前記事にした度数分布表作成ツール PHP-FrequencyTable

を使って分析した結果から、ヒストグラムを作成するツール

PHP-Histogram を作りました。(PHP >= 8.1)

Githubで公開しています。Composerでインストールできます。

GitHub - macocci7/PHP-Histogram: PHP-Histogram creates histograms using FrequencyTable.
PHP-Histogram creates histograms using FrequencyTable. - macocci7/PHP-Histogram

PHP CLI環境で簡単で便利に使えるツールになっていると思います。

このような感じでヒストグラムを簡単に作ることができます。

インストールの仕方

ローカルPCの作業用フォルダを作り、CLIで次のコマンドを実行するだけです。

ここでは、~/work/histogram/ の中で実行しています。

composer require macocci7/php-histogram

使い方

PHPコードの中で、データを1次元配列で渡して階級幅を指定し create($path) するだけです。

使用例が同梱されているので、それをコピーしてきていじるのがイイかもしれません。

cp -ra vendor/macocci7/php-histogram/examples/ ./

これでフォルダを丸ごとコピーしてきたので、

examples フォルダ内の HistogramExample.php を見てみましょう。

<?php
require('../vendor/autoload.php');

use Macocci7\PhpHistogram\Histogram;

$hg = new Histogram();
$hg->ft->setClassRange(5);
$hg->ft->setData([0,5,8,10,12,13,15,16,17,18,19,20,24]);
$hg->create('img/Histogram.png');

これを実行しみるとヒストグラム画像が examples/img/Histogram.png に保存されます。

php -f HistogramExample.php

このようなヒストグラムのPNG画像になっています。

色やフォント、グリッドの太さの指定、度数折れ線、累積相対度数折れ線の表示、

ラベルやキャプションなど、プロパティを色々とカスタマイズすることもできます。

examples/ChangeProperties.php を編集してみましょう。

<?php
require('../vendor/autoload.php');

use Macocci7\PhpHistogram\Histogram;

$hg = new Histogram();
$hg->ft->setClassRange(5);
$hg->ft->setData([1, 5, 6, 10, 12, 14, 15, 16, 17, 18, 20, 24, 25]);
$hg->resize(600,400) // 画像のサイズ(幅, 高さ) : 規定値(400, 300)
   ->frame(0.6, 0.6) // 画像サイズに対する描画領域の割合(横, 縦)
   ->bgcolor('#3333cc') // 背景色: #rgb か #rrggbb 形式で指定
   ->axis(3, '#ffffff') // 軸(幅ピクセル, 色)
   ->grid(1, '#cccccc') // グリッド(幅ピクセル, 色)
   ->color('#99aaff') // ヒストグラム棒の色
   ->border(4, '#0000ff') // ヒストグラム棒の境界線(幅ピクセル, 色)
   ->fp(4, '#00ff00') // 度数折れ線(幅ピクセル, 色)
   ->crfp(3, '#ffff00') // 累積相対度数折れ線(幅ピクセル, 色)

   // True Type フォントの使用システム上の実在するパスを指定
   ->fontPath('/usr/share/fonts/truetype/ipafont-nonfree-uigothic/ipagui.ttf')
   ->fontSize(20)
   ->fontColor('#ffff99') // 階級、度数、ラベル、キャプションの色
   ->barOn() // ヒストグラム棒の表示 barOn():ON / barOff(): OFF
   ->fpOn() // 度数折れ線の表示 fpOn():ON / fpOff(): OFF
   ->crfpOn() // 累積相対度数折れ線の表示 crfpOn(): ON / crfpOff(): OFF
   ->frequencyOn() // 度数の表示 frequencyOn(): ON / frequencyOff(): OFF
   ->labelX('階級(個)') // 横軸のラベル
   ->labelY('度数(人)') // 縦軸のラベル
   ->caption('商品購入数 / 月(2023年5月)') // キャプション
   ->create('img/ChangeProperties.png');

ラベルとキャプションを日本語にしてみました。これを実行してみます。

php -f ChangeProperties.php

このようなPNG画像ができます。(examples/img/ChangeProperties.png)

他にも、箱ひげ図作成ツール PHP-Boxplot、散布図作成ツール PHP-Scatterplot もあるので、そちらもよろしくです。

GitHub - macocci7/PHP-Boxplot: PHP-Boxplot creates boxplots using FrequencyTable.
PHP-Boxplot creates boxplots using FrequencyTable. - macocci7/PHP-Boxplot
GitHub - macocci7/PHP-Scatterplot: PHP-Scatterplot is a simple PHP library to create scatter plot easily.
PHP-Scatterplot is a simple PHP library to create scatter plot easily. - macocci7/PHP-Scatterplot

おまけ

Ubuntuのシステム上の True Typeフォントの一覧は次のコマンドで取得できます。

fc-list | grep .ttf
/usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf: DejaVu Serif:style=Bold
/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf: DejaVu Sans Mono:style=Book
/usr/share/fonts/truetype/ubuntu/UbuntuMono-RI.ttf: Ubuntu Mono:style=Italic
/usr/share/fonts/truetype/ipafont-nonfree-jisx0208/ipag0208_for_legacy_compatibility.ttf: IPAX0208Gothic,IPA X0208 ゴシック:style=Regular
/usr/share/fonts/truetype/noto/NotoSansMono-Regular.ttf: Noto Sans Mono:style=Regular
/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book
/usr/share/fonts/truetype/fonts-japanese-mincho.ttf: IPAX0208Mincho,IPA X0208 明朝:style=Regular
/usr/share/fonts/truetype/ubuntu/UbuntuMono-B.ttf: Ubuntu Mono:style=Bold
/usr/share/fonts/opentype/ipafont-mincho/ipam.ttf: IPAMincho,IPA明朝:style=Regular
/usr/share/fonts/truetype/ubuntu/Ubuntu-L.ttf: Ubuntu,Ubuntu Light:style=Light,Regular
/usr/share/fonts/truetype/ubuntu/Ubuntu-BI.ttf: Ubuntu:style=Bold Italic
/usr/share/fonts/truetype/fonts-japanese-gothic.ttf: IPAX0208Gothic,IPA X0208 ゴシック:style=Regular
/usr/share/fonts/truetype/ubuntu/Ubuntu-Th.ttf: Ubuntu,Ubuntu Thin:style=Thin,Regular
/usr/share/fonts/truetype/ubuntu/Ubuntu-RI.ttf: Ubuntu:style=Italic
/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold
/usr/share/fonts/truetype/ubuntu/Ubuntu-MI.ttf: Ubuntu,Ubuntu Light:style=Medium Italic,Bold Italic
/usr/share/fonts/truetype/ubuntu/UbuntuMono-BI.ttf: Ubuntu Mono:style=Bold Italic
/usr/share/fonts/truetype/ubuntu/Ubuntu-B.ttf: Ubuntu:style=Bold
/usr/share/fonts/opentype/ipafont-gothic/ipagp.ttf: IPAPGothic,IPA Pゴシック:style=Regular
/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf: Droid Sans Fallback:style=Regular
/usr/share/fonts/truetype/ipafont-nonfree-jisx0208/ipagui0208_for_legacy_compatibility.ttf: IPAX0208UIGothic,IPA X0208 UIゴシック:style=Regular
/usr/share/fonts/truetype/ubuntu/UbuntuMono-R.ttf: Ubuntu Mono:style=Regular
/usr/share/fonts/truetype/ipafont-nonfree-jisx0208/ipagp0208_for_legacy_compatibility.ttf: IPAX0208PGothic,IPA X0208 Pゴシック:style=Regular
/usr/share/fonts/truetype/ubuntu/Ubuntu-LI.ttf: Ubuntu,Ubuntu Light:style=Light Italic,Italic
/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf: DejaVu Sans Mono:style=Bold
/usr/share/fonts/opentype/ipafont-mincho/ipamp.ttf: IPAPMincho,IPA P明朝:style=Regular
/usr/share/fonts/opentype/ipafont-gothic/ipag.ttf: IPAGothic,IPAゴシック:style=Regular
/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf: Ubuntu:style=Regular
/usr/share/fonts/truetype/ubuntu/Ubuntu-M.ttf: Ubuntu,Ubuntu Light:style=Medium,Bold
/usr/share/fonts/truetype/noto/NotoSansMono-Bold.ttf: Noto Sans Mono:style=Bold
/usr/share/fonts/truetype/ipafont-nonfree-uigothic/ipagui.ttf: IPAUIGothic,IPA UIゴシック:style=Regular
/usr/share/fonts/truetype/ubuntu/Ubuntu-C.ttf: Ubuntu Condensed:style=Regular
/usr/share/fonts/truetype/noto/NotoMono-Regular.ttf: Noto Mono:style=Regular
/usr/share/fonts/truetype/ipafont-nonfree-jisx0208/ipamp0208_for_legacy_compatibility.ttf: IPAX0208PMincho,IPA X0208 P明朝:style=Regular
/usr/share/fonts/truetype/ipafont-nonfree-jisx0208/ipam0208_for_legacy_compatibility.ttf: IPAX0208Mincho,IPA X0208 明朝:style=Regular
/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf: DejaVu Serif:style=Book

たくさんありますね。このうち、日本語対応のものはIPA系のフォントです。

(2024/03/10追記) Version 1.1.0にバージョンアップしました。

詳細についてはこちらの記事をご覧ください。

コメント

タイトルとURLをコピーしました