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