matplotlib绘图表(Python)
作者:互联网
matplotlib绘图表
1. 柱状图
import csv
import matplotlib.pyplot as plt
subject = ["语文","数学","英语","生物"]
score_first = [75, 90, 70, 77]
score_second = [80, 90, 80, 80]
score_third = [85, 90, 75, 88]
score_fourth = [90, 90, 80, 90]
# 写数据
with open ("test.csv", 'w', encoding="utf8", newline="") as f:
# 基于文件对象构建csv写入对象
csv_write = csv.writer(f)
# 逐行写入
csv_write.writerow(subject)
csv_write.writerow (score_first)
csv_write.writerow (score_second)
csv_write.writerow (score_third)
csv_write.writerow (score_fourth)
# 读数据
with open ("test.csv", 'r', encoding="utf8", newline="") as f:
# 基于文件对象构建读取对象
csv_read = csv.reader(f)
# 逐行读取
data = []
for row in csv_read:
data.append(row)
# 计算各科目平均分
sumlist = []
for i in range (0,4):
sum = 0
for j in range (1, 5):
#print (j,i)
sum = sum + int (data[j][i])
sumlist.append(sum/4)
# 画图
x_axis_data = data[0]
y_axis_data = sumlist
# y_axis_data加标签数据
for x, y in zip(x_axis_data, y_axis_data):
plt.text(x, y+0.5, '%.00f' % y, ha='center', va='bottom', fontsize=10)
# 画柱状图
plt.bar(x_axis_data, y_axis_data, width=0.2)
# 设置y轴范围
plt.ylim([0, 100])
# 设置matplotlib显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
# 设置柱状图标题
plt.title("四次联考平均分", fontsize=20)
# 显示柱状图
plt.show()
2. 折线图
import csv
from turtle import color
import matplotlib.pyplot as plt
import numpy as np
subject = ["语文","数学","英语","生物","物理","化学"]
score_first = [65, 70, 80, 85, 77, 65]
score_second = [70, 75, 85, 90, 84, 94]
score_third = [80, 60, 75, 88, 67, 55]
score_fourth = [90, 65, 77, 95, 63, 88]
# 写数据
with open ("test.csv", 'w', encoding="utf8", newline="") as f:
# 基于文件对象构建csv写入对象
csv_write = csv.writer(f)
# 逐行写入
csv_write.writerow(subject)
csv_write.writerow (score_first)
csv_write.writerow (score_second)
csv_write.writerow (score_third)
csv_write.writerow (score_fourth)
# 读数据
with open ("test.csv", 'r', encoding="utf8", newline="") as f:
# 基于文件对象构建读取对象
csv_read = csv.reader(f)
# 逐行读取
data = []
for row in csv_read:
data.append(row)
# 已知数据
x_axis_data = data[0]
y_axis_data1 = [int(x) for x in data[1]]
y_axis_data2 = [int(x) for x in data[2]]
y_axis_data3 = [int(x) for x in data[3]]
y_axis_data4 = [int(x) for x in data[4]]
# 设置图片大小
plt.figure (figsize=(12, 8), dpi=90)
# 设置matplotlib显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
# 画折线图
plt.plot(x_axis_data, y_axis_data1, color='r', linestyle='--', label="第一次联考")
plt.scatter (x_axis_data, y_axis_data1, color='r')
plt.plot(x_axis_data, y_axis_data2, color='b', linestyle='--', label="第二次联考")
plt.scatter (x_axis_data, y_axis_data2, color='b')
plt.plot(x_axis_data, y_axis_data3, color='g', linestyle='--', label="第三次联考")
plt.scatter (x_axis_data, y_axis_data3, color='g')
plt.plot(x_axis_data, y_axis_data4, color='y', linestyle='--', label="第四次联考")
plt.scatter (x_axis_data, y_axis_data4, color='y')
# plt.plot(x_axis_data, y_axis_data2, "rs--", alphas=0.5, linewidth=1, label="second")
# plt.plot(x_axis_data, y_axis_data3, "go--", alphas=0.5, linewidth=1, label="third")
# plt.plot(x_axis_data, y_axis_data4, "cc--", alphas=0.5, linewidth=1, label="fourth")
# 设置y轴范围
plt.ylim(0, 100)
# 设置图例字体大小
plt.legend(fontsize=15)
# 显示网格
plt.grid (alpha=0.2)
# 设置柱状图标题
plt.title("四次联考平均分", fontsize=20)
# 显示柱状图
plt.show()
标签:plt,Python,matplotlib,write,score,绘图,csv,data,axis 来源: https://www.cnblogs.com/caojun97/p/16244370.html