Python根据csv表格的数据画趋势图或者实时展现折线图
表格内容为
时间 | 内存占用量 |
---|---|
2023.4.25 10:20 | 800mb |
… | … |
2023.4.25 15:35 | 750mb |
封装两个方法:
1、实时绘图监视系统获取内存占用信息
2、根据统计结果绘制图片并保存
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def current_draw(csv_path):
# 实时绘图
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title(u"Memory Usage", size=20)
ax.legend(loc='upper left')
plt.xlabel(u'time', size=15)
plt.ylabel(u'memory(Mb)', size=15)
plt.xticks(rotation=60, size=8)
while True:
time = []
virtual_memory = []
real_memory = []
share_memory = []
# 读表
with open(csv_path, 'rb') as csvfile:
reader = pd.read_csv(csvfile)
for index, row in reader.iterrows():
time.append(row.iloc[1])
# 去掉单位
real_memory.append(float(row.iloc[3].strip('M')))
h_time = []
for i in time:
h_time.append(i.split(' ')[-1])
# 物理内存
ax.plot(h_time, real_memory, 'o-', label='real_memory', color='blue')
plt.pause(1)
def main(csv_path, png_path):
# 绘图
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title(u"Memory Usage", size=20)
ax.legend(loc='upper left')
plt.xlabel(u'time', size=15)
plt.ylabel(u'memory(Mb)', size=15)
plt.xticks(rotation=60, size=8)
time = []
virtual_memory = []
real_memory = []
share_memory = []
# 读表
with open(csv_path, 'rb') as csvfile:
reader = pd.read_csv(csvfile)
for index, row in reader.iterrows():
time.append(row.iloc[1])
# 去掉单位
real_memory.append(float(row.iloc[3].strip('M')))
h_time = []
for i in time:
h_time.append(i.split(' ')[-1])
# 物理内存
ax.plot(h_time, real_memory, 'o-', label='real_memory', color='blue')
plt.savefig(png_path)
plt.show()
if __name__ == '__main__':
# log文件路径
csv_file = './mem_log.csv'
# 保存图片路径
png_file = './mem_log.png'
main(csv_file, png_file)