python绘图多子图 分别美化

这篇博客介绍了如何使用Python的matplotlib库进行图表绘制,包括简单的折线图和多子图的创建。示例代码展示了如何美化图表,设置坐标轴,以及从CSV文件中读取数据进行拟合和比较。同时,对IPv4和IPv6在不同指标下的性能进行了对比分析,生成了对应的图表并计算了性能差异。
摘要由CSDN通过智能技术生成

 通过一个简单的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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值