python 讀取csv 數據並畫圖分析


數據源 : https://pan.baidu.com/s/1eR593Uy    密碼: yqjh

python環境 python3

#encoding: utf-8
import csv
import numpy as np
import matplotlib.pyplot as plt
with open(r"D:\DJIA_table.csv") as c:
    r = csv.reader(c)

    Date,Open,High,Low,Close,Volume,Adj_Close = [],[],[],[],[],[],[]
    index = 0
    for i in  r :
        if(index !=  0 ):
            Date.append(i[0])
            Open.append(i[1])
            High.append(i[2])
            Low.append(i[3])
            Close.append(i[4])
            Volume.append(i[5])
            Adj_Close.append(i[6])
        #print(i)
        index =index+1
    list = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj_Close']
    list1 = ['o', '*', 'v', '-.', '--', 'x']
    lists = {};
    #lists["Date"],lists["Open"],lists["High"],lists["Low"],lists["Close"],lists["Volume"],lists["Adj_Close"] = Date,Open,High,Low,Close,Volume,Adj_Close
    lists["Open"],lists["High"],lists["Low"],lists["Close"],lists["Volume"],lists["Adj_Close"] = Open,High,Low,Close,Volume,Adj_Close
#print(lists)
"""  制圖開始   """
list = [ 'Open', 'High', 'Low', 'Close','Volume', 'Adj_Close']
list1 = ['-', '_', 'v', '-.', ':', ':']
colors = ['r','y','g','b','m','k']
"""開始畫圖"""
fit =plt.figure()
#組裝 legends 對象
legends = {}
for i in range(len(list)):
    legends[list[i]]= list1[i]
print(legends)

x = [x for x in range(len(lists["Open"]))]
for index,t in  enumerate(legends.keys()):#迭代
    #print(index,t,list[index])
    fit.add_subplot("61%s"%(index + 1 ))#subplot 頁面布局
    plt.plot(x,lists[list[index]],legends[t],color = colors[index])#填充數據(1.x軸數據,2,.y軸數據,3.線條,4.顏色)
    plt.legend(t,loc ="upper left" )
plt.show()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM