def ax_draw_macd(axes, kdata, n1=12, n2=26, n3=9): """绘制MACD :param axes: 指定的坐标轴 :param KData kdata: KData :param int n1: 指标 MACD 的参数1 :param int n2: 指标 MACD 的参数2 :param int n3: 指标 MACD 的参数3 """ macd = MACD(CLOSE(kdata), n1, n2, n3) bmacd, fmacd, smacd = macd.getResult(0), macd.getResult(1), macd.getResult(2) text = 'MACD(%s,%s,%s) DIF:%.2f, DEA:%.2f, BAR:%.2f'%(n1,n2,n3,fmacd[-1],smacd[-1],bmacd[-1]) axes.text(0.01,0.97, text, horizontalalignment='left', verticalalignment='top', transform=axes.transAxes) total = len(kdata) x = [i-0.2 for i in range(total)] x1 = [x[i] for i,d in enumerate(bmacd) if d>0] y1 = [i for i in bmacd if i>0] x2 = [x[i] for i,d in enumerate(bmacd) if d<=0] y2 = [i for i in bmacd if i<=0] axes.bar(x1,y1, width=0.4, color='r', edgecolor='r') axes.bar(x2,y2, width=0.4, color='g', edgecolor='g') axt = axes.twinx() axt.grid(False) axt.set_yticks([]) fmacd.plot(axes=axt, linestyle='--', legend_on=False, text_on=False) smacd.plot(axes=axt, legend_on=False, text_on=False) for label in axt.get_xticklabels(): label.set_visible(False)
def ibar(indicator, new=True, axes=None, legend_on=False, text_on=False, text_color='k', label=None, width=0.4, color='r', edgecolor='r', zero_on=False, *args, **kwargs): """绘制indicator柱状图 :param Indicator indicator: Indicator实例 :param axes: 指定的坐标轴 :param new: 是否在新窗口中显示,只在没有指定axes时生效 :param legend_on: 是否打开图例 :param text_on: 是否在左上角显示指标名称及其参数 :param text_color: 指标名称解释文字的颜色,默认为黑色 :param str label: label显示文字信息,text_on 及 legend_on 为 True 时生效 :param zero_on: 是否需要在y=0轴上绘制一条直线 :param width: Bar的宽度 :param color: Bar的颜色 :param edgecolor: Bar边缘颜色 :param args: pylab plot参数 :param kwargs: pylab plot参数 """ if not indicator: print("indicator is None") return if not axes: axes = create_figure() if new else gca() if not label: label = "%s %.2f" % (indicator.long_name, indicator[-1]) py_indicatr = [ None if x == constant.null_price else x for x in indicator] x = [i-0.2 for i in range(len(indicator))] y = py_indicatr axes.bar(x, py_indicatr, width=width, color=color, edgecolor=edgecolor, *args, **kwargs) if legend_on: leg = axes.legend(loc='upper left') leg.get_frame().set_alpha(0.5) if text_on: if not axes.texts: axes.text(0.01,0.97, label, horizontalalignment='left', verticalalignment='top', transform=axes.transAxes, color=text_color) else: temp_str = axes.texts[0].get_text() + ' ' + label axes.texts[0].set_text(temp_str) if zero_on: ylim = axes.get_ylim() if ylim[0]<0<ylim[1]: axes.hlines(0,0,len(indicator)) axes.autoscale_view() axes.set_xlim(-1, len(indicator)+1)
def main(fileName): imgName = fileName.replace('csv', 'png') df = pd.read_csv(fileName, names=['YEAR', 'SEASON', 'BASIN_CD', 'BASIN_MAP', 'STN_CD', 'STN_MAP']) df['SEASON'].replace(r'^S', '',inplace=True, regex=True) df['SEASON'].astype(float) startYear = min(df.YEAR) endYear = max(df.YEAR) yearCount = 0 columns = 9 rows = 5 fig, ax_array = plt.subplots(rows, columns, figsize=(55,30)) for i, ax_row in enumerate(ax_array): for j, axes in enumerate(ax_row): currentYear = str(startYear + yearCount) axes.set_title('Year: ' + currentYear) df2 = df.query('YEAR==' + str(currentYear)) basinCd = df2.BASIN_CD[yearCount * len(df2)] stnCd = df2.STN_CD[yearCount * len(df2)] basinLegend = 'BASIN(' + str(basinCd) + ')' stnLegend = 'STN(' + str(stnCd) + ')' seasonList = pd.Series([1, 2, 3, 4], dtype='float') axes.bar(seasonList + 0.14, df2.BASIN_MAP, color = 'b', width = 0.25) axes.bar(seasonList - 0.14, df2.STN_MAP, color = 'r', width = 0.25) axes.legend([basinLegend, stnLegend]) axes.set_xlabel('Season', size=12) axes.set_ylabel('Prcp(mm)', size=12) axes.set_xticks(range(int(1), int(4) + 1, 1)) axes.set_xticklabels(['Spring', 'Summer', 'Autumn', 'Winter']) #axes.grid(True) yearCount = yearCount + 1 savefig('graph_season/' + imgName)
def ax_draw_macd(axes, kdata, n1=12, n2=26, n3=9): """绘制MACD :param axes: 指定的坐标轴 :param KData kdata: KData :param int n1: 指标 MACD 的参数1 :param int n2: 指标 MACD 的参数2 :param int n3: 指标 MACD 的参数3 """ macd = MACD(CLOSE(kdata), n1, n2, n3) bmacd, fmacd, smacd = macd.getResult(0), macd.getResult(1), macd.getResult(2) text = 'MACD(%s,%s,%s) DIF:%.2f, DEA:%.2f, BAR:%.2f' % ( n1, n2, n3, fmacd[-1], smacd[-1], bmacd[-1] ) axes.text( 0.01, 0.97, text, horizontalalignment='left', verticalalignment='top', transform=axes.transAxes ) total = len(kdata) x = [i - 0.2 for i in range(total)] x1 = [x[i] for i, d in enumerate(bmacd) if d > 0] y1 = [i for i in bmacd if i > 0] x2 = [x[i] for i, d in enumerate(bmacd) if d <= 0] y2 = [i for i in bmacd if i <= 0] axes.bar(x1, y1, width=0.4, color='r', edgecolor='r') axes.bar(x2, y2, width=0.4, color='g', edgecolor='g') axt = axes.twinx() axt.grid(False) axt.set_yticks([]) fmacd.plot(axes=axt, linestyle='--', legend_on=False, text_on=False) smacd.plot(axes=axt, legend_on=False, text_on=False) for label in axt.get_xticklabels(): label.set_visible(False)
def ax_draw_macd2(axes, ref, kdata, n1=12, n2=26, n3=9): """绘制MACD。 当BAR值变化与参考序列ref变化不一致时,显示为灰色, 当BAR和参考序列ref同时上涨,显示红色 当BAR和参考序列ref同时下跌,显示绿色 :param axes: 指定的坐标轴 :param ref: 参考序列,EMA :param KData kdata: KData :param int n1: 指标 MACD 的参数1 :param int n2: 指标 MACD 的参数2 :param int n3: 指标 MACD 的参数3 """ macd = MACD(CLOSE(kdata), n1, n2, n3) bmacd, fmacd, smacd = macd.getResult(0), macd.getResult(1), macd.getResult(2) text = 'MACD(%s,%s,%s) DIF:%.2f, DEA:%.2f, BAR:%.2f' % ( n1, n2, n3, fmacd[-1], smacd[-1], bmacd[-1] ) axes.text( 0.01, 0.97, text, horizontalalignment='left', verticalalignment='top', transform=axes.transAxes ) total = len(kdata) x = [i - 0.2 for i in range(0, total)] y = bmacd x1, x2, x3 = [x[0]], [], [] y1, y2, y3 = [y[0]], [], [] for i in range(1, total): if ref[i] - ref[i - 1] > 0 and y[i] - y[i - 1] > 0: x2.append(x[i]) y2.append(y[i]) elif ref[i] - ref[i - 1] < 0 and y[i] - y[i - 1] < 0: x3.append(x[i]) y3.append(y[i]) else: x1.append(x[i]) y1.append(y[i]) axes.bar(x1, y1, width=0.4, color='#BFBFBF', edgecolor='#BFBFBF') axes.bar(x2, y2, width=0.4, color='r', edgecolor='r') axes.bar(x3, y3, width=0.4, color='g', edgecolor='g') axt = axes.twinx() axt.grid(False) axt.set_yticks([]) fmacd.plot(axes=axt, linestyle='--', legend_on=False, text_on=False) smacd.plot(axes=axt, legend_on=False, text_on=False) for label in axt.get_xticklabels(): label.set_visible(False)
def ax_draw_macd2(axes, ref, kdata, n1=12, n2=26, n3=9): """绘制MACD。 当BAR值变化与参考序列ref变化不一致时,显示为灰色, 当BAR和参考序列ref同时上涨,显示红色 当BAR和参考序列ref同时下跌,显示绿色 :param axes: 指定的坐标轴 :param ref: 参考序列,EMA :param KData kdata: KData :param int n1: 指标 MACD 的参数1 :param int n2: 指标 MACD 的参数2 :param int n3: 指标 MACD 的参数3 """ macd = MACD(CLOSE(kdata), n1, n2, n3) bmacd, fmacd, smacd = macd.getResult(0), macd.getResult(1), macd.getResult(2) text = 'MACD(%s,%s,%s) DIF:%.2f, DEA:%.2f, BAR:%.2f'%(n1,n2,n3,fmacd[-1],smacd[-1],bmacd[-1]) axes.text(0.01,0.97, text, horizontalalignment='left', verticalalignment='top', transform=axes.transAxes) total = len(kdata) x = [i-0.2 for i in range(0, total)] y = bmacd x1,x2,x3 = [x[0]],[],[] y1,y2,y3 = [y[0]],[],[] for i in range(1, total): if ref[i]-ref[i-1]>0 and y[i]-y[i-1]>0: x2.append(x[i]) y2.append(y[i]) elif ref[i]-ref[i-1]<0 and y[i]-y[i-1]<0: x3.append(x[i]) y3.append(y[i]) else: x1.append(x[i]) y1.append(y[i]) axes.bar(x1,y1, width=0.4, color='#BFBFBF', edgecolor='#BFBFBF') axes.bar(x2,y2, width=0.4, color='r', edgecolor='r') axes.bar(x3,y3, width=0.4, color='g', edgecolor='g') axt = axes.twinx() axt.grid(False) axt.set_yticks([]) fmacd.plot(axes=axt, linestyle='--', legend_on=False, text_on=False) smacd.plot(axes=axt, legend_on=False, text_on=False) for label in axt.get_xticklabels(): label.set_visible(False)
def ibar( indicator, new=True, axes=None, legend_on=False, text_on=False, text_color='k', label=None, width=0.4, color='r', edgecolor='r', zero_on=False, *args, **kwargs ): """绘制indicator柱状图 :param Indicator indicator: Indicator实例 :param axes: 指定的坐标轴 :param new: 是否在新窗口中显示,只在没有指定axes时生效 :param legend_on: 是否打开图例 :param text_on: 是否在左上角显示指标名称及其参数 :param text_color: 指标名称解释文字的颜色,默认为黑色 :param str label: label显示文字信息,text_on 及 legend_on 为 True 时生效 :param zero_on: 是否需要在y=0轴上绘制一条直线 :param width: Bar的宽度 :param color: Bar的颜色 :param edgecolor: Bar边缘颜色 :param args: pylab plot参数 :param kwargs: pylab plot参数 """ if not indicator: print("indicator is None") return if not axes: axes = create_figure() if new else gca() if not label: label = "%s %.2f" % (indicator.long_name, indicator[-1]) py_indicatr = [None if x == constant.null_price else x for x in indicator] x = [i - 0.2 for i in range(len(indicator))] y = py_indicatr axes.bar(x, py_indicatr, width=width, color=color, edgecolor=edgecolor, *args, **kwargs) if legend_on: leg = axes.legend(loc='upper left') leg.get_frame().set_alpha(0.5) if text_on: if not axes.texts: axes.text( 0.01, 0.97, label, horizontalalignment='left', verticalalignment='top', transform=axes.transAxes, color=text_color ) else: temp_str = axes.texts[0].get_text() + ' ' + label axes.texts[0].set_text(temp_str) if zero_on: ylim = axes.get_ylim() if ylim[0] < 0 < ylim[1]: axes.hlines(0, 0, len(indicator)) axes.autoscale_view() axes.set_xlim(-1, len(indicator) + 1) k = indicator.get_context() if len(k) > 0: ax_set_locator_formatter(axes, k.get_date_list(), k.get_query().ktype)
def ibar(indicator, axes=None, width=0.4, color='r', edgecolor='r', new=True, legend_on=False, text_on=False, text_color='k', label=None, zero_on=False, *args, **kwargs): """绘制indicator曲线 indicator: Indicator实例 axes: 指定的坐标轴 new: 是否在新窗口中显示,只在没有指定axes时生效 legend_on : 是否打开图例 text_on: 是否在左上角显示指标名称及其参数 text_color: 指标名称解释文字的颜色,默认为黑色 zero_on: 是否需要在y=0轴上绘制一条直线 *args, **kwargs : pylab bar参数 """ if not indicator: print("indicator is None") return if not axes: axes = create_one_axes_figure() if new else gca() if not label: label = "%s %.2f" % (indicator.long_name, indicator[-1]) py_indicatr = [None if x == constant.null_price else x for x in indicator] x = [i - 0.2 for i in range(len(indicator))] y = py_indicatr axes.bar(x, py_indicatr, width=width, color=color, edgecolor=edgecolor, *args, **kwargs) if legend_on: leg = axes.legend(loc='upper left') leg.get_frame().set_alpha(0.5) if text_on: if not axes.texts: axes.text(0.01, 0.97, label, horizontalalignment='left', verticalalignment='top', transform=axes.transAxes, color=text_color) else: temp_str = axes.texts[0].get_text() + ' ' + label axes.texts[0].set_text(temp_str) if zero_on: ylim = axes.get_ylim() if ylim[0] < 0 < ylim[1]: axes.hlines(0, 0, len(indicator)) axes.autoscale_view() axes.set_xlim(-1, len(indicator) + 1)