import warnings warnings.filterwarnings("ignore") import numpy as np import pandas as pd import tushare as ts from datetime import datetime, timedelta today = datetime.now().strftime('%Y-%m-%d') before = (datetime.now() - timedelta(days = 240)).strftime('%Y-%m-%d') #120自然日,大概80个交易日 tsla_df = ts.get_k_data("000063", start = before, end = today) #获取行情数据,格式: pandas.core.frame.DataFrame goog_df = ts.get_k_data("600036", start = before, end = today) appl_df = ts.get_k_data("000001", start = before, end = today)
def plot_two_stock(tsla, goog, appl, axs=None): # 如果有传递子画布,使用子画布,否则plt drawer = plt if axs is None else axs drawer.plot(tsla, c='r') drawer.plot(goog, c='g') drawer.plot(appl, c='b') drawer.grid(True) # 显示网格 drawer.legend(['tsla', 'google','apple'], loc='best') # 图例标注 plot_two_stock(tsla_df.close, goog_df.close, appl_df.close) plt.title('TSLA and Google CLOSE') plt.xlabel('time') # x轴时间 plt.ylabel('close') # y轴收盘价格