通过一个简单的demo了解绘图:
#print(matplotlib.matplotlib_fname())
#print(matplotlib.get_cachedir())
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
#美化
fig, ax = plt.subplots()
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.grid()
#数据
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
c, s = np.cos(x), np.sin(x)
#绘图
plt.plot(x, c, label="余弦", color='#33CCCC')
plt.plot(x, s, label="正弦", color='#FF3366')
#标签
legend(loc='upper left')
#保存
plt.savefig("helloworld.png")
多子图:
import numpy as np
import matplotlib.pyplot as plt
#正确显示中文
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
#获取数据
type_list = ["每个用户请求数", "吞吐率", "用户请求等待时间ms", "请求平均完成时间ms", "网络传输速率",
"服务器响应连接时间ms", "服务器响应处理时间ms", "服务器响应等待时间ms", "服务器响应总耗时ms", "完成90%请求所需时间ms"]
#文件名
ipv444ConFile = "https_ipv4_con_0.txt"
ipv666ConFile = "https_ipv6_con_0.txt"
ipv444PeoFile = "https_ipv4_peo_0.txt"
ipv666PeoFile = "https_ipv6_peo_0.txt"
#解析csv文件为矩阵
def getCscMat(fileName):
outPut = np.loadtxt(open(fileName, "rb"), delimiter=",", skiprows=0)
return outPut
def getMean(varHigh, varLow):
meanSum = sum((varHigh - varLow) / varLow) / len(varLow)
meanSum = meanSum * 100
return format(meanSum, '.2f')
if __name__ == "__main__":
ipv444ConMat = getCscMat(ipv444ConFile)
ipv666ConMat = getCscMat(ipv666ConFile)
ipv444PeoMat = getCscMat(ipv444PeoFile)
ipv666PeoMat = getCscMat(ipv666PeoFile)
#横坐标
xConCoor = ipv444ConMat[:,0]
xPeoCoor = ipv444PeoMat[:,0]
#拟合阶数
order = 10
#循环
for typeIindex in range(1,10):
#拟合函数
ipv444ConLd = np.poly1d(np.polyfit(xConCoor, ipv444ConMat[:,typeIindex], order))
ipv666ConLd = np.poly1d(np.polyfit(xConCoor, ipv666ConMat[:,typeIindex], order))
ipv444PeoLd = np.poly1d(np.polyfit(xPeoCoor, ipv444PeoMat[:,typeIindex], order))
ipv666PeoLd = np.poly1d(np.polyfit(xPeoCoor, ipv666PeoMat[:,typeIindex], order))
#设定纵横比
fig = plt.figure(figsize=(16, 7))
#获取当前Figure
fig = plt.gcf()
#在画布上创建子图
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)
#美化
ax1.spines['right'].set_visible(False)
ax1.spines['top'].set_visible(False)
ax1.grid()
#绘制折线图1
ax1.plot(xConCoor, ipv444ConLd(xConCoor), color='#33CCCC', label='IPV4')
ax1.plot(xConCoor, ipv666ConLd(xConCoor), color='#FF3366', label='IPV6')
ax1.set_title('固定请求数100 ' + type_list[typeIindex] + " "
+ getMean(ipv666ConMat[:,typeIindex], ipv444ConMat[:,typeIindex]) + "%")
ax1.set_xlabel("用户数")
ax1.set_ylabel(type_list[typeIindex])
#美化
ax2.spines['left'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.grid()
#绘制折线图2
ax2.plot(xPeoCoor, ipv444PeoLd(xPeoCoor), color='#33CCCC', label='IPV4')
ax2.plot(xPeoCoor, ipv666PeoLd(xPeoCoor), color='#FF3366', label='IPV6')
ax2.set_title('固定用户数50 ' + type_list[typeIindex] + " "
+ getMean(ipv666PeoMat[:,typeIindex], ipv444PeoMat[:,typeIindex]) + "%")
ax2.set_xlabel("每个用户请求数")
ax2.set_ylabel(type_list[typeIindex])
#显示或保存
ax1.legend()
ax2.legend()
plt.savefig(type_list[typeIindex]+".png")
#plt.show()