matplotlib绘制多条动态曲线并保存

# -*- coding: utf-8 -*-

import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import gif

plt.ion()  # 开启交互模式
plt.figure

file = 'result.xlsx'    # 数据来源文件路径
matplotlib.rc("font", family='Microsoft YaHei')  # 图显示中文字体

fs = []
index_list = []
result_list0 = []
result_list1 = []
result_list2 = []
result_list3 = []
result_list4 = []
result_list5 = []
result_list6 = []
result_list7 = []

y0 = pd.read_excel(file, header=None, sheet_name="mAP", usecols=[0])
y1 = pd.read_excel(file, header=None, sheet_name="mAP", usecols=[1])
y2 = pd.read_excel(file, header=None, sheet_name="mAP", usecols=[2])
y3 = pd.read_excel(file, header=None, sheet_name="mAP", usecols=[3])
y4 = pd.read_excel(file, header=None, sheet_name="mAP", usecols=[4])
y5 = pd.read_excel(file, header=None, sheet_name="mAP", usecols=[5])
y6 = pd.read_excel(file, header=None, sheet_name="mAP", usecols=[6])



@gif.frame #@gif.frame是GIF库用来创建帧序列的装饰器
def plott(x):
    plt.title('mAP',fontsize=16)
    plt.xlabel('Epoch', fontsize=10)
    plt.plot(x, result_list0, "#A0522D", marker='o', markersize=3, label=y0[0][0])
    plt.plot(x, result_list1, '#2E8B57', marker='o', markersize=3, label=y1[1][0])
    plt.plot(x, result_list2, '#FF6347', marker='o', markersize=3, label=y2[2][0])
    plt.plot(x, result_list3, '#B8860B', marker='o', markersize=3, label=y3[3][0])
    plt.plot(x, result_list4, '#0000FF', marker='o', markersize=3, label=y4[4][0])
    plt.plot(x, result_list5, '#00BFFF', marker='o', markersize=3, label=y5[5][0])
    plt.plot(x, result_list6, '#9400D3', marker='o', markersize=3, label=y6[6][0])


for i in range(1, 121):
    plt.clf() #清空画布上的所有内容
    index_list.append(i)
    result_list0.append(y0[0][i])
    result_list1.append(y1[1][i])
    result_list2.append(y2[2][i])
    result_list3.append(y3[3][i])
    result_list4.append(y4[4][i])
    result_list5.append(y5[5][i])
    result_list6.append(y6[6][i])
    #result_list7.append(y7[7][i])
    f = plott(index_list)
    #plt.pause(0.1)
    fs.append(f)

gif.save(fs, 'dynamicMAP.gif', duration=3.5)

 

你可能感兴趣的:(matplotlib)