您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Python中matplotlib以日期為x軸作圖的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
效果圖如下
代碼如下
from datetime import datetime, date, timedelta import matplotlib.pyplot as plt import tushare as ts plt.rcParams['font.sans-serif'] = ['SimHei'] #顯示中文 plt.rcParams['axes.unicode_minus']=False #用來正常顯示負號 def get_riqi(): count = 30 i = 0 today = date.today() #今天日期 riqi_list=[] riqi_list.append(str(today)) while(i<=count): i+=1 riqi_list.append(str((today + timedelta(days=-i)))) return riqi_list def count(Data): data_list = [] sum_list = [] for data in Data: sum = 0 list = [] for jiage in data: sum+=jiage list.append(jiage) sum_list.append(round(sum,2)) data_list.append(list) return sum_list,data_list def get_juedui(Data): sum = 0 data = [] for i in Data[::-1]: sum += i data.append(sum) return data technology = { '海康威視':'002415', '中興通訊':'000063', '科大訊飛':'002230', '立訊精密':'002475', '藍思科技':'300433', '歌爾股份':'002241' } store = [technology] technology = [] def get_data(store,riqi_list): count = 0 for Stock_pool in store: count +=1 for stock in Stock_pool.values(): data = ts.get_hist_data(stock,start=riqi_list[-1]) if (count == 1): technology.append(data.loc[:, 'p_change']) riqi_list = get_riqi() get_data(store,riqi_list) riqi_list = technology[0].index rili = [] for riqi in riqi_list: rili.append(riqi[5:]) sum2,data2 = count(technology) riqi_list = [datetime.strptime(d, '%m-%d').date() for d in rili] count = 0 for i in data2: count+=1 data = get_juedui(i) if(count==1): plt.plot(rili[::-1], data,label = '海康威視' ) if(count==2): plt.plot(rili[::-1], data,label = '中興通訊' ) if(count==3): plt.plot(rili[::-1], data,label = '科大訊飛' ) if(count==4): plt.plot(rili[::-1], data,label = '立訊精密' ) if (count == 5): plt.plot(rili[::-1], data, label='藍思科技') if (count == 6): plt.plot(rili[::-1], data, label='歌爾股份') plt.legend() plt.show()
關于“Python中matplotlib以日期為x軸作圖的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。