py matplotlib 多個figure同時畫多個圖以及多個圖例多個折線圖

圖例負號亂碼的問題app

import numpy as np  
import matplotlib.pyplot as plt
import  matplotlib
plt.rcParams['axes.unicode_minus']=False #用來正常顯示負號人工智能

須要讀取數據模板文件,格式以下,首先是後續要顯示的名稱,而後是成本價、淨值,使用【/】分隔spa

建信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()
相關文章
相關標籤/搜索