def _set_axes(self, xlim, ylim, pot_energy_lim, kin_energy_lim, tot_energy_lim, pressure_lim): axes = [] axes.append(plt.subplot2grid((2, 4), (0, 0), colspan=2, rowspan=2, xlim=xlim, ylim=ylim, aspect="equal")) axes.append(plt.subplot2grid((2, 4), (0, 2), title="Potential Energy", ylim=pot_energy_lim)) axes.append(plt.subplot2grid((2, 4), (1, 2), title="Kinetic Energy", ylim=kin_energy_lim)) axes.append(plt.subplot2grid((2, 4), (0, 3), title="Total Energy", ylim=tot_energy_lim)) axes.append(plt.subplot2grid((2, 4), (1, 3), title="Pressure", ylim=pressure_lim)) for a in axes[1:]: a.grid() a.set_xticks([]) return axes
def old_candles_plot(self, data, slide=-1): data.index.name = 'Date' data = data.rename( columns={ "askopen": "Open", "askclose": "Close", "askhigh": "High", "asklow": "Low", "tickqty": "Volume" }) data['3T'] = data['Close'].rolling(window=3, min_periods=0).mean() data.dropna(inplace=True) df_ohlc = data['Close'].resample('3T').ohlc() # not a moving avg df_volume = data['Volume'].resample('3T').sum() dataPeak = data.copy() dataTrough = data.copy() dataPeak["Open"].where(dataPeak['TP'] == 1, np.nan, inplace=True) dataTrough["Open"].where(dataTrough['TP'] == -1, np.nan, inplace=True) dataPeak.reset_index(inplace=True) dataTrough.reset_index(inplace=True) df_ohlc.reset_index(inplace=True) dataPeak['Date'] = dataPeak['Date'].map(mdates.date2num) dataTrough['Date'] = dataTrough['Date'].map(mdates.date2num) df_ohlc['Date'] = df_ohlc['Date'].map(mdates.date2num) ax1 = plt.subplot2grid((5, 1), (0, 0), rowspan=3, colspan=1) ax2 = plt.subplot2grid((5, 1), (4, 0), rowspan=1, colspan=1, sharex=ax1) ax1.xaxis_date() #display dates as dates candlestick_ohlc(ax1, df_ohlc.values, width=0.001, colorup='g') ax2.fill_between(df_volume.index.map(mdates.date2num), df_volume.values, 0) ax1.plot(data.index, data['3T']) ax1.plot(data.index, data['Open'], color="k", linestyle='dashed') ax1.scatter(dataPeak["Date"].shift(slide), dataPeak["Open"], marker="^") ax1.scatter(dataTrough["Date"].shift(slide), dataTrough["Open"], marker="v") ax1.set_ylabel('Price') ax2.set_ylabel('Volume') ax2.set_xlabel('Date') ax1.set_title('EUR/USD') plt.show()
def show_chan_mpl(code, start_date, end_date, stock_days, resample, show_mpl=True, least_init=3, chanK_flag=False, windows=20): def get_least_khl_num(resample, idx=0, init_num=3): # init = 3 if init_num - idx > 0: initw = init_num - idx else: initw = 0 return init_num if resample == 'd' else initw if resample == 'w' else init_num-idx-1 if init_num-idx-1 >0 else 0\ if resample == 'm' else 5 stock_code = code # 股票代码 # stock_code = '002176' # 股票代码 # start_date = '2017-09-05' # start_date = None # end_date = '2017-10-12 15:00:00' # 最后生成k线日期 # end_date = None # stock_days = 60 # 看几天/分钟前的k线 # resample = 'd' # resample = 'w' x_jizhun = 3 # window 周期 x轴展示的时间距离 5:日,40:30分钟, 48: 5分钟 least_khl_num = get_least_khl_num(resample, init_num=least_init) # stock_frequency = '5m' # 1d日线, 30m 30分钟, 5m 5分钟,1m 1分钟 stock_frequency = resample # 1d日线, 30m 30分钟, 5m 5分钟,1m 1分钟 w:week # chanK_flag = chanK # True 看缠论K线, False 看k线 # chanK_flag = True # True 看缠论K线, False 看k线 show_mpl = show_mpl def con2Cxianduan(stock, k_data, chanK, frsBiType, biIdx, end_date, cur_ji=1, recursion=False, dl=None, chanK_flag=False, least_init=3): max_k_num = 4 if cur_ji >= 6 or len(biIdx) == 0 or recursion: return biIdx idx = biIdx[len(biIdx) - 1] k_data_dts = list(k_data.index) st_data = chanK['enddate'][idx] if st_data not in k_data_dts: return biIdx # 重构次级别线段的点到本级别的chanK中 def refactorXd(biIdx, xdIdxc, chanK, chanKc, cur_ji): new_biIdx = [] biIdxB = biIdx[len(biIdx) - 1] if len(biIdx) > 0 else 0 for xdIdxcn in xdIdxc: for chanKidx in range(len(chanK.index))[biIdxB:]: if judge_day_bao(chanK, chanKidx, chanKc, xdIdxcn, cur_ji): new_biIdx.append(chanKidx) break return new_biIdx # 判断次级别日期是否被包含 def judge_day_bao(chanK, chanKidx, chanKc, xdIdxcn, cur_ji): _end_date = chanK['enddate'][chanKidx] + datetime.timedelta( hours=15) if cur_ji == 1 else chanK['enddate'][chanKidx] _start_date = chanK.index[chanKidx] if chanKidx == 0\ else chanK['enddate'][chanKidx - 1] + datetime.timedelta(minutes=1) return _start_date <= chanKc.index[xdIdxcn] <= _end_date # cur_ji = 1 #当前级别 # 符合k线根数大于4根 1日级别, 2 30分钟, 3 5分钟, 4 一分钟 if not recursion: resample = 'd' if cur_ji + 1 == 2 else '5m' if cur_ji + 1 == 3 else \ 'd' if cur_ji + 1 == 5 else 'w' if cur_ji + 1 == 6 else 'd' least_khl_num = get_least_khl_num(resample, 1, init_num=least_init) print "次级:%s st_data:%s k_data_dts:%s least_khl_num:%s" % ( len(k_data_dts) - k_data_dts.index(st_data), str(st_data)[:10], len(k_data_dts), least_khl_num) if cur_ji + 1 != 2 and len(k_data_dts) - k_data_dts.index( st_data) >= least_khl_num + 1: frequency = '30m' if cur_ji + 1 == 2 else '5m' if cur_ji + 1 == 3 else '1m' # else: # frequency = 'd' if cur_ji+1==2 else '5m' if cur_ji+1==3 else \ # 'd' if cur_ji+1==5 else 'w' if cur_ji+1==6 else 'd' start_lastday = str(chanK.index[biIdx[-1]])[0:10] print "次级别为:%s cur_ji:%s %s" % (resample, cur_ji, start_lastday) # print [chanK.index[x] for x in biIdx] k_data_c, cname = get_quotes_tdx(stock, start=start_lastday, end=end_date, dl=dl, resample=resample) print k_data_c.index[0], k_data_c.index[-1] chanKc = chan.parse2ChanK( k_data_c, k_data_c.values) if chanK_flag else k_data_c fenTypesc, fenIdxc = chan.parse2ChanFen(chanKc, recursion=True) if len(fenTypesc) == 0: return biIdx biIdxc, frsBiTypec = chan.parse2ChanBi( fenTypesc, fenIdxc, chanKc, least_khl_num=least_khl_num - 1) if len(biIdxc) == 0: return biIdx print "biIdxc:", [round(k_data_c.high[x], 2) for x in biIdxc ], [str(k_data_c.index[x])[:10] for x in biIdxc] xdIdxc, xdTypec = chan.parse2Xianduan( biIdxc, chanKc, least_windows=1 if least_khl_num > 0 else 0) biIdxc = con2Cxianduan(stock, k_data_c, chanKc, frsBiTypec, biIdxc, end_date, cur_ji + 1, recursion=True) print "xdIdxc:%s xdTypec:%s biIdxc:%s" % (xdIdxc, xdTypec, biIdxc) if len(xdIdxc) == 0: return biIdx # 连接线段位为上级别的bi lastBiType = frsBiType if len(biIdx) % 2 == 0 else -frsBiType if len(biIdx) == 0: return refactorXd(biIdx, xdIdxc, chanK, chanKc, cur_ji) lastbi = biIdx.pop() firstbic = xdIdxc.pop(0) # 同向连接 if lastBiType == xdTypec: biIdx = biIdx + refactorXd(biIdx, xdIdxc, chanK, chanKc, cur_ji) # 逆向连接 else: # print '开始逆向连接' _mid = [lastbi] if (lastBiType == -1 and chanK['low'][lastbi] <= chanKc['low'][firstbic])\ or (lastBiType == 1 and chanK['high'][lastbi] >= chanKc['high'][firstbic]) else\ [chanKidx for chanKidx in range(len(chanK.index))[biIdx[len(biIdx) - 1]:] if judge_day_bao(chanK, chanKidx, chanKc, firstbic, cur_ji)] biIdx = biIdx + [_mid[0]] + refactorXd(biIdx, xdIdxc, chanK, chanKc, cur_ji) # print "次级:",len(biIdx),biIdx,[str(k_data_c.index[x])[:10] for x in biIdx] return biIdx def get_quotes_tdx(code, start=None, end=None, dl=120, resample='d', show_name=True): quotes = tdd.get_tdx_append_now_df_api( code=stock_code, start=start, end=end, dl=dl).sort_index(ascending=True) if not resample == 'd' and resample in tdd.resample_dtype: quotes = tdd.get_tdx_stock_period_to_type(quotes, period_day=resample) quotes.index = quotes.index.astype('datetime64') if show_name: if 'name' in quotes.columns: cname = quotes.name[0] # cname_g =cname else: dm = tdd.get_sina_data_df(code) if 'name' in dm.columns: cname = dm.name[0] else: cname = '-' else: cname = '-' if quotes is not None and len(quotes) > 0: quotes = quotes.loc[:, [ 'open', 'close', 'high', 'low', 'vol', 'amount' ]] else: # log.error("quotes is None check:%s"%(code)) raise Exception("Code:%s error, df is None%s" % (code)) return quotes, cname quotes, cname = get_quotes_tdx(stock_code, start_date, end_date, dl=stock_days, resample=resample, show_name=show_mpl) # quotes.rename(columns={'amount': 'money'}, inplace=True) # quotes.rename(columns={'vol': 'vol'}, inplace=True) # print quotes[-2:] # print quotes[:1] # 缠论k线 # open close high low volume money # 2017-05-03 15.69 15.66 15.73 15.53 10557743 165075887 # 2017-05-04 15.66 15.63 15.70 15.52 8343270 130330396 # 2017-05-05 15.56 15.65 15.68 15.41 18384031 285966842 # 2017-05-08 15.62 15.75 15.76 15.54 12598891 197310688 quotes = chan.parse2ChanK(quotes, quotes.values) if chanK_flag else quotes # print quotes[:1].index # print quotes[-1:].index quotes[quotes['vol'] == 0] = np.nan quotes = quotes.dropna() Close = quotes['close'] Open = quotes['open'] High = quotes['high'] Low = quotes['low'] T0 = quotes.index.values # T0 = mdates.date2num(T0) length = len(Close) initial_trend = "down" cur_ji = 1 if stock_frequency == 'd' else \ 2 if stock_frequency == '30m' else \ 3 if stock_frequency == '5m' else \ 4 if stock_frequency == 'w' else \ 5 if stock_frequency == 'm' else 6 log.debug('======笔形成最后一段未完成段判断是否是次级别的走势形成笔=======:%s %s' % (stock_frequency, cur_ji)) x_date_list = quotes.index.values.tolist() # for x_date in x_date_list: # d = datetime.datetime.fromtimestamp(x_date/1000000000) # print d.strftime("%Y-%m-%d %H:%M:%S.%f") # print x_date_list k_data = quotes k_values = k_data.values # 缠论k线 chanK = quotes if chanK_flag else chan.parse2ChanK( k_data, k_values, chan_kdf=chanK_flag) fenTypes, fenIdx = chan.parse2ChanFen(chanK) # log.debug("code:%s fenTypes:%s fenIdx:%s k_data:%s" % (stock_code,fenTypes, fenIdx, len(k_data))) biIdx, frsBiType = chan.parse2ChanBi(fenTypes, fenIdx, chanK, least_khl_num=least_khl_num) # log.debug("biIdx1:%s chanK:%s" % (biIdx, len(chanK))) print("biIdx1:%s %s chanK:%s" % (biIdx, str(chanK.index.values[biIdx[-1]])[:10], len(chanK))) biIdx = con2Cxianduan(stock_code, k_data, chanK, frsBiType, biIdx, end_date, cur_ji, least_init=least_init) # log.debug("biIdx2:%s chanK:%s" % (biIdx, len(biIdx))) chanKIdx = [(chanK.index[x]) for x in biIdx] if len(biIdx) == 0 and len(chanKIdx) == 0: print "BiIdx is None and chanKidx is None:%s" % (code) return None log.debug("con2Cxianduan:%s chanK:%s %s" % (biIdx, len(chanK), chanKIdx[-1] if len(chanKIdx) > 0 else None)) # print quotes['close'].apply(lambda x:round(x,2)) # print '股票代码', get_security_info(stock_code).display_name # print '股票代码', (stock_code), resample, least_khl_num # 3.得到分笔结果,计算坐标显示 def plot_fenbi_seq(biIdx, frsBiType, plt=None, color=None): x_fenbi_seq = [] y_fenbi_seq = [] for i in range(len(biIdx)): if biIdx[i] is not None: fenType = -frsBiType if i % 2 == 0 else frsBiType # dt = chanK['enddate'][biIdx[i]] # 缠论k线 dt = chanK.index[biIdx[i]] if chanK_flag else chanK['enddate'][ biIdx[i]] # print i,k_data['high'][dt], k_data['low'][dt] time_long = long( time.mktime( (dt + datetime.timedelta(hours=8)).timetuple()) * 1000000000) # print x_date_list.index(time_long) if time_long in x_date_list else 0 if fenType == 1: if plt is not None: if color is None: plt.text(x_date_list.index(time_long), k_data['high'][dt], str(k_data['high'][dt]), ha='left', fontsize=12) else: col_v = color[0] if fenType > 0 else color[1] plt.text(x_date_list.index(time_long), k_data['high'][dt], str(k_data['high'][dt]), ha='left', fontsize=12, bbox=dict(facecolor=col_v, alpha=0.5)) x_fenbi_seq.append(x_date_list.index(time_long)) y_fenbi_seq.append(k_data['high'][dt]) if fenType == -1: if plt is not None: if color is None: plt.text(x_date_list.index(time_long), k_data['low'][dt], str(k_data['low'][dt]), va='bottom', fontsize=12) else: col_v = color[0] if fenType > 0 else color[1] plt.text(x_date_list.index(time_long), k_data['low'][dt], str(k_data['low'][dt]), va='bottom', fontsize=12, bbox=dict(facecolor=col_v, alpha=0.5)) x_fenbi_seq.append(x_date_list.index(time_long)) y_fenbi_seq.append(k_data['low'][dt]) # bottom_time = None # for k_line_dto in m_line_dto.member_list[::-1]: # if k_line_dto.low == m_line_dto.low: # # get_price返回的日期,默认时间是08:00:00 # bottom_time = k_line_dto.begin_time.strftime('%Y-%m-%d') +' 08:00:00' # break # x_fenbi_seq.append(x_date_list.index(long(time.mktime(datetime.strptime(bottom_time, "%Y-%m-%d %H:%M:%S").timetuple())*1000000000))) # y_fenbi_seq.append(m_line_dto.low) return x_fenbi_seq, y_fenbi_seq # print T0[-len(T0):].astype(dt.date) T1 = T0[-len(T0):].astype(datetime.date) / 1000000000 Ti = [] if len(T0) / x_jizhun > 12: x_jizhun = len(T0) / 12 for i in range(len(T0) / x_jizhun): # print "len(T0)/x_jizhun:",len(T0)/x_jizhun a = i * x_jizhun d = datetime.date.fromtimestamp(T1[a]) # print d T2 = d.strftime('$%Y-%m-%d$') Ti.append(T2) # print tab d1 = datetime.date.fromtimestamp(T1[len(T0) - 1]) d2 = (d1 + datetime.timedelta(days=1)).strftime('$%Y-%m-%d$') Ti.append(d2) ll = Low.min() * 0.97 hh = High.max() * 1.03 # ht = HoverTool(tooltips=[ # ("date", "@date"), # ("open", "@open"), # ("close", "@close"), # ("high", "@high"), # ("low", "@low"), # ("volume", "@volume"), # ("money", "@money"),]) # TOOLS = [ht, WheelZoomTool(dimensions=['width']),\ # ResizeTool(), ResetTool(),\ # PanTool(dimensions=['width']), PreviewSaveTool()] if show_mpl: fig = plt.figure(figsize=(10, 6)) ax1 = plt.subplot2grid((10, 1), (0, 0), rowspan=8, colspan=1) # ax1 = fig.add_subplot(2,1,1) #fig = plt.figure() #ax1 = plt.axes([0,0,3,2]) X = np.array(range(0, length)) pad_nan = X + nan # 计算上 下影线 max_clop = Close.copy() max_clop[Close < Open] = Open[Close < Open] min_clop = Close.copy() min_clop[Close > Open] = Open[Close > Open] # 上影线 line_up = np.array([High, max_clop, pad_nan]) line_up = np.ravel(line_up, 'F') # 下影线 line_down = np.array([Low, min_clop, pad_nan]) line_down = np.ravel(line_down, 'F') # 计算上下影线对应的X坐标 pad_nan = nan + X pad_X = np.array([X, X, X]) pad_X = np.ravel(pad_X, 'F') # 画出实体部分,先画收盘价在上的部分 up_cl = Close.copy() up_cl[Close <= Open] = nan up_op = Open.copy() up_op[Close <= Open] = nan down_cl = Close.copy() down_cl[Open <= Close] = nan down_op = Open.copy() down_op[Open <= Close] = nan even = Close.copy() even[Close != Open] = nan # 画出收红的实体部分 pad_box_up = np.array([up_op, up_op, up_cl, up_cl, pad_nan]) pad_box_up = np.ravel(pad_box_up, 'F') pad_box_down = np.array([down_cl, down_cl, down_op, down_op, pad_nan]) pad_box_down = np.ravel(pad_box_down, 'F') pad_box_even = np.array([even, even, even, even, pad_nan]) pad_box_even = np.ravel(pad_box_even, 'F') # X的nan可以不用与y一一对应 X_left = X - 0.25 X_right = X + 0.25 box_X = np.array([X_left, X_right, X_right, X_left, pad_nan]) # print box_X box_X = np.ravel(box_X, 'F') # print box_X # Close_handle=plt.plot(pad_X,line_up,color='k') vertices_up = np.array([box_X, pad_box_up]).T vertices_down = np.array([box_X, pad_box_down]).T vertices_even = np.array([box_X, pad_box_even]).T handle_box_up = mat.patches.Polygon(vertices_up, color='r', zorder=1) handle_box_down = mat.patches.Polygon(vertices_down, color='g', zorder=1) handle_box_even = mat.patches.Polygon(vertices_even, color='k', zorder=1) ax1.add_patch(handle_box_up) ax1.add_patch(handle_box_down) ax1.add_patch(handle_box_even) handle_line_up = mat.lines.Line2D(pad_X, line_up, color='k', linestyle='solid', zorder=0) handle_line_down = mat.lines.Line2D(pad_X, line_down, color='k', linestyle='solid', zorder=0) ax1.add_line(handle_line_up) ax1.add_line(handle_line_down) v = [0, length, Open.min() - 0.5, Open.max() + 0.5] plt.axis(v) ax1.set_xticks(np.linspace(-2, len(Close) + 2, len(Ti))) ax1.set_ylim(ll, hh) ax1.set_xticklabels(Ti) plt.grid(True) plt.setp(plt.gca().get_xticklabels(), rotation=30, horizontalalignment='right') ''' 以上代码拷贝自https://www.joinquant.com/post/1756 感谢alpha-smart-dog K线图绘制完毕 ''' # print "biIdx:%s chankIdx:%s"%(biIdx,str(chanKIdx[-1])[:10]) if show_mpl: x_fenbi_seq, y_fenbi_seq = plot_fenbi_seq(biIdx, frsBiType, plt) # plot_fenbi_seq(fenIdx,fenTypes[0], plt,color=['red','green']) plot_fenbi_seq(fenIdx, frsBiType, plt, color=['red', 'green']) else: x_fenbi_seq, y_fenbi_seq = plot_fenbi_seq(biIdx, frsBiType, plt=None) plot_fenbi_seq(fenIdx, frsBiType, plt=None, color=['red', 'green']) # 在原图基础上添加分笔蓝线 inx_value = chanK.high.values inx_va = [round(inx_value[x], 2) for x in biIdx] log.debug("inx_va:%s count:%s" % (inx_va, len(quotes.high))) log.debug("yfenbi:%s count:%s" % ([round(y, 2) for y in y_fenbi_seq], len(chanK))) j_BiType = [ -frsBiType if i % 2 == 0 else frsBiType for i in range(len(biIdx)) ] BiType_s = j_BiType[-1] if len(j_BiType) > 0 else -2 # bi_price = [str(chanK.low[idx]) if i % 2 == 0 else str(chanK.high[idx]) for i,idx in enumerate(biIdx)] # print ("笔 :%s %s"%(biIdx,bi_price)) # fen_dt = [str(chanK.index[fenIdx[i]])[:10] if chanK_flag else str(chanK['enddate'][fenIdx[i]])[:10]for i in range(len(fenIdx))] fen_dt = [(chanK.index[fenIdx[i]]) if chanK_flag else (chanK['enddate'][fenIdx[i]]) for i in range(len(fenIdx))] if len(fenTypes) > 0: if fenTypes[0] == -1: # fen_price = [str(k_data.low[idx]) if i % 2 == 0 else str(k_data.high[idx]) for i,idx in enumerate(fen_dt)] low_fen = [idx for i, idx in enumerate(fen_dt) if i % 2 == 0] high_fen = [idx for i, idx in enumerate(fen_dt) if i % 2 <> 0] else: # fen_price = [str(k_data.high[idx]) if i % 2 == 0 else str(k_data.low[idx]) for i,idx in enumerate(fen_dt)] high_fen = [idx for i, idx in enumerate(fen_dt) if i % 2 == 0] low_fen = [idx for i, idx in enumerate(fen_dt) if i % 2 <> 0] # fen_duration =[fenIdx[i] - fenIdx[i -1 ] if i >0 else 0 for i,idx in enumerate(fenIdx)] else: # fen_price = fenTypes # fen_duration = fenTypes low_fen = [] high_fen = [] # fen_dt = [str(k_data.index[idx])[:10] for i,idx in enumerate(fenIdx)] # print low_fen,high_fen def dataframe_mode_round(df): roundlist = [1, 0] df_mode = [] # df.high.cummin().value_counts() for i in roundlist: df_mode = df.apply(lambda x: round(x, i)).mode() if len(df_mode) > 0: break return df_mode kdl = k_data.loc[low_fen].low kdl_mode = dataframe_mode_round(kdl) kdh = k_data.loc[high_fen].high kdh_mode = dataframe_mode_round(kdh) print("kdl:%s" % (kdl.values)) print("kdh:%s" % (kdh.values)) print("kdl_mode:%s kdh_mode%s chanKidx:%s" % (kdl_mode.values, kdh_mode.values, str(chanKIdx[-1])[:10])) lastdf = k_data[k_data.index >= chanKIdx[-1]] if BiType_s == -1: keydf = lastdf[((lastdf.close >= kdl_mode.max()) & (lastdf.low >= kdl_mode.max()))] elif BiType_s == 1: keydf = lastdf[((lastdf.close >= kdh_mode.max()) & (lastdf.high >= kdh_mode.min()))] else: keydf = lastdf[((lastdf.close >= kdh_mode.max()) & (lastdf.high >= kdh_mode.min())) | ((lastdf.close <= kdl_mode.min()) & (lastdf.low <= kdl_mode.min()))] print("BiType_s:%s keydf:%s key:%s" % (BiType_s, None if len(keydf) == 0 else str( keydf.index.values[0])[:10], len(keydf))) # return BiType_s,None if len(keydf) == 0 else str(keydf.index.values[0])[:10],len(keydf) # import ipdb;ipdb.set_trace() log.debug("Fentype:%s " % (fenTypes)) log.debug("fenIdx:%s " % (fenIdx)) # print ("fen_duration:%s "%(fen_duration)) # print ("fen_price:%s "%(fen_price)) # print ("fendt:%s "%(fen_dt)) print("BiType :%s frsBiType:%s" % (j_BiType, frsBiType)) if len(j_BiType) > 0: if j_BiType[0] == -1: tb_price = [ str(quotes.low[idx]) if i % 2 == 0 else str(quotes.high[idx]) for i, idx in enumerate(x_fenbi_seq) ] else: tb_price = [ str(quotes.high[idx]) if i % 2 == 0 else str(quotes.low[idx]) for i, idx in enumerate(x_fenbi_seq) ] tb_duration = [ x_fenbi_seq[i] - x_fenbi_seq[i - 1] if i > 0 else 0 for i, idx in enumerate(x_fenbi_seq) ] else: tb_price = j_BiType tb_duration = j_BiType print "图笔 :", x_fenbi_seq, tb_price print "图笔dura :", tb_duration # 线段画到笔上 xdIdxs, xfenTypes = chan.parse2ChanXD(frsBiType, biIdx, chanK) print '线段', xdIdxs, xfenTypes x_xd_seq = [] y_xd_seq = [] for i in range(len(xdIdxs)): if xdIdxs[i] is not None: fenType = xfenTypes[i] # dt = chanK['enddate'][biIdx[i]] # 缠论k线 dt = chanK.index[xdIdxs[i]] if chanK_flag else chanK['enddate'][ xdIdxs[i]] # print k_data['high'][dt], k_data['low'][dt] time_long = long( time.mktime((dt + datetime.timedelta(hours=8)).timetuple()) * 1000000000) # print x_date_list.index(time_long) if time_long in x_date_list else 0 if fenType == 1: x_xd_seq.append(x_date_list.index(time_long)) y_xd_seq.append(k_data['high'][dt]) if fenType == -1: x_xd_seq.append(x_date_list.index(time_long)) y_xd_seq.append(k_data['low'][dt]) # bottom_time = None # for k_line_dto in m_line_dto.member_list[::-1]: # if k_line_dto.low == m_line_dto.low: # # get_price返回的日期,默认时间是08:00:00 # bottom_time = k_line_dto.begin_time.strftime('%Y-%m-%d') +' 08:00:00' # break # x_fenbi_seq.append(x_date_list.index(long(time.mktime(datetime.strptime(bottom_time, "%Y-%m-%d %H:%M:%S").timetuple())*1000000000))) # y_fenbi_seq.append(m_line_dto.low) # 在原图基础上添加分笔蓝线 print("线段 :%s" % (x_xd_seq)) print("笔值 :%s" % ([str(x) for x in (y_xd_seq)])) # Y_hat = X * b + a if show_mpl: plt.plot(x_fenbi_seq, y_fenbi_seq) plt.legend([stock_code, cname], loc=0) plt.title(stock_code + " | " + cname + " | " + str(quotes.index[-1])[:10], fontsize=14) plt.plot(x_xd_seq, y_xd_seq) if len(quotes) > windows: roll_mean = pd.rolling_mean(quotes.close, window=windows) plt.plot(roll_mean, 'r') zp = zoompan.ZoomPan() figZoom = zp.zoom_factory(ax1, base_scale=1.1) figPan = zp.pan_factory(ax1) '''#subplot2 bar ax2 = plt.subplot2grid((10, 1), (8, 0), rowspan=2, colspan=1) # ax2.plot(quotes.vol) # ax2.set_xticks(np.linspace(-2, len(quotes) + 2, len(Ti))) ll = min(quotes.vol.values.tolist()) * 0.97 hh = max(quotes.vol.values.tolist()) * 1.03 ax2.set_ylim(ll, hh) # ax2.set_xticklabels(Ti) # plt.hist(quotes.vol, histtype='bar', rwidth=0.8) plt.bar(x_date_list,quotes.vol, label="Volume", color='b') ''' #画Volume no tight_layout() ''' pad = 0.25 yl = ax1.get_ylim() ax1.set_ylim(yl[0]-(yl[1]-yl[0])*pad,yl[1]) ax2 = ax1.twinx() ax2.set_position(mat.transforms.Bbox([[0.125,0.1],[0.9,0.32]])) volume = np.asarray(quotes.amount) pos = quotes['open']-quotes['close']<0 neg = quotes['open']-quotes['close']>=0 idx = quotes.reset_index().index ax2.bar(idx[pos],volume[pos],color='red',width=1,align='center') ax2.bar(idx[neg],volume[neg],color='green',width=1,align='center') yticks = ax2.get_yticks() ax2.set_yticks(yticks[::3]) ''' # same sharex plt.subplots_adjust(left=0.05, bottom=0.08, right=0.95, top=0.95, wspace=0.15, hspace=0.00) plt.setp(ax1.get_xticklabels(), visible=False) yl = ax1.get_ylim() # ax2 = plt.subplot(212, sharex=ax1) ax2 = plt.subplot2grid((10, 1), (8, 0), rowspan=2, colspan=1, sharex=ax1) # ax2.set_position(mat.transforms.Bbox([[0.125,0.1],[0.9,0.32]])) volume = np.asarray(quotes.amount) pos = quotes['open'] - quotes['close'] < 0 neg = quotes['open'] - quotes['close'] >= 0 idx = quotes.reset_index().index ax2.bar(idx[pos], volume[pos], color='red', width=1, align='center') ax2.bar(idx[neg], volume[neg], color='green', width=1, align='center') yticks = ax2.get_yticks() ax2.set_yticks(yticks[::3]) # plt.tight_layout() # plt.subplots_adjust(hspace=0.00, bottom=0.08) plt.xticks(rotation=15, horizontalalignment='center') # plt.bar(x_date_list,quotes.vol, label="Volume", color='b') # quotes['vol'].plot(kind='bar', ax=ax2, color='g', alpha=0.1) # ax2.set_ylim([0, ax2.get_ylim()[1] * 2]) # plt.gcf().subplots_adjust(bottom=0.15) # fig.subplots_adjust(left=0.05, bottom=0.08, right=0.95, top=0.95, wspace=0.15, hspace=0.25) #scale the x-axis tight # ax2.set_xlim(min(x_date_list),max(x_date_list)) # the y-ticks for the bar were too dense, keep only every third one # plt.grid(True) # plt.xticks(rotation=30, horizontalalignment='center') # plt.setp( axs[1].xaxis.get_majorticklabels(), rotation=70 ) # plt.legend() # plt.tight_layout() # plt.draw() # plt.show() plt.show(block=False)
from statistics import mean import seaborn as sns #import pymc3 as pm from scipy import stats plt.style.use('seaborn') mpl.rcParams['font.family'] = 'serif' np.random.seed(1000) raw = pd.read_csv('2510_input.csv', nrows=300, index_col=0, parse_dates=True).dropna() df = DataFrame(raw) gridsize = (3, 2) fig = plt.figure(figsize=(12, 8)) ax1 = plt.subplot2grid(gridsize, (0, 0), colspan=2, rowspan=2) ax2 = plt.subplot2grid(gridsize, (2, 0)) ax3 = plt.subplot2grid(gridsize, (2, 1)) fig.tight_layout() #AVG_2Y5Y10Y = df['CZK 2Y5Y10Y'+'PLN 2Y5Y10Y'+'ILS 2Y5Y10Y'+'HUF 2Y5Y10Y'+'CHF 2Y5Y10Y'+'EUR 2Y5Y10Y'+'GBP 2Y5Y10Y'+'SEK 2Y5Y10Y'][-250:] df_5y5y = df[[ 'CZK 5Y X 5Y Fwd Swap Rate', 'PLN 5Y X 5Y Fwd Swap Rate', 'ILS 5Y X 5Y Fwd Swap Rate', 'HUF 5Y X 5Y Fwd Swap Rate', '5Y X 5Y CHF Fwd Swap Rate', '5Y X 5Y EUR Fwd Swap Rate', '5Y X 5Y GBP Fwd Swap Rate', '5Y X 5Y SEK Fwd Swap Rate' ]][:-150] avg_5y5y = df_5y5y.sum(axis=1) / 8 legend = ['CZK', 'PLN', 'ILS', 'HUF', 'CHF', 'EUR', 'GBP', 'SEK']
def weatherstat(df, destguid=None): dfduplicates = df.groupby('date').apply( lambda d: tuple(d.index) if len(d.index) > 1 else None).dropna() log.info(dfduplicates) df.drop_duplicates(inplace=True) # 去重,去除可能重复的天气数据记录,原因可能是邮件重复发送等 # print(df.head(30)) df['date'] = df['date'].apply(lambda x: pd.to_datetime(x)) # 日期做索引,去重并重新排序 df.index = df['date'] df = df[~df.index.duplicated()] df.sort_index(inplace=True) # print(df) df.dropna(how='all', inplace=True) # 去掉空行,索引日期,后面索引值相同的行会被置空,需要去除 # print(len(df)) # df['gaowen'] = df['gaowen'].apply(lambda x: np.nan if str(x).isspace() else int(x)) #处理空字符串为空值的另外一骚 df['gaowen'] = df['gaowen'].apply(lambda x: int(x) if x else None) # 数字串转换成整数,如果空字符串则为空值 df['diwen'] = df['diwen'].apply(lambda x: int(x) if x else None) df['fengsu'] = df['fengsu'].apply(lambda x: int(x) if x else None) df['shidu'] = df['shidu'].apply(lambda x: int(x) if x else None) # df['gaowen'] = df['gaowen'].astype(int) # df['diwen'] = df['diwen'].astype(int) # df['fengsu'] = df['fengsu'].astype(int) # df['shidu'] = df['shidu'].astype(int) df.fillna(method='ffill', inplace=True) # 向下填充处理可能出现的空值,bfill是向上填充 df['wendu'] = (df['gaowen'] + df['diwen']) / 2 df['richang'] = df['sunoff'] - df['sunon'] df['richang'] = df['richang'].astype(int) df['wendu'] = df['wendu'].astype(int) # print(df.tail(30)) df_recent_year = df.iloc[-365:] # print(df_recent_year) # print(df[df.gaowen == df.iloc[-364:]['gaowen'].max()]) # df_before_year = df.iloc[:-364] plt.figure(figsize=(16, 20)) ax1 = plt.subplot2grid((4, 2), (0, 0), colspan=2, rowspan=2) ax1.plot(df['gaowen'], lw=0.3, label=u'日高温') ax1.plot(df['diwen'], lw=0.3, label=u'日低温') ax1.plot(df['wendu'], 'g', lw=0.7, label=u'日温度(高温低温平均)') quyangtianshu = 10 ax1.plot(df['wendu'].resample('%dD' % quyangtianshu).mean(), 'b', lw=1.2, label='日温度(每%d天平均)' % quyangtianshu) ax1.plot(df[df.fengsu > 5]['fengsu'], '*', label='风速(大于五级)') plt.legend(loc=2) # 起始统计日 kedu = df.iloc[0] ax1.plot([kedu['date'], kedu['date']], [0, kedu['wendu']], 'c--', lw=0.4) ax1.scatter([ kedu['date'], ], [kedu['wendu']], 50, color='Wheat') fsize = 8 txt = str(kedu['wendu']) ax1.annotate(txt, xy=(kedu['date'], kedu['wendu']), xycoords='data', xytext=(-(len(txt) * fsize), +20), textcoords='offset points', fontsize=fsize, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -20), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) # 去年今日,如果数据不足一年,取今日 if len(df) >= 366: locqnjr = -365 else: locqnjr = -1 kedu = df.iloc[locqnjr] # kedu = df.iloc[-364] print(kedu) ax1.plot([kedu['date'], kedu['date']], [0, kedu['wendu']], 'c--') ax1.scatter([ kedu['date'], ], [kedu['wendu']], 50, color='Wheat') ax1.annotate(str(kedu['wendu']), xy=(kedu['date'], kedu['wendu']), xycoords='data', xytext=(-5, +20), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -35), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) # 今日 kedu = df.iloc[-1] # print(kedu) ax1.plot([kedu['date'], kedu['date']], [0, kedu['wendu']], 'c--') ax1.scatter([ kedu['date'], ], [kedu['gaowen']], 50, color='BlueViolet') ax1.annotate(str(kedu['gaowen']), xy=(kedu['date'], kedu['gaowen']), xycoords='data', xytext=(-10, +20), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) ax1.scatter([ kedu['date'], ], [kedu['wendu']], 50, color='BlueViolet') ax1.annotate(str(kedu['wendu']), xy=(kedu['date'], kedu['wendu']), xycoords='data', xytext=(10, +5), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) ax1.scatter([ kedu['date'], ], [kedu['diwen']], 50, color='BlueViolet') ax1.annotate(str(kedu['diwen']), xy=(kedu['date'], kedu['diwen']), xycoords='data', xytext=(-10, -20), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -35), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) # 最近一年最高温 kedu = df_recent_year[df_recent_year.gaowen == df_recent_year.iloc[-364:] ['gaowen'].max()].iloc[0] # print(kedu) ax1.plot([kedu['date'], kedu['date']], [0, kedu['gaowen']], 'c--') ax1.scatter([ kedu['date'], ], [kedu['gaowen']], 50, color='Wheat') ax1.annotate(str(kedu['gaowen']), xy=(kedu['date'], kedu['gaowen']), xycoords='data', xytext=(-20, +5), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -20), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) # 最近一年最低温 kedu = df_recent_year[df_recent_year.diwen == df_recent_year.iloc[-364:] ['diwen'].min()].iloc[0] ax1.plot([kedu['date'], kedu['date']], [0, kedu['diwen']], 'c--') ax1.scatter([ kedu['date'], ], [kedu['diwen']], 50, color='Wheat') ax1.annotate(str(kedu['diwen']), xy=(kedu['date'], kedu['diwen']), xycoords='data', xytext=(-20, +5), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -20), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) # 最高温 kedu = df[df.gaowen == df['gaowen'].max()].iloc[0] ax1.plot([kedu['date'], kedu['date']], [0, kedu['gaowen']], 'c--') ax1.scatter([ kedu['date'], ], [kedu['gaowen']], 50, color='Wheat') ax1.annotate(str(kedu['gaowen']), xy=(kedu['date'], kedu['gaowen']), xycoords='data', xytext=(-20, +5), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -20), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) # 最低温 kedu = df[df.diwen == df['diwen'].min()].iloc[0] ax1.plot([kedu['date'], kedu['date']], [0, kedu['diwen']], 'c--') ax1.scatter([ kedu['date'], ], [kedu['diwen']], 50, color='Wheat') ax1.annotate(str(kedu['diwen']), xy=(kedu['date'], kedu['diwen']), xycoords='data', xytext=(-20, +5), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -20), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) ax1.set_ylabel(u'(摄氏度℃)') ax1.grid(True) ax1.set_title(u'最高气温、最低气温和均值温度图') ax3 = plt.subplot2grid((4, 2), (2, 0), colspan=2, rowspan=2) # print(type(ax3)) ax3.plot(df_recent_year['shidu'], 'c.', lw=0.3, label=u'湿度') ax3.plot(df_recent_year['shidu'].resample('15D').mean(), 'g', lw=1.5) ax3.set_ylabel(u'(百分比%)') ax3.set_title(u'半月平均湿度图') img_wenshifeng_path = dirmainpath / "img" / 'weather' / 'wenshifeng.png' img_wenshifeng_path_str = str(img_wenshifeng_path) touchfilepath2depth(img_wenshifeng_path) plt.legend(loc='lower left') plt.savefig(img_wenshifeng_path_str) imglist = list() imglist.append(img_wenshifeng_path_str) plt.close() plt.figure(figsize=(16, 10)) fig, ax1 = plt.subplots() plt.plot(df['date'], df['sunon'], lw=0.8, label=u'日出') plt.plot(df['date'], df['sunoff'], lw=0.8, label=u'日落') ax = plt.gca() # ax.yaxis.set_major_formatter(FuncFormatter(min_formatter)) # 主刻度文本用pi_formatter函数计算 ax.yaxis.set_major_formatter( FuncFormatter(lambda x, pos: "%02d:%02d" % (int(x / 60), int(x % 60)))) # 主刻度文本用pi_formatter函数计算 plt.ylim((0, 24 * 60)) plt.yticks(np.linspace(0, 24 * 60, 25)) plt.xlabel(u'日期') plt.ylabel(u'时刻') plt.legend(loc=6) plt.title(u'日出日落时刻和白天时长图') plt.grid(True) ax2 = ax1.twinx() print(ax2) plt.plot(df_recent_year['date'], df_recent_year['richang'], 'r', lw=1.5, label=u'日长') ax = plt.gca() # ax.yaxis.set_major_formatter(FuncFormatter(min_formatter)) # 主刻度文本用pi_formatter函数计算 ax.yaxis.set_major_formatter( FuncFormatter(lambda x, pos: "%02d:%02d" % (int(x / 60), int(x % 60)))) # 主刻度文本用pi_formatter函数计算 # ax.set_xticklabels(rotation=45, horizontalalignment='right') plt.ylim((3 * 60, 12 * 60)) plt.yticks(np.linspace(3 * 60, 15 * 60, 13)) plt.ylabel(u'时分') plt.legend(loc=5) plt.grid(True) # plt.show() img_sunonoff_path = dirmainpath / 'img' / 'weather' / 'sunonoff.png' img_sunonoff_path_str = str(img_sunonoff_path) touchfilepath2depth(img_sunonoff_path) plt.savefig(img_sunonoff_path_str) imglist.append(img_sunonoff_path_str) plt.close() imglist2note(get_notestore(), imglist, destguid, '武汉天气图')
print_frame = False file_name = "square_lattice" L = 10 dims = 2 initializer = ParticleInitialize() c = Container(dims,10) c = initializer(file_name,c) ################################# distance_matrix = PeroidicDistanceMatrix(L) force = LeonardJonesForce(distance_matrix,c.masses) integrate = VerletIntegrator(.01) ########### FORCED ANIMATION ########## circles = [] fig = plt.figure(figsize=(10,5)) ax = plt.subplot2grid((2,2),(0,0),rowspan=2,xlim=(0,c.L[0]),ylim=(0,c.L[1]),aspect='equal') ax2 = plt.subplot2grid((2,2),(0,1),title='Potential Energy',ylim=(-1,potential_ylim)) ax2.grid() ax2.set_xticklabels([]) ax3 = plt.subplot2grid((2,2),(1,1),title='Kinetic Energy') ax3.grid() ax3.set_xticklabels([]) ax3.text(.4,.5,"????",fontsize=25, transform=ax3.transAxes) fig.subplots_adjust(wspace=.4) # this makes space between subplots fig.subplots_adjust(hspace=.4) # this makes space between subplots def prettify_circle(e): color="lightsteelblue" facecolor="green" alpha=.6 e.set_clip_box(ax.bbox)