TQC+ 網頁資料擷取與分析 Python 3 _ 404 成績統計:長條圖 -- 題目的width設定為2,線條太寬,設定0.4時,圖形正常,可能是版本不同所致,請多嘗試

說明:
請讀取read.csv中的資料,再以matplotlib輸出長條圖chart.png,輸出圖表的參數如下:
  • 圖表標題:Score ranges count
  • X軸名稱:Range
  • Y軸名稱:Quantity
  • 標題字型大小:20
  • X軸和Y軸字型大小:14
  • 長條寬度:2
  • X軸刻度:0~19, 20~39, 40~59, 60~79, 80~100
  • Y軸刻度:0到25,間隔5
檔案連結:read.csv (請另存檔案,必須與程式同一資料夾)
範例輸出:
Alt text

程式碼:
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

df = pd.read_csv("read.csv")
scores = df["scores"].values
range_count = [0] * 5

for score in scores:
    if score < 20: range_count[0] += 1
    elif score < 40: range_count[1] += 1
    elif score < 60: range_count[2] += 1
    elif score < 80: range_count[3] += 1
    else: range_count[4] += 1

index = np.arange(0, 25, 5)
labels = ['0~19', '20~39', '40~59', '60~79', '80~100']

plt.bar(labels, range_count, width = 0.4)
plt.xlabel('Range', fontsize=14)
plt.ylabel('Quantity', fontsize=14)
plt.yticks(index)
plt.title('Score ranges count', fontsize=20)

plt.savefig('chart.png')
plt.close()

沒有留言:

張貼留言