依据csv文件绘制折线图:
# -*- coding: UTF-8 -*- from matplotlib.font_manager import FontProperties # 导入FontProperties font = FontProperties(fname="SimHei.ttf", size=14) # 设置黑体字 import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl from datetime import datetime #import pandas as pd #matplotlib其实是不支持显示中文的 显示中文需要一行代码设置字体 mpl.rcParams['font.family'] = 'SimHei' #步骤一设置字体为黑体 plt.rcParams['axes.unicode_minus'] = False #步骤二(解决坐标轴负数的负号显示问题) def process_csv(csv_path): time = [] r_await = [] w_await = [] index = 0 with open(csv_path,'r') as f: lines = f.readlines() for line in lines: index = index + 1; data=line.replace('\n','').split(',') #print(data[0],data[1]) if index != 1: time.append(datetime.strptime(data[0],'%m/%d/%Y %I:%M:%S %p')) #07/27/2022 07:57:13 PM r_await.append(float(data[1])) w_await.append(float(data[2])) xpoints = np.array(time) ypoints = np.array(r_await) x = np.array(time) y = np.array(w_await) ##绘制折线 plt.plot(time,ypoints,color="blue",label="r_await") plt.plot(time,y,color="red",label="w_await") ## x y 轴标签,字体大小 plt.xlabel("time(s)", fontsize=13) plt.ylabel("await(ms)", fontsize=13) #设置x轴方向显示网格线, axis=both,x,y,分别表示两个方向,x 轴方向或 y 轴方向 plt.grid(axis='y',color = 'black', linestyle = '--', linewidth = 0.5) #显示图例 plt.legend(loc='upper left') #标题 plt.title("node-3 缓存盘(sde)") #保存图片展示show, dpi=200表示像素,bbox_inches=tight表示把空白区域去掉 #plt.savefig("test.png", dpi=200) plt.savefig("test.svg", bbox_inches='tight') plt.show() def main(): process_csv("node-100.sde.iostat.csv") if __name__ == "__main__": main()