Python Visual Matplotlib-k Line chart

Source: Internet
Author: User
Tags timedelta

Introducing Class Libraries

Import Matplotlib as Mpl Import Tushare as TS Import Matplotlib.pyplot as Plt Import Matplotlib.finance as MPF  from Import RC  from Import Date2num  from Import datetime, Timedelta

Set the time to take the K line

Today = DateTime.Now (). Strftime ('%y-%m-%d'= (DateTime.Now ()-Timedelta (days = ). Strftime ('%y-%m-%d'#120 Nature Day, about 80 trading days

Get Market data

# get market data, format: Pandas.core.frame.DataFrame ' 000063 '  = ts.get_k_data (code, start = before, end = today)#  print (TSLA_DF)

 

  "  The date of the candlestick, The normal YYYY-MM-DD format is not supported to be converted to a unique numeric value using Matplotlib.finance.date2num   Span style= "color: #000000;" >qutotes  = []  for  _, (d, O, C, h, L) Span style= "color: #0000ff;" >in   Enumerate (Zip (tsla_df.date, Tsla_df.open, Tsla_df.close, Tsla_df.high, TS La_df.low): D  = Mpf.date2num (Datetime.strptime (D, " %y-%m-%d    ))  #   date, open, close, highest, lowest constituent tuple object Val  val = (d, O , H, L, c)  #   plus Val joins Qutotes  Qutotes. Append (val) 

Fig, ax = plt.subplots (figsize= (15,5))#Set Picture sizeFig.subplots_adjust (bottom=0.5)#adjust the position of the frame to eliminate white edgesax.xaxis_date ()#the scale of the x-axis is the datePlt.xticks (rotation=45)#set the angle of the date scale rotationPlt.title (Code)#Set Picture titlePlt.xlabel ('Date')#set X axis titlePlt.ylabel (' Price')#Set the y-axis titlePlt.grid (True)#Show GridMPF.CANDLESTICK_OHLC (ax, qutotes, Width=0.6, colorup='g', colordown='R', alpha=1.0) plt.show ()



Python Visual Matplotlib-k Line chart

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.