py matplotlib 多个figure同时画多个图以及多个图例多个折线图

图例负号乱码的问题

import numpy as np  
import matplotlib.pyplot as plt
import  matplotlib
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号

需要读取数据模板文件,格式如下,首先是后续要显示的名称,然后是成本价、净值,使用【/】分隔

建信50 | 0.8502/0.8499/0.8496    | 0.8263/0.8239/0.8253 | 0.0260/0.0246
富国中正红利 | 1.1273/1.1270/1.1263   | 1.1030/1.1040/1.0960 | 0.0230/0.0310
景顺中正500  | 0.9798/0.9792/0.9780   | 0.9271/0.9250/0.9149 | 0.0542/0.0643
前海开源人工智能 | 1.0665/1.0653/1.0625 |0.9930/0.9890/0.9870 | 0.0763
易方达创业板 | 1.6463/1.6452/1.6434 | 1.5368/1.5377/1.5233 | 0.1075
南方价值混合C|1.0843/1.0837/1.0819 | 1.0520/1.0520/1.0490 | 0.0317
# -*- coding:utf-8 -*-
import numpy as np  
import matplotlib.pyplot as plt
import  matplotlib

font = {
    'family' : 'SimHei'
}
matplotlib.rc('font', **font)

["006539","NFYXJZHHC","南方优选价值混合C","混合型","NANFANGYOUXUANJIAZHIHUNHEC"]
["100032","FGZZHLZSZQ","富国中证红利指数增强","股票指数","FUGUOZHONGZHENGHONGLIZHISHUZENGQIANG"]
["003318","JSZZ500HYZXDBD","景顺中证500行业中性低波动","股票指数","JINGSHUNZHONGZHENG500HANGYEZHONGXINGDIBODONG"]
["001986","QHKYRGZNZTHH","前海开源人工智能主题混合","混合型","QIANHAIKAIYUANRENGONGZHINENGZHUTIHUNHE"]
["004744","YFDCYBETFLJC","易方达创业板ETF联接C","联接基金","YIFANGDACHUANGYEBANETFLIANJIEC"]
["165312","JXYS50","建信央视50","股票指数","JIANXINYANGSHI50"]
i=1
index=321
subIndex=321
def calc(value):
    global index
    day="6-1,6-2,6-3".split(',')
    #v="建信50 | 0.8502/0.8499/0.8496    | 0.8263/0.8239/0.8253 | 0.0260/0.0246".split('|')
    v=value.split('|')
    name=v[0]
    myValue=[]
    for m in v[1].split('/'):
        myValue.append(float(m))
    curValue=[]
    for m in v[2].split('/'):
        curValue.append(float(m))
    z=[]
    #整理差值数据
    for i in range(len(day)):
        z.append((myValue[i]-curValue[i])*10)
    
    plt.figure(1)
    plt.subplot(index)
    plt.plot(day, myValue, color = 'blue', linewidth = 2.0, linestyle = '-',label="持仓成本价")
    plt.plot(day, curValue, color = 'red', linewidth = 2.0, linestyle = '--',label="当前净值")
    plt.legend()  #显示上面的label
    plt.title(name) #添加标题

    plt.figure(2)
    plt.subplot(index)
    plt.plot(day, z, color = 'red', linewidth = 2.0, linestyle = '-',label="差值")
    plt.legend()  #显示上面的label
    plt.title(name+"差值") #添加标题
    index=index+1
    return z

def sub(name,value):
    plt.figure(1)
    plt.subplot(index)
    index=index+1

    plt.plot(day, value, color = 'red', linewidth = 2.0, linestyle = '-',label="差值")
    plt.legend()  #显示上面的label
    plt.title(name+"差值") #添加标题
    
with open("./a.txt",'r', encoding='UTF-8') as file:
    for line in file:
        z=calc(line)
        name=(line.split('|'))[0]
        #print(name)
plt.show()

 

posted @ 2019-06-04 17:40 Hey,Coder! 阅读(...) 评论(...) 编辑 收藏