404_成績統計:長條圖

題目說明:

請開啟PYD04.py檔案,依下列題意進行作答,使輸出值符合題意要求。作答完成請另存新檔為PYA04.py再進行評分。

設計說明:

請讀取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的內容

輸出說明

輸出chart.png圖檔

輸入輸出範例:

範例輸入_1

範例輸出_1

1637834506569

 

  • 注意:
    matplotlib套件的版本,會造成輸出的圖檔有差異,但不影響評分的準確性。
    Code Judger平台會將您的程式,於伺服器中運行輸出圖檔進行評分。

 

待編修檔案:

# --開始--批改評分使用,請勿變動
import matplotlib as mpl
mpl.use('Agg')
# --結束--批改評分使用,請勿變動

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

# 讀取學生分數資料
# 讀取 read.csv
df = ___(___)
scores = df["___"].values

# range_count[0]: range0~19
# range_count[1]: range20~39
# range_count[2]: range40~59
# range_count[3]: range60~79
# range_count[4]: range80~100
# 以0初始化計數串列
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

# y軸標籤
index = np.arange(___, ___, ___)
# X軸刻度
labels = [___, ___, '40~59', ___, '80~100']
# 畫出長條圖
plt.bar(___, range_count, ___)
# 設定X軸名稱
plt.xlabel('___', fontsize=___)
# 設定Y軸名稱
plt.ylabel('___', fontsize=___)
# 設定x軸標籤
plt.xticks(index, labels)
# 設定y軸標籤
plt.yticks(index)
# 設定圖名稱
plt.title('___', fontsize=___)
# 輸出圖片檔案
plt.___('___')
plt.close()

程式碼_1:

# --開始--批改評分使用,請勿變動
import matplotlib as mpl
mpl.use('Agg')
# --結束--批改評分使用,請勿變動

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

# 讀取學生分數資料
# 讀取 read.csv
df =
pd.read_csv("read.csv")
scores = df["
scores"].values

# range_count[0]: range0~19
# range_count[1]: range20~39
# range_count[2]: range40~59
# range_count[3]: range60~79
# range_count[4]: range80~100
# 以0初始化計數串列
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

# y軸標籤
index = np.arange(
0, 25, 5)
# X軸刻度
labels = [
'0~19', '20~39', '40~59', '60~79', '80~100']
# 畫出長條圖
plt.bar(
index, range_count, width=2)
# 設定X軸名稱
plt.
xlabel('Range', fontsize=14)
# 設定Y軸名稱
plt.ylabel('
Quantity', fontsize=14)
# 設定x軸標籤
plt.xticks(index, labels)
# 設定y軸標籤
plt.yticks(index)
# 設定圖名稱
plt.title('
Score ranges count', fontsize=20)
# 輸出圖片檔案
plt.
savefig('chart.png')
plt.close()

程式碼_2:

# --開始--批改評分使用,請勿變動
import matplotlib as mpl
mpl.use('Agg')
# --結束--批改評分使用,請勿變動

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

# 讀取學生分數資料
# 讀取 read.csv
df =
pd.read_csv("read.csv")
scores = df["
scores"].values

# range_count[0]: range0~19
# range_count[1]: range20~39
# range_count[2]: range40~59
# range_count[3]: range60~79
# range_count[4]: range80~100
# 以0初始化計數串列
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

# y軸標籤
index = np.arange(
0, 25, 5)
# X軸刻度
labels = [
'0~19', '20~39', '40~59', '60~79', '80~100']
# 畫出長條圖

plt.bar(index, range_count, width=2.0)
# 設定X軸名稱
plt.xlabel('Range', fontsize=14)
# 設定Y軸名稱
plt.ylabel('Quantity', fontsize=14)
# 設定x軸標籤
plt.xticks(index, labels)
# 設定y軸標籤
plt.yticks(index)
# 設定圖名稱
plt.title('Score ranges count', fontsize=20)
# 輸出圖片檔案
plt.savefig('chart.png')
plt.close()

程式碼_3:

# --開始--批改評分使用,請勿變動
import matplotlib as mpl
mpl.use('Agg')
# --結束--批改評分使用,請勿變動

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

# 讀取學生分數資料
# 讀取 read.csv

df = pd.read_csv("./read.csv")
scores = df["
scores"].values

# range_count[0]: range0~19
# range_count[1]: range20~39
# range_count[2]: range40~59
# range_count[3]: range60~79
# range_count[4]: range80~100
# 以0初始化計數串列
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

# y軸標籤
index = np.arange(0, 25, 5)
# X軸刻度
labels = ['0~19', '20~39', '40~59', '60~79', '80~100']
# y軸標籤
index = np.arange(
0, 25, 5)
# X軸刻度
labels = [
'0~19', '20~39', '40~59', '60~79', '80~100']
# 畫出長條圖

plt.bar(index, range_count, width=2)
# 設定X軸名稱
plt.xlabel('Range', fontsize=14)
# 設定Y軸名稱
plt.ylabel('Quantity', fontsize=14)
# 設定x軸標籤
plt.xticks(index, labels)
# 設定y軸標籤
plt.yticks(index)
# 設定圖名稱
plt.title('Score ranges count', fontsize=20)
# 輸出圖片檔案
plt.savefig('chart.png')
plt.close()

 


 搭 配 學 習 

  • Python 3.x 網頁資料擷取與分析特訓教材風_碁峰

    博客來      誠品

  • TQC+ Python 3.x網頁資料擷取與分析特訓教材_全華

    博客來      誠品

 

arrow
arrow
    創作者介紹
    創作者 DING BANG 的頭像
    DING BANG

    DING BANG的部落格

    DING BANG 發表在 痞客邦 留言(0) 人氣()