Python Matplotlibでテストの点数別人数の度数分布図(直方图)を作成する
このタスクでは、テストの点数別の人数の度数分布図(直方图)を作成する必要があります。以下にスクリプトの例を示します。
import matplotlib.pyplot as plt
import numpy as np
# テストの点数データ(仮のデータ)
scores = [0, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10]
# 点数別の人数をカウント
hist, bins = np.histogram(scores, bins=np.arange(11))
# 直方図を描画
plt.bar(bins[:-1], hist)
# 点数別の人数を表示
for i, h in enumerate(hist):
plt.text(bins[i], h, str(h))
# グラフの設定
plt.xlabel('Score')
plt.ylabel('Frequency')
plt.title('Histogram of Test Scores')
# グラフの表示
plt.show()
このスクリプトでは、まずテストの点数データを準備します(仮のデータを使用しています)。次に、np.histogram関数を使用して、点数別の人数をカウントします。最後に、plt.bar関数を使用して直方図を描画し、plt.text関数を使用して点数別の人数を表示します。その後、グラフの設定を行い、plt.show関数でグラフを表示します。
このスクリプトを実行すると、直方図が表示されます。スクリプトの実行結果や直方図のスクリーンショットを提出してください。
原文地址: http://www.cveoy.top/t/topic/fTVJ 著作权归作者所有。请勿转载和采集!