パーセンタイルの水平棒グラフの作成

PythonPythonBeginner
今すぐ練習

This tutorial is from open-source community. Access the source code

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、Python の Matplotlib ライブラリを使用して水平棒グラフを作成する方法を学びます。体育の教師が保護者に、他の子供たちと比較して、自分の子供がいくつかのフィットネステストでどのような結果を出したかを示したいという例を使います。デモ目的で、小さなジョニー・ドゥのデータを作成し、プロットコードを抽出します。

VM のヒント

VM の起動が完了したら、左上隅をクリックして Notebook タブに切り替え、Jupyter Notebook を開いて練習を行います。

時々、Jupyter Notebook の読み込みが完了するまで数秒待つ必要がある場合があります。Jupyter Notebook の制限により、操作の検証を自動化することはできません。

学習中に問題が発生した場合は、いつでも Labby に質問してください。セッション終了後にフィードバックを提供していただければ、迅速に問題を解決します。

ライブラリのインポート

必要なライブラリをインポートすることから始めます。データを作成するために numpy を、グラフをプロットするために matplotlib.pyplot を使用します。

import matplotlib.pyplot as plt
import numpy as np

データの定義

名前付きタプルを使用してデータを定義します。生徒の名前、学年、性別を持つ Student タプルを定義します。また、スコアの値、単位、パーセンタイルを持つ Score タプルも定義します。

from collections import namedtuple

Student = namedtuple('Student', ['name', 'grade', 'gender'])
Score = namedtuple('Score', ['value', 'unit', 'percentile'])

ヘルパー関数の定義

2 つのヘルパー関数を定義します。最初の関数 to_ordinal は、整数を序数の文字列に変換します(例:2 -> '2nd')。2 番目の関数 format_score は、右側の y 軸のスコアラベルを作成します。テスト名の後に測定単位(あれば)が続き、2 行に分割されます。

def to_ordinal(num):
    suffixes = {str(i): v
                for i, v in enumerate(['th', 'st', 'nd', 'rd', 'th',
                                       'th', 'th', 'th', 'th', 'th'])}
    v = str(num)
    if v in {'11', '12', '13'}:
        return v + 'th'
    return v + suffixes[v[-1]]

def format_score(score):
    return f'{score.value}\n{score.unit}' if score.unit else str(score.value)

プロット関数の定義

Student タプル、テストごとのスコアの辞書、およびコホートのサイズを引数に取る plot_student_results という関数を定義します。この関数は、各テストのパーセンタイルランキングを、生徒の学年と性別のコホートと比較して水平棒グラフで作成します。

def plot_student_results(student, scores_by_test, cohort_size):
    fig, ax1 = plt.subplots(figsize=(9, 7), layout='constrained')
    fig.canvas.manager.set_window_title('Fitness Chart')

    ax1.set_title(student.name)
    ax1.set_xlabel(
        'Percentile Ranking Across {grade} Grade {gender}s\n'
        'Cohort Size: {cohort_size}'.format(
            grade=to_ordinal(student.grade),
            gender=student.gender.title(),
            cohort_size=cohort_size))

    test_names = list(scores_by_test.keys())
    percentiles = [score.percentile for score in scores_by_test.values()]

    rects = ax1.barh(test_names, percentiles, align='center', height=0.5)

    large_percentiles = [to_ordinal(p) if p > 40 else '' for p in percentiles]
    small_percentiles = [to_ordinal(p) if p <= 40 else '' for p in percentiles]
    ax1.bar_label(rects, small_percentiles,
                  padding=5, color='black', fontweight='bold')
    ax1.bar_label(rects, large_percentiles,
                  padding=-32, color='white', fontweight='bold')

    ax1.set_xlim([0, 100])
    ax1.set_xticks([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
    ax1.xaxis.grid(True, linestyle='--', which='major',
                   color='grey', alpha=.25)
    ax1.axvline(50, color='grey', alpha=0.25)

    ax2 = ax1.twinx()
    ax2.set_ylim(ax1.get_ylim())
    ax2.set_yticks(
        np.arange(len(scores_by_test)),
        labels=[format_score(score) for score in scores_by_test.values()])

    ax2.set_ylabel('Test Scores')

プロット用のデータの定義

先ほど定義した名前付きタプルを使用して、プロット用のデータを定義します。Johnny Doe の Student タプルと、各テストの Score タプルの辞書を作成します。

student = Student(name='Johnny Doe', grade=2, gender='Boy')
scores_by_test = {
    'Pacer Test': Score(7, 'laps', percentile=37),
    'Flexed Arm\n Hang': Score(48, 'sec', percentile=95),
    'Mile Run': Score('12:52', 'min:sec', percentile=73),
    'Agility': Score(17, 'sec', percentile=60),
    'Push Ups': Score(14, '', percentile=16),
}

データのプロット

生徒のデータ、テストごとのスコア、およびコホートのサイズを引数として plot_student_results 関数を呼び出し、その後 plt.show() を呼び出してプロットを表示します。

plot_student_results(student, scores_by_test, cohort_size=62)
plt.show()

まとめ

この実験では、Python の Matplotlib ライブラリを使用して水平棒グラフを作成する方法を学びました。体育の教師が保護者に、他の子供たちと比較して自分の子供がいくつかのフィットネステストでどのような結果を出したかを示したいという例を用いました。名前付きタプルを使用してデータを定義し、整数を序数の文字列に変換するヘルパー関数と、右側の y 軸のスコアラベルを作成するヘルパー関数を定義しました。生徒の学年と性別のコホートと比較した各テストのパーセンタイルランキングの水平棒グラフを作成するプロット関数を定義しました。その後、データを使ってプロット関数を呼び出し、プロットを表示しました。