請讀取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
範例輸出:

程式碼:
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()
沒有留言:
張貼留言