matplotlib画图

创建日期: 2023-03-29 19:24 | 作者: 风波 | 浏览次数: 15 | 分类: Python

参考: - https://blog.csdn.net/u014779536/article/details/112284375 - https://stackoverflow.com/questions/4325733/save-a-subplot-in-matplotlib

画多个图

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,2*np.pi,20)
y1 = np.sin(x)
y2 = np.cos(x)
# 设置颜⾊,线型,点型
plt.plot(x,y1,color = 'indigo',ls = '-.',marker = 'p')
plt.plot(x,y2,color = '#FF00EE',ls = '--',marker = 'o')
plt.plot(x,y1 + y2,color = (0.2,0.7,0.2),marker = '*',ls = ':')
plt.plot(x,y1 + 2*y2,linewidth = 3,alpha = 0.7,color = 'orange') # 线宽、透明度
plt.plot(x,2*y1 - y2,'bo--') # 参数连⽤

https://img-blog.csdnimg.cn/img_convert/a766e9de87e8a4c1a8680ba146ab1c63.png

保存图片

    fig, ax = plt.subplots()
    ax.plot(Xs, Y2, label='qps')
    ax.plot(Xs, Y1, label='gpu', alpha=0.3)
    ax.plot(Xs, Y3, label='avg-gpu')
    ax.legend()
    fig.savefig('abc.png')

画线并保存图片

import numpy as np
import matplotlib.pyplot as plt


def read_xy_ok(filename):
    xx = dict()
    #with open('video-count.txt', 'r') as f:
    with open(filename, 'r') as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            blks = line.split()
            xe = int(blks[0])
            ye = int(blks[1])
            xx[xe] = ye

    xs = sorted(list(xx.keys()))

    x = [0] * len(xs)
    y = [0] * len(xs)
    for i in range(len(xs)):
        x[i] = xs[i]
        y[i] = xx[x[i]]
    return x, y


def read_xy(filename):
    x = list()
    y = list()
    #with open('video-count.txt', 'r') as f:
    with open(filename, 'r') as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            blks = line.split()
            xe = int(blks[0])
            ye = int(blks[1])
            x.append(xe)
            y.append(ye)
    return x, y


x1, y1 = read_xy_ok("count-task-srv01/video-count.txt")
x2, y2 = read_xy_ok("count-task-srv02/video-count.txt")
x3, y3 = read_xy_ok("count-task-srv03/video-count.txt")
x4, y4 = read_xy_ok("count-task-srv04/video-count.txt")
x6, y6 = read_xy_ok("count-task-srv06/video-count.txt")


plt.rcParams['figure.figsize']=(12.8, 7.2)

fig, ax = plt.subplots()

#x = np.linspace(0,2*np.pi,20)
#y1 = np.sin(x)
#y2 = np.cos(x)

## 设置颜⾊,线型,点型
#ax.plot(x,y1,color = 'indigo',ls = '-.',marker = 'p')
#ax.plot(x,y2,color = '#FF00EE',ls = '--',marker = 'o')
#ax.plot(x,y1 + y2,color = (0.2,0.7,0.2),marker = '*',ls = ':')
#ax.plot(x,y1 + 2*y2,linewidth = 3,alpha = 0.7,color = 'orange') # 线宽、透明度
#ax.plot(x,2*y1 - y2,'bo--') # 参数连⽤

ax.plot(x1, y1, linewidth=1, label='srv01') # 参数连⽤
ax.plot(x2, y2, linewidth=1, label='srv02') # 参数连⽤
ax.plot(x3, y3, linewidth=1, label='srv03') # 参数连⽤
ax.plot(x4, y4, linewidth=1, label='srv04') # 参数连⽤
ax.plot(x6, y6, linewidth=1, label='srv06') # 参数连⽤

ax.legend()
fig.savefig('abc.png')
15 浏览
14 爬虫
0 评论