Python可視化Matplotlib-K線圖

來源:互聯網
上載者:User

標籤:tle   可視化   jpg   資料   share   python   rup   div   顯示   

 

引入類庫

import matplotlib as mplimport tushare as tsimport matplotlib.pyplot as pltimport matplotlib.finance as mpffrom matplotlib import rcfrom matplotlib.pylab import date2numfrom datetime import datetime, timedelta

設定取k線的時間

today = datetime.now().strftime(‘%Y-%m-%d‘)before = (datetime.now() - timedelta(days = 120)).strftime(‘%Y-%m-%d‘) #120自然日,大概80個交易日

擷取行情資料

#擷取行情資料,格式: pandas.core.frame.DataFramecode = ‘000063‘tsla_df = ts.get_k_data(code, start = before, end = today)# print(tsla_df)

 

‘‘‘蠟燭圖的日期,不支援普通的YYYY-MM-DD格式要使用matplotlib.finance.date2num進行轉換為特有的數字值‘‘‘qutotes = []for _, (d, o, c, h, l) in enumerate(        zip(tsla_df.date, tsla_df.open, tsla_df.close, tsla_df.high, tsla_df.low)):    d = mpf.date2num(datetime.strptime(d,‘%Y-%m-%d‘))    # 日期,開盤,收盤,最高,最低組成tuple對象val    val = (d, o, h, l, c)    # 加val加入qutotes    qutotes.append(val)

 

fig, ax = plt.subplots(figsize=(15,5))      #設定圖片大小fig.subplots_adjust(bottom=0.5)             #調整畫框的位置,用來消除白邊ax.xaxis_date()                             # X軸的刻度為日期plt.xticks(rotation=45)                     # 設定日期刻度旋轉的角度plt.title(code)                         # 設定圖片標題plt.xlabel(‘Date‘)                          # 設定X軸標題plt.ylabel(‘Price‘)                         # 設定Y軸標題plt.grid(True)                              # 顯示網格mpf.candlestick_ohlc(ax, qutotes, width=0.6, colorup=‘g‘, colordown=‘r‘, alpha=1.0)plt.show()

 



Python可視化Matplotlib-K線圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.