python用matplot绘制柱形图数据统计班级的平均分(Python uses matplot to draw the column chart data to count the average score of the class)

导入csv库、matplotlib库

import csv
import matplotlib.pyplot as plt

定义文件路径

path = r”score.csv”

以只读的形式打开文件

stream = open(path, ‘r’)

读取文件内容

data = csv.reader(stream)

将读取的数据存放在list中

list = []
i = 0

使用for循环进行读取

for row in data:
if i != 0:
list.append(row)
i = i + 1

打印数据

“””
for row in list:
print(row)
“””

x = [‘大数据32101’, ‘大数据32102’, ‘大数据32103’]

大数据32101班总分

s01 = 0

大数据32101班人数

d01 = 0

大数据32102班总分

s02 = 0

大数据32102班人数

d02 = 0

大数据32103班总分

s03 = 0

大数据32103班人数

d03 = 0

for row in list:
if row[3] == ‘大数据32101’:
print(row[4])
s01 = s01 + int(row[4])
d01 += 1
elif row[3] == ‘大数据32102’:
print(row[4])
s02 = s02 + int(row[4])
d02 += 1
elif row[3] == ‘大数据32103’:
print(row[4])
s03 = s03 + int(row[4])
d03 += 1
else:
break

算出平均分

p01 = s01 / d01
p02 = s02 / d02
p03 = s03 / d03

num = [p01, p02, p03]
plt.subplot(1, 2, 1)

绘制柱形图(横,纵)数据

plt.bar(x, num)

设置可支持中文

plt.rcParams[‘font.sans-serif’] = [‘SimHei’]

设置横轴、纵轴的标题

plt.xlabel(“班级”)
plt.ylabel(“平均成绩”)

设置总标题

plt.title(“21级成绩表”)

在柱形图显示具体数据–plt.text(柱形图x下标,数据显示位置,数据)

plt.text(0, 85.5, 85.5)

画折线图

plt.subplot(1, 2, 2)
“””
for i in range(3):
plt.text(i, num[i], num[i])
“””
plt.plot(x, num)

显示图标

plt.show()

————————

导入csv库、matplotlib库

import csv
import matplotlib.pyplot as plt

Path definition file

path = r”score.csv”

Open the file as read-only

stream = open(path, ‘r’)

Read file contents

data = csv.reader(stream)

Store the read data in the list

list = []
i = 0

Read using the for loop

for row in data:
if i != 0:
list.append(row)
i = i + 1

print data

“””
for row in list:
print(row)
“””

X = [‘big data 32101’, ‘big data 32102’, ‘big data 32103’]

Total score of big data class 32101

s01 = 0

Number of big data class 32101

d01 = 0

Total score of big data class 32102

s02 = 0

Number of big data class 32102

d02 = 0

Total score of class 32103 of big data

s03 = 0

Number of big data class 32103

d03 = 0

for row in list:
if row[3] == ‘大数据32101’:
print(row[4])
s01 = s01 + int(row[4])
d01 += 1
elif row[3] == ‘大数据32102’:
print(row[4])
s02 = s02 + int(row[4])
d02 += 1
elif row[3] == ‘大数据32103’:
print(row[4])
s03 = s03 + int(row[4])
d03 += 1
else:
break

Calculate the average score

p01 = s01 / d01
p02 = s02 / d02
p03 = s03 / d03

num = [p01, p02, p03]
plt.subplot(1, 2, 1)

Draw column chart (horizontal and vertical) data

plt.bar(x, num)

Set to support Chinese

plt.rcParams[‘font.sans-serif’] = [‘SimHei’]

Set the title of horizontal axis and vertical axis

plt. Xlabel (“class”)
plt. Ylabel (“GPA”)

Set general title

plt. Title (“grade sheet of level 21”)

Display specific data in the column chart — PLT Text (column chart x subscript, data display position, data)

plt. text(0, 85.5, 85.5)

Draw a line chart

plt.subplot(1, 2, 2)
“””
for i in range(3):
plt.text(i, num[i], num[i])
“””
plt.plot(x, num)

Display icon

plt.show()