# -*- coding:utf-8 -*- import random import matplotlib.pyplot as plt from pylab import * import os import pandas as pd import numpy as np import xlsxwriter t_indx= pd.date_range('11/9/2018 00:00:00',periods=100,freq='s',normalize=True) t= [] for i in t_indx: t.append(str(i)) len_num = len(t_indx) print"len: ", len_num cpu=[] mem=[] random1 = np.random.RandomState(100) random2 = np.random.RandomState(50) for j in range(100): cpu.append(round(random1.uniform(0, 400.0),2)) mem.append(round(random2.uniform(0, 100.0),2)) #for k in cpu: # print "CPU: ", k # #for l in mem: # print "MEM: ", l if os.path.isfile("data.txt"): os.remove("data.txt") else: #write to file f = open('data.txt','a') for m in range(100): f.write(t[m][11:]) f.write(' ') f.write(str(cpu[m])) f.write(' ') f.write(str(mem[m])) f.write(' ') f.close() data1=[] data2=[] data3=[] with open('data.txt', 'r') as file_to_read: while True: lines = file_to_read.readline() if not lines: break pass data1.append(lines.split(' ',2)[0]) data2.append(float(lines.split(' ',2)[1])) data3.append(float(lines.split(' ',2)[2])) #for k in data1: # print k #for k in data2: # print k for k in data3: print k data=[data1,data2,data3] mpl.rcParams['font.sans-serif'] = ['SimHei'] times=data1 x = range(len(times)) y = data2 y1 = data3 plt.plot(x, y, marker='o', mec='r', mfc='w',label=u'CPU曲线图') plt.plot(x, y1, marker='*', ms=10,label=u'memory曲线图') plt.legend() # 让图例生效 plt.xticks(x, times, rotation=45) plt.margins(0) plt.subplots_adjust(bottom=0.15) plt.xlabel(u"time(s)") #X轴标签 plt.ylabel("%") #Y轴标签 plt.title("perfomance") #标题 plt.show()
生成的图: