def draw_china(sat1Nm, sat2Nm, ymd_s, ymd_e, drew_points, out_fig_file): ''' 画 中国区图 ''' fig = plt.figure(figsize=(8, 6), dpi=100) # china ax = subplot(111) plt.subplots_adjust(left=0.11, right=0.91, bottom=0.12, top=0.92) drawFigs(fig, drew_points, False) circle1 = mpatches.Circle((58, 36), 6, color=RED, ec=EDGE_GRAY, lw=0.3) circle2 = mpatches.Circle((219, 36), 6, color=BLUE, ec=EDGE_GRAY, lw=0.3) fig.patches.extend([circle1, circle2]) TEXT_Y = 0.05 fig.text(0.1, TEXT_Y, '%s' % sat1Nm, color=RED, fontproperties=FONT0) fig.text(0.3, TEXT_Y, '%s' % sat2Nm, color=BLUE, fontproperties=FONT0) if ymd_s != ymd_e: fig.text(0.55, TEXT_Y, '%s-%s' % (ymd_s, ymd_e), fontproperties=FONT0) else: fig.text(0.55, TEXT_Y, '%s' % ymd_s, fontproperties=FONT0) fig.text(0.83, TEXT_Y, ORG_NAME, fontproperties=FONT0) # 设定Map边框粗细 spines = ax.spines for eachspine in spines: spines[eachspine].set_linewidth(0) out_fig_dir = os.path.dirname(out_fig_file) # add 2015-10-16 10:30 if not os.path.exists(out_fig_dir): os.makedirs(out_fig_dir) fig.savefig(out_fig_file, dpi=100) fig.clear() plt.close()
def plot(cls, data, figure_folder, msg="", suffix=""): fig, ax = plt.subplots() plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.2) x = np.arange(len(data[:])) ax.plot(x, data[:, 0], c="red", linewidth=2, label="agent 01") ax.plot(x, data[:, 1], c="blue", linewidth=2, label="agent 12") ax.plot(x, data[:, 2], c="green", linewidth=2, label="agent 20") plt.ylim([-0.01, 1.01]) plt.text(0, -0.23, "PARAMETERS. {}".format(msg)) ax.legend(fontsize=12, bbox_to_anchor=(1.1, 1.05)) # loc='upper center' ax.set_xlabel("$t$") ax.set_ylabel("Proportion of agents proceeding to indirect exchange") ax.set_title("Money emergence with a basal ganglia model") # Save fig if not exists(figure_folder): mkdir(figure_folder) fig_name = "{}/figure_{}.pdf".format(figure_folder, suffix.split(".p")[0]) plt.savefig(fig_name) plt.close()
def draw_fixed(sat2Nm, ymd_s, ymd_e, drew_points, out_fig_file): ''' 画固定点图 ''' fig = plt.figure(figsize=(8, 4.5)) # world ax = subplot(111) plt.subplots_adjust(left=0.09, right=0.95, bottom=0.11, top=0.97) drawFigs(fig, drew_points, True) circle1 = mpatches.Circle((60, 24), 6, color=RED, ec=EDGE_GRAY, lw=0.3) circle2 = mpatches.Circle((219, 24), 6, color=BLUE, ec=EDGE_GRAY, lw=0.3) fig.patches.extend([circle1, circle2]) fig.text(0.1, 0.04, 'Fixed Points', color=RED, fontproperties=FONT0) fig.text(0.3, 0.04, '%s' % sat2Nm, color=BLUE, fontproperties=FONT0) if ymd_s != ymd_e: fig.text(0.55, 0.04, '%s-%s' % (ymd_s, ymd_e), fontproperties=FONT0) else: fig.text(0.55, 0.04, '%s' % ymd_s, fontproperties=FONT0) fig.text(0.83, 0.04, ORG_NAME, fontproperties=FONT0) # 设定Map边框粗细 spines = ax.spines for eachspine in spines: spines[eachspine].set_linewidth(0) out_fig_dir = os.path.dirname(out_fig_file) # add 2015-10-16 10:30 if not os.path.exists(out_fig_dir): os.makedirs(out_fig_dir) fig.savefig(out_fig_file, dpi=100) fig.clear() plt.close()
def draw_polar(sat1Nm, sat2Nm, ymd_s, ymd_e, drew_points, out_fig_file): ''' 画 两极图 ''' fig = plt.figure(figsize=(8, 4.5)) # polar plt.subplots_adjust(left=0.09, right=0.93, bottom=0.11, top=0.94) drew_polar_Figs(fig, drew_points) circle1 = mpatches.Circle((58, 33), 6, color=RED, ec=EDGE_GRAY, lw=0.3) circle2 = mpatches.Circle((219, 33), 6, color=BLUE, ec=EDGE_GRAY, lw=0.3) fig.patches.extend([circle1, circle2]) TEXT_Y = 0.06 fig.text(0.1, TEXT_Y, '%s' % sat1Nm, color=RED, fontproperties=FONT0) fig.text(0.3, TEXT_Y, '%s' % sat2Nm, color=BLUE, fontproperties=FONT0) if ymd_s != ymd_e: fig.text(0.55, TEXT_Y, '%s-%s' % (ymd_s, ymd_e), fontproperties=FONT0) else: fig.text(0.55, TEXT_Y, '%s' % ymd_s, fontproperties=FONT0) fig.text(0.83, TEXT_Y, ORG_NAME, fontproperties=FONT0) fig.subplots_adjust(wspace=0.1) out_fig_dir = os.path.dirname(out_fig_file) # add 2015-10-16 10:30 if not os.path.exists(out_fig_dir): os.makedirs(out_fig_dir) fig.savefig(out_fig_file, dpi=100) fig.clear() plt.close()
def __init__(self, kernel, xmin, xmax, ymin, ymax, base_accuracy, max_iter, interpolation='none', splits=None, *args): fig, ax = plt.subplots() self.base_accuracy = base_accuracy self.height = base_accuracy self.width = base_accuracy self.max_iter = max_iter self.kernel = kernel self.x = np.linspace(xmin, xmax, self.width) self.y = np.linspace(ymin, ymax, self.height) self.splits = splits self.args = args self.ax = ax self.fig = fig self.interpolation = interpolation self.image_array = None class ImageSaver(ToolBase): image_array = None description = 'Save the image only' def trigger(self, *args, **kwargs): path = filedialog.asksaveasfilename(initialfile='Fractal_1', defaultextension='png', filetypes=[('PNG', ".png") ]) if path: image = Image.fromarray(self.image_array, mode='RGB') image.save(path, "PNG", quality=95, optimize=False) tm = fig.canvas.manager.toolmanager self.image_saver = tm.add_tool("Save Image", ImageSaver) fig.canvas.manager.toolbar.add_tool(tm.get_tool("Save Image"), "toolgroup") self.slider_box = plt.axes([0.12, 0.02, 0.7, 0.03]) self.slider = Slider(self.slider_box, 'Max iter:', 100, 50000, valinit=max_iter, valstep=10) self.slider.set_val(max_iter) self.slider.on_changed(self.on_slider_change) plt.subplots_adjust(bottom=0.1, top=0.95)
def drawHeatMap(dataDF, groupByVariableNames, subplotIndex, subplotIndexName, valueIndexName): figure = plt.figure(figsize=(12, 10)) order = [7,8,9,4,5,6,1,2,3] numOfplot = 0 for key, subDF in dataDF.groupby(groupByVariableNames): subplot = figure.add_subplot(subplotIndex[0], subplotIndex[1], order[numOfplot]) plotDF = subDF.reset_index() plotDF.plot.scatter(x=subplotIndexName[0], y=subplotIndexName[1], c=valueIndexName, colormap="jet", ax=subplot, vmin=0, vmax=9) numOfplot = numOfplot + 1 plt.subplots_adjust(wspace=0.8, hspace=0.4) plt.savefig("./Graphs/Factor3_fixed_50sample_actionDistribution_divergence_HeatMap.png")
def draw_predictions(self, data, predictions): from pylab import plt true, lstm, bp = [], [], [] for i in range(len(predictions)): true.append(predictions[i][0]) bp.append(predictions[i][1]) lstm.append(predictions[i][2]) x = [1, 2, 3, 4, 5] plt.plot(x, true, 'cx--', label='true') plt.plot(x, bp, 'mo:', label='fusion') plt.plot(x, lstm, 'kp-.', label='lstm') plt.legend() plt.margins(0) plt.subplots_adjust(bottom=0.15) plt.xlabel(u"days") plt.ylabel("price") plt.title("tendency predictions of different models") plt.savefig('blog/static/blog/bootstrap/img/presult.jpg')
def show(filename=None, labels=False): if not labels: # fix everything if in 3D mode plt.subplots_adjust(left=0.0, right=1.1, bottom=0.0, top=1.0) # also do this if in 2d mode if not is_3d: frame1 = plt.gca() frame1.axes.get_xaxis().set_visible(False) frame1.axes.get_yaxis().set_visible(False) if legend: plt.legend(loc="upper left", fontsize=8, prop={'family': "Monaco", 'weight': "roman", 'size': "x-small"}) if filename is not None: if '.' not in filename: if not os.path.isdir(filename): os.makedirs(filename) filename = os.path.abspath(os.path.join(filename, "%s.png" % util.timestamp())) figure.savefig(filename, dpi=150, facecolor=figure.get_facecolor(), edgecolor='none') plt.show()
qval_min = min(qvals) qval_max = max(qvals) qval_n_bins = 40 hist(qvals, bins=qval_n_bins, range=(qval_min, qval_max), facecolor='red', alpha=0.5) plt.title('QVal Histogram') plt.xlabel('QVal') plt.ylabel('Counts') plt.savefig(output_dir + "/automatic/qval.tif") plt.close() plt.subplots_adjust(hspace=0.6) if len(qvals_pertilt[0]) > 0: plt.subplot(411) hist(qvals_pertilt[0], bins=qval_n_bins, range=(qval_min, qval_max), facecolor='red', alpha=0.5) plt.title('QVAL Histogram (0-10 degrees)') plt.ylabel('Counts') if len(qvals_pertilt[1]) == 0 and len(qvals_pertilt[2]) == 0 and len( qvals_pertilt[3]) == 0: plt.xlabel('QVAL') if len(qvals_pertilt[1]) > 0: plt.subplot(412) hist(qvals_pertilt[1],
plt.subplot(241).set_title("无翻拍 : %s 分" % score_1) plt.imshow(image_1) plt.axis('off') plt.subplot(242).set_title("翻拍 : %s 分" % score_2) plt.imshow(image_2) plt.axis('off') plt.subplot(243).set_title("无翻拍 : %s 分" % score_3) plt.imshow(image_3) plt.axis('off') plt.subplot(244).set_title("无翻拍-黑白 : %s 分" % score_4) plt.imshow(image_4) plt.axis('off') plt.subplot(245).set_title("电脑拍摄 : %s 分" % score_5) plt.imshow(image_5) plt.axis('off') plt.subplot(246).set_title("手机拍摄 : %s 分" % score_6) plt.imshow(image_6) plt.axis('off') plt.subplot(247).set_title("电脑拍摄-翻拍 : %s 分" % score_7) plt.imshow(image_7) plt.axis('off') plt.subplot(248).set_title("手机拍摄-翻拍 : %s 分" % score_8) plt.imshow(image_8) plt.axis('off') plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1) plt.axis('off') plt.suptitle("腾讯-活体检测") plt.show()
def my_2p5d_plot_non_dominated_fronts(points, marker='o', comp=[0, 1], up_to_rank_no=1, no_text=True, ax=None, fig=None, no_colorbar=False, z_filter=None, label=None, bool_return_auto_optimal_design=False): # this is adapted from pygmo package but there is a bug therein so I write my own function (I also initiated an issue at their Github page and they acknowledge the issue). # from pylab import mpl # mpl.rcParams['font.family'] = ['Times New Roman'] # mpl.rcParams['font.size'] = 16.0 full_comp = [0, 1, 2] full_comp.remove(comp[0]) full_comp.remove(comp[1]) z_comp = full_comp[0] # We plot # fronts, dl, dc, ndr = pg.fast_non_dominated_sorting(points) fronts, _, _, _ = pg.fast_non_dominated_sorting(points) # We define the colors of the fronts (grayscale from black to white) cl = list( zip(np.linspace(0.9, 0.1, len(fronts)), np.linspace(0.9, 0.1, len(fronts)), np.linspace(0.9, 0.1, len(fronts)))) if ax is None: fig, ax = plt.subplots(constrained_layout=False) plt.subplots_adjust(left=None, bottom=None, right=0.85, top=None, wspace=None, hspace=None) count = 0 for ndr, front in enumerate(fronts): count += 1 # Frist compute the points coordinates x_scale = 1 y_scale = 1 z_scale = 1 if comp[0] == 1: # efficency x_scale = 100 if comp[1] == 1: # efficency y_scale = 100 if z_comp == 1: # efficency z_scale = 100 x = [points[idx][comp[0]] * x_scale for idx in front] y = [points[idx][comp[1]] * y_scale for idx in front] z = [points[idx][z_comp] * z_scale for idx in front] # # We plot the points # for idx in front: # ax.plot(points[idx][comp[0]], points[idx][comp[1]], marker=marker, color=cl[ndr]) # Then sort them by the first objective tmp = [(a, b, c) for a, b, c in zip(x, y, z)] tmp = sorted(tmp, key=lambda k: k[0]) # Now plot using step ax.step([coords[0] for coords in tmp], [coords[1] for coords in tmp], color=cl[ndr], where='post') # Now add color according to the value of the z-axis variable usign scatter if z_filter is not None: z = np.array(z) x = np.array(x)[z < z_filter] y = np.array(y)[z < z_filter] z = z[z < z_filter] print('Cost, -Efficency, Ripple Sum') min_a_design = None min_a_value = 99999999.0 min_b_design = None min_b_value = 99999999.0 min_c_design = None min_c_value = 99999999.0 for a, b, c in zip(x, y, z): if a < min_a_value: min_a_value = a min_a_design = (a, b, c) if b < min_b_value: min_b_value = b min_b_design = (a, b, c) if c < min_c_value: min_c_value = c min_c_design = (a, b, c) print(a, b, c) auto_optimal_design = (min_a_design, min_b_design, min_c_design) # scatter_handle = ax.scatter(x, y, c=z, edgecolor=None, alpha=0.5, cmap='Spectral', marker=marker, zorder=99, vmin=0, vmax=z_filter, label=label) #'viridis' Spectral scatter_handle = ax.scatter(x, y, c=z, edgecolor=None, alpha=0.5, cmap='plasma', marker=marker, zorder=99, vmin=0, vmax=z_filter, label=label) # ValueError: Colormap Option A is not recognized. Possible values are: Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, winter, winter_r # https://medium.com/better-programming/how-to-use-colormaps-with-matplotlib-to-create-colorful-plots-in-python-969b5a892f0c # Perceptually Uniform Sequential # ['viridis', 'plasma', 'inferno', 'magma'] # Sequential # ['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds', 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'] # Sequential (2) # ['binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink', 'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper'] # Diverging # ['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic'] # Qualitative # ['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c'] # Miscellaneous # ['flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv', 'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar'] else: scatter_handle = ax.scatter(x, y, c=z, edgecolor=None, alpha=0.5, cmap='Spectral', marker=marker, zorder=99) #'viridis' if not no_colorbar: # color bar cbar_ax = fig.add_axes([0.875, 0.15, 0.02, 0.7]) cbar_ax.get_yaxis().labelpad = 10 clb = fig.colorbar(scatter_handle, cax=cbar_ax) else: pass # use vmin/vmax or set_clim # if comp[0] == 0 and comp[1] == 1: # min_, max_ = 0, 20 # Only implemented for Ripple Sum # scatter_handle.set_clim(min_, max_) # https://stackoverflow.com/questions/3373256/set-colorbar-range-in-matplotlib # # # # color bar # # # cbar_ax = fig.add_axes([0.875, 0.15, 0.02, 0.7]) # # # cbar_ax.get_yaxis().labelpad = 10 # # # clb = fig.colorbar(scatter_handle, cax=cbar_ax) # # # clb.ax.set_ylabel('Ripple Sum [1]', rotation=270) # else: # raise Exception('Not Implemented') if z_comp == 0: z_label = r'$\rm {Cost}$ [USD]' z_text = '%.0f' elif z_comp == 1: z_label = r'$-\eta$ [%]' z_text = '%.1f' elif z_comp == 2: z_label = r'$O_C$ [1]' z_text = '%.1f' if not no_colorbar: clb.ax.set_ylabel(z_label, rotation=270) if z_comp == 2: # when OC as z-axis print('-----------------------------------------------------') print('-----------------------------------------------------') print('-----------------------------------------------------') # Add index next to the points for x_coord, y_coord, z_coord, idx in zip(x, y, z, front): if no_text: pass else: ax.annotate(z_text % (z_coord) + ' #%d' % (idx), (x_coord, y_coord)) else: # text next scatter showing the value of the 3rd objective for i, val in enumerate(z): if no_text: pass else: ax.annotate(z_text % (val), (x[i], y[i])) # refine the plotting if comp[0] == 0: ax.set_xlabel(r'$\rm {Cost}$ [USD]') elif comp[0] == 1: ax.set_xlabel(r'$-\eta$ [\%]') elif comp[0] == 2: ax.set_xlabel(r'$O_C$ [1]') if comp[1] == 0: ax.set_ylabel(r'$\rm {Cost}$ [USD]') elif comp[1] == 1: ax.set_ylabel(r'$-\eta$ [\%]') elif comp[1] == 2: ax.set_ylabel(r'$O_C$ [1]') ax.grid() # plot up to which domination rank? if up_to_rank_no is None: pass else: if count >= up_to_rank_no: break # Y730 # fig.savefig(r'C:\Users\horyc\Desktop/'+ '2p5D-%d%d.png'%(comp[0],comp[1]), dpi=300) if bool_return_auto_optimal_design: return scatter_handle, auto_optimal_design else: return scatter_handle
def plot_mean_curve(time_data, data, additions, plate_name, save_folder = r'Figures/'): """ """ if not os.path.isdir(save_folder): os.makedirs(save_folder) for block in data: # group = group_similar(data[block].keys()) names = data[block].keys() names.sort() # ----------------- # Plot each group individualy # ----------------- data_per_inch = 5.0 y_len = int(math.sqrt(len(names))) x_len = int(np.ceil(len(names) / float(y_len))) fig, axs = plt.subplots(y_len, x_len, sharex=True, sharey=True, figsize=(x_len*data_per_inch,y_len*data_per_inch), dpi=300) y_min = data[block][names[0]]['original_data'].min() y_max = data[block][names[0]]['original_data'].max() for i, name in enumerate(names): if data[block][name]['original_data'].min() < y_min: y_min = data[block][name]['original_data'].min() if data[block][name]['original_data'].max() > y_max: y_max = data[block][name]['original_data'].max() y_range = y_max - y_min for i, name in enumerate(names): px = i / x_len py = i % x_len if y_len > 1: position = (px, py) else: position = py time = time_data[block][name] mean = np.array(data[block][name]['mean']) std = np.array(data[block][name]['std']) all_curve = data[block][name]['original_data'] axs[position].plot(time, [1.0 for _ in time], 'k-') for curve in all_curve: axs[position].plot(time, curve, color = [0.7 for _ in xrange(3)]) axs[position].plot(time, mean, color = [0.2 for _ in xrange(3)], linewidth = 2.5, label = name) axs[position].plot(time, mean+std, 'k--', color = [0.2 for _ in xrange(3)], linewidth = 2.5) axs[position].plot(time, mean-std, 'k--', color = [0.2 for _ in xrange(3)], linewidth = 2.5) for addition in additions[:block+1]: axs[position].plot([addition[0] for _ in xrange(2)], [0.0,3.0], 'k-') axs[position].text(addition[0], 0.98, addition[1], rotation=270) if i % x_len == 0: axs[position].set_ylabel('Normalized fluorescence', size = 14) if i / x_len == y_len - 1: axs[position].set_xlabel('Time / sec', size = 14) axs[position].set_xlim(0, time[-1]) for j, text in enumerate(name.split(';')): axs[position].text(10, y_max - y_range*j*0.1, text, size = 14) axs[position].set_ylim(y_min-y_range*0.1, y_max+y_range*0.1) # plt.suptitle(plate_name) plt.subplots_adjust(wspace = 0.01, hspace = 0.01) plt.tight_layout() plt.savefig(save_folder + 'block_' + str(block + 1)) # return None
def get_linear_model_histogram(code, ptype='low', dtype='d', start=None, end=None, vtype='f', filter='n', df=None): # 399001','cyb':'zs399006','zxb':'zs399005 # code = '999999' # code = '601608' # code = '000002' # asset = get_kdate_data(code)['close'].sort_index(ascending=True) # df = tdd.get_tdx_Exp_day_to_df(code, 'f').sort_index(ascending=True) # ptype='close' # if ptype == 'close' or ptype=='' # ptype= if start is not None and filter == 'y': if code not in ['999999', '399006', '399001']: index_d, dl = tdd.get_duration_Index_date(dt=start) log.debug("index_d:%s dl:%s" % (str(index_d), dl)) else: index_d = cct.day8_to_day10(start) log.debug("index_d:%s" % (index_d)) start = tdd.get_duration_price_date(code, ptype='low', dt=index_d) log.debug("start:%s" % (start)) if df is None: # df = tdd.get_tdx_append_now_df(code, ptype, start, end).sort_index(ascending=True) df = tdd.get_tdx_append_now_df_api(code, start, end).sort_index(ascending=True) if not dtype == 'd': df = tdd.get_tdx_stock_period_to_type(df, dtype).sort_index(ascending=True) asset = df[ptype] log.info("df:%s" % asset[:1]) asset = asset.dropna() dates = asset.index if not code.startswith('999') and not code.startswith('399'): # print "code:",code if code[:1] in ['5', '6', '9']: code2 = '999999' elif code[:2] in ['30']: # print "cyb" code2 = '399006' else: code2 = '399001' df1 = tdd.get_tdx_append_now_df_api(code2, start, end).sort_index(ascending=True) # df1 = tdd.get_tdx_append_now_df(code2, ptype, start, end).sort_index(ascending=True) if not dtype == 'd': df1 = tdd.get_tdx_stock_period_to_type( df1, dtype).sort_index(ascending=True) # if len(asset) < len(df1): # asset1 = df1.loc[asset.index, ptype] # else: # asset1 = df1.loc[asset.index, ptype] # startv = asset1[:1] # asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2)) # print asset[:1].index[0] , df1[:1].index[0] if asset[:1].index[0] > df1[:1].index[0]: asset1 = df1.loc[asset.index, ptype] startv = asset1[:1] asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2)) else: df = df[df.index >= df1.index[0]] asset = df[ptype] asset = asset.dropna() dates = asset.index asset1 = df1.loc[df.index, ptype] asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2)) else: if code.startswith('399001'): code2 = '999999' elif code.startswith('399006'): code2 = '399005' else: code2 = '399001' df1 = tdd.get_tdx_append_now_df_api(code2, start, end).sort_index(ascending=True) # print df1[:1] # df1 = tdd.get_tdx_append_now_df(code2, ptype, start, end).sort_index(ascending=True) if not dtype == 'd': df1 = tdd.get_tdx_stock_period_to_type( df1, dtype).sort_index(ascending=True) if len(asset) < len(df1): asset1 = df1.loc[asset.index, ptype] asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2)) else: df = df[df.index >= df1.index[0]] asset = df[ptype] asset = asset.dropna() dates = asset.index asset1 = df1.loc[df.index, ptype] asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2)) # print len(df),len(asset),len(df1),len(asset1) if end is not None: # print asset[-1:] asset = asset[:-1] dates = asset.index asset1 = asset1[:-1] asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2)) # 画出价格随时间变化的图像 # _, ax = plt.subplots() # fig = plt.figure() fig = plt.figure(figsize=(16, 5)) # fig = plt.figure(figsize=(16, 10), dpi=72) # fig.autofmt_xdate() #(no fact) # plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9) plt.subplots_adjust(left=0.05, bottom=0.08, right=0.95, top=0.95, wspace=0.15, hspace=0.25) # set (gca,'Position',[0,0,512,512]) # fig.set_size_inches(18.5, 10.5) # fig=plt.fig(figsize=(14,8)) ax1 = fig.add_subplot(121) # asset=asset.apply(lambda x:round( x/asset[:1],2)) ax1.plot(asset) # ax1.plot(asset1,'-r', linewidth=2) ticks = ax1.get_xticks() # start, end = ax1.get_xlim() # print start, end, len(asset) # print ticks, ticks[:-1] # (ticks[:-1] if len(asset) > end else np.append(ticks[:-1], len(asset) - 1)) ax1.set_xticklabels( [dates[int(i)] for i in (np.append(ticks[:-1], len(asset) - 1))], rotation=15) # Label x-axis with dates # 拟合 X = np.arange(len(asset)) x = sm.add_constant(X) model = regression.linear_model.OLS(asset, x).fit() a = model.params[0] b = model.params[1] # log.info("a:%s b:%s" % (a, b)) log.info("X:%s a:%s b:%s" % (len(asset), a, b)) Y_hat = X * b + a # 真实值-拟合值,差值最大最小作为价值波动区间 # 向下平移 i = (asset.values.T - Y_hat).argmin() c_low = X[i] * b + a - asset.values[i] Y_hatlow = X * b + a - c_low # 向上平移 i = (asset.values.T - Y_hat).argmax() c_high = X[i] * b + a - asset.values[i] Y_hathigh = X * b + a - c_high plt.plot(X, Y_hat, 'k', alpha=0.9) plt.plot(X, Y_hatlow, 'r', alpha=0.9) plt.plot(X, Y_hathigh, 'r', alpha=0.9) # plt.xlabel('Date', fontsize=12) plt.ylabel('Price', fontsize=12) plt.title(code + " | " + str(dates[-1])[:11], fontsize=14) plt.legend([asset.iat[-1]], fontsize=12, loc=4) plt.grid(True) # plt.legend([code]); # plt.legend([code, 'Value center line', 'Value interval line']); # fig=plt.fig() # fig.figsize = [14,8] scale = 1.1 zp = zoompan.ZoomPan() figZoom = zp.zoom_factory(ax1, base_scale=scale) figPan = zp.pan_factory(ax1) # 将Y-Y_hat股价偏离中枢线的距离单画出一张图显示,对其边界线之间的区域进行均分,大于0的区间为高估,小于0的区间为低估,0为价值中枢线。 ax3 = fig.add_subplot(122) # distance = (asset.values.T - Y_hat) distance = (asset.values.T - Y_hat)[0] # if code.startswith('999') or code.startswith('399'): if len(asset) > len(df1): ax3.plot(asset) plt.plot(distance) ticks = ax3.get_xticks() ax3.set_xticklabels( [dates[int(i)] for i in (np.append(ticks[:-1], len(asset) - 1))], rotation=15) n = 5 d = (-c_high + c_low) / n c = c_high while c <= c_low: Y = X * b + a - c plt.plot(X, Y - Y_hat, 'r', alpha=0.9) c = c + d ax3.plot(asset) ## plt.xlabel('Date', fontsize=12) plt.ylabel('Price-center price', fontsize=14) plt.grid(True) else: as3 = asset.apply(lambda x: round(x / asset[:1], 2)) ax3.plot(as3) ax3.plot(asset1, '-r', linewidth=2) # assvol = df.loc[asset.index]['vol'] # assvol = assvol.apply(lambda x: round(x / assvol[:1], 2)) # ax3.plot(assvol, '-g', linewidth=2) plt.grid(True) zp3 = zoompan.ZoomPan() figZoom = zp3.zoom_factory(ax3, base_scale=scale) figPan = zp3.pan_factory(ax3) # plt.title(code, fontsize=14) if 'name' in df.columns: plt.legend([df.name[-1], df1.name[-1]], loc=0) else: dm = tdd.get_sina_data_df(code) if 'name' in dm.columns: cname = dm.name[0] else: cname = '-' # plt.legend([code, code2], loc=0) plt.legend([cname, code2], loc=0) plt.show(block=False)
plt.ylabel('Counts') plt.savefig(output_dir + "/automatic/defoci.tif") plt.close() qval_min = min(qvals) qval_max = max(qvals) qval_n_bins = 40 hist(qvals, bins=qval_n_bins, range=(qval_min,qval_max), facecolor='red', alpha=0.5) plt.title('QVal Histogram') plt.xlabel('QVal') plt.ylabel('Counts') plt.savefig(output_dir + "/automatic/qval.tif") plt.close() plt.subplots_adjust(hspace=0.6) if len(qvals_pertilt[0])>0: plt.subplot(411) hist(qvals_pertilt[0], bins=qval_n_bins, range=(qval_min,qval_max), facecolor='red', alpha=0.5) plt.title('QVAL Histogram (0-10 degrees)') plt.ylabel('Counts') if len(qvals_pertilt[1])==0 and len(qvals_pertilt[2])==0 and len(qvals_pertilt[3])==0: plt.xlabel('QVAL') if len(qvals_pertilt[1])>0: plt.subplot(412) hist(qvals_pertilt[1], bins=qval_n_bins, range=(qval_min,qval_max), facecolor='red', alpha=0.5) plt.title('QVAL Histogram (10-20 degrees)') plt.ylabel('Counts') if len(qvals_pertilt[2])==0 and len(qvals_pertilt[3])==0: plt.xlabel('QVAL') if len(qvals_pertilt[2])>0:
def my_2p5d_plot_non_dominated_fronts(points, marker='o', comp=[0, 1], up_to_rank_no=1, no_text=True): # from pylab import mpl # mpl.rcParams['font.family'] = ['Times New Roman'] # mpl.rcParams['font.size'] = 16.0 full_comp = [0, 1, 2] full_comp.remove(comp[0]) full_comp.remove(comp[1]) z_comp = full_comp[0] # We plot # fronts, dl, dc, ndr = pg.fast_non_dominated_sorting(points) fronts, _, _, _ = pg.fast_non_dominated_sorting(points) # We define the colors of the fronts (grayscale from black to white) cl = list( zip(np.linspace(0.9, 0.1, len(fronts)), np.linspace(0.9, 0.1, len(fronts)), np.linspace(0.9, 0.1, len(fronts)))) fig, ax = plt.subplots(constrained_layout=False) plt.subplots_adjust(left=None, bottom=None, right=0.85, top=None, wspace=None, hspace=None) count = 0 for ndr, front in enumerate(fronts): count += 1 # Frist compute the points coordinates x_scale = 1 y_scale = 1 z_scale = 1 if comp[0] == 1: # efficency x_scale = 100 if comp[1] == 1: # efficency y_scale = 100 if z_comp == 1: # efficency z_scale = 100 x = [points[idx][comp[0]] * x_scale for idx in front] y = [points[idx][comp[1]] * y_scale for idx in front] z = [points[idx][z_comp] * z_scale for idx in front] # # We plot the points # for idx in front: # ax.plot(points[idx][comp[0]], points[idx][comp[1]], marker=marker, color=cl[ndr]) # Then sort them by the first objective tmp = [(a, b, c) for a, b, c in zip(x, y, z)] tmp = sorted(tmp, key=lambda k: k[0]) # Now plot using step ax.step([coords[0] for coords in tmp], [coords[1] for coords in tmp], color=cl[ndr], where='post') # Now add color according to the value of the z-axis variable usign scatter scatter_handle = ax.scatter(x, y, c=z, alpha=0.5, cmap='Spectral', marker=marker, zorder=99) #'viridis' # color bar cbar_ax = fig.add_axes([0.875, 0.15, 0.02, 0.7]) cbar_ax.get_yaxis().labelpad = 10 clb = fig.colorbar(scatter_handle, cax=cbar_ax) if z_comp == 0: z_label = r'$-\rm {TRV}$ [Nm/m^3]' z_text = '%.0f' elif z_comp == 1: z_label = r'$-\eta$ [%]' z_text = '%.1f' elif z_comp == 2: z_label = r'$O_C$ [1]' z_text = '%.1f' clb.ax.set_ylabel(z_label, rotation=270) if z_comp == 2: # when OC as z-axis print('-----------------------------------------------------') print('-----------------------------------------------------') print('-----------------------------------------------------') # Add index next to the points for x_coord, y_coord, z_coord, idx in zip(x, y, z, front): if no_text: pass else: ax.annotate(z_text % (z_coord) + ' #%d' % (idx), (x_coord, y_coord)) else: # text next scatter showing the value of the 3rd objective for i, val in enumerate(z): if no_text: pass else: ax.annotate(z_text % (val), (x[i], y[i])) # refine the plotting if comp[0] == 0: ax.set_xlabel(r'$-\rm {TRV}$ [Nm/m^3]') elif comp[0] == 1: ax.set_xlabel(r'$-\eta$ [%]') elif comp[0] == 2: ax.set_xlabel(r'$O_C$ [1]') if comp[1] == 0: ax.set_ylabel(r'$-\rm {TRV}$ [Nm/m^3]') elif comp[1] == 1: ax.set_ylabel(r'$-\eta$ [%]') elif comp[1] == 2: ax.set_ylabel(r'$O_C$ [1]') ax.grid() # plot up to which domination rank? if up_to_rank_no is None: pass else: if count >= up_to_rank_no: break # Y730 # fig.savefig(r'C:\Users\horyc\Desktop/'+ '2p5D-%d%d.png'%(comp[0],comp[1]), dpi=300) return ax
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)
def show_patches(): dirname = C.TRAIN.SAMPLES.DIR labels = EasyDict(pos=EasyDict(), neg=EasyDict()) labels.pos.samples = pd.read_csv(os.path.join(dirname, 'positives.csv')) labels.pos.index = 0 labels.neg.samples = pd.read_csv(os.path.join(dirname, 'negatives.csv')) labels.neg.index = 0 labels.label = 'pos' fig = plt.figure() ax1 = plt.subplot(131) ax2 = plt.subplot(132) ax3 = plt.subplot(133) plt.subplots_adjust(bottom=0.15) # Slider to choose the sample ax_slider = plt.axes([0.25, 0.05, 0.65, 0.05], facecolor='lightgoldenrodyellow') slider = Slider(ax_slider, 'Index', 0, len(labels[labels.label].samples), valinit=labels[labels.label].index, valstep=1, closedmax=False) # Radio buttons to choose the label ax_label = plt.axes([0.025, 0.05, 0.1, 0.1]) radio_buttons = RadioButtons(ax_label, ['pos', 'neg'], active=0) ax_help = plt.axes([0.25, 0.10, 0.65, 0.05]) ax_help.axis('off') ax_help.text(0, 0.5, 'Press [j,k] to change the index, [p,n] to set the label.') def update(dummy): if str(radio_buttons.value_selected) != labels.label: labels.label = radio_buttons.value_selected slider.valmax = len(labels[labels.label].samples) slider.val = labels[labels.label].index ax_slider.set_xlim(0, slider.valmax) labels[labels.label].index = int(slider.val) current = labels[labels.label].samples.iloc[labels[labels.label].index] with open(os.path.join(dirname, current['name']), 'rb') as f: patch = pickle.load(f) print(f.name) plt.suptitle(patch.name) vol = patch.volumetric[2:2 + 3].transpose(1, 2, 0) display_vol = 2 * np.arctan(vol) / np.pi radius = patch.rgb.shape[1] / 2 extent = (-radius, radius, -radius, radius) ax1.clear() ax2.clear() ax3.clear() ax1.imshow(patch.rgb.transpose(1, 2, 0), extent=extent) ax1.set_title('rgb') ax2.imshow(patch.rgb.transpose(1, 2, 0), extent=extent) ax2.imshow(display_vol, extent=extent, alpha=0.5) if patch.obb is not None: patch.obb.plot(ax2, lw=4, color='yellow') patch.obb.plot(ax2, lw=3, ls='--', color='red') ax2.set_title('both') ax3.imshow(display_vol, extent=extent) ax3.set_title('vol:max={:.1f}'.format(vol.max())) fig.canvas.draw_idle() # First plot update(0) radio_buttons.on_clicked(update) slider.on_changed(update) def keypress(event): if event.key == 'j': slider.set_val(slider.val - 1) elif event.key == 'k': slider.set_val(slider.val + 1) elif event.key == 'p': radio_buttons.set_active(0) elif event.key == 'n': radio_buttons.set_active(1) fig.canvas.mpl_connect('key_press_event', keypress) plt.show()
def get_linear_model_histogramDouble(code, ptype='f', dtype='d', start=None, end=None, vtype='close', filter='n', df=None): # 399001','cyb':'zs399006','zxb':'zs399005 # code = '999999' # code = '601608' # code = '000002' # asset = ts.get_hist_data(code)['close'].sort_index(ascending=True) # df = tdd.get_tdx_Exp_day_to_df(code, 'f').sort_index(ascending=True) # vtype='close' # if vtype == 'close' or vtype=='' # ptype= if start is not None and filter == 'y': if code not in ['999999', '399006', '399001']: index_d, dl = tdd.get_duration_Index_date(dt=start) log.debug("index_d:%s dl:%s" % (str(index_d), dl)) else: index_d = cct.day8_to_day10(start) log.debug("index_d:%s" % (index_d)) start = tdd.get_duration_price_date(code, ptype='low', dt=index_d) log.debug("start:%s" % (start)) if df is None: # df = tdd.get_tdx_append_now_df(code, ptype, start, end).sort_index(ascending=True) df = tdd.get_tdx_append_now_df_api(code, ptype, start, end).sort_index(ascending=True) if not dtype == 'd': df = tdd.get_tdx_stock_period_to_type(df, dtype).sort_index(ascending=True) asset = df[vtype] log.info("df:%s" % asset[:1]) asset = asset.dropna() dates = asset.index if not code.startswith('999') or not code.startswith('399'): if code[:1] in ['5', '6', '9']: code2 = '999999' elif code[:1] in ['3']: code2 = '399006' else: code2 = '399001' df1 = tdd.get_tdx_append_now_df_api(code2, ptype, start, end).sort_index(ascending=True) # df1 = tdd.get_tdx_append_now_df(code2, ptype, start, end).sort_index(ascending=True) if not dtype == 'd': df1 = tdd.get_tdx_stock_period_to_type(df1, dtype).sort_index(ascending=True) asset1 = df1.loc[asset.index, vtype] startv = asset1[:1] # asset_v=asset[:1] # print startv,asset_v asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2)) # print asset1[:4] # 画出价格随时间变化的图像 # _, ax = plt.subplots() # fig = plt.figure() fig = plt.figure(figsize=(16, 10)) # fig = plt.figure(figsize=(16, 10), dpi=72) # fig.autofmt_xdate() #(no fact) # plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9) plt.subplots_adjust(left=0.05, bottom=0.08, right=0.95, top=0.95, wspace=0.15, hspace=0.25) # set (gca,'Position',[0,0,512,512]) # fig.set_size_inches(18.5, 10.5) # fig=plt.fig(figsize=(14,8)) ax1 = fig.add_subplot(321) # asset=asset.apply(lambda x:round( x/asset[:1],2)) ax1.plot(asset) # ax1.plot(asset1,'-r', linewidth=2) ticks = ax1.get_xticks() # start, end = ax1.get_xlim() # print start, end, len(asset) # print ticks, ticks[:-1] # (ticks[:-1] if len(asset) > end else np.append(ticks[:-1], len(asset) - 1)) ax1.set_xticklabels([dates[i] for i in (np.append(ticks[:-1], len(asset) - 1))], rotation=15) # Label x-axis with dates # 拟合 X = np.arange(len(asset)) x = sm.add_constant(X) model = regression.linear_model.OLS(asset, x).fit() a = model.params[0] b = model.params[1] # log.info("a:%s b:%s" % (a, b)) log.info("X:%s a:%s b:%s" % (len(asset), a, b)) Y_hat = X * b + a # 真实值-拟合值,差值最大最小作为价值波动区间 # 向下平移 i = (asset.values.T - Y_hat).argmin() c_low = X[i] * b + a - asset.values[i] Y_hatlow = X * b + a - c_low # 向上平移 i = (asset.values.T - Y_hat).argmax() c_high = X[i] * b + a - asset.values[i] Y_hathigh = X * b + a - c_high plt.plot(X, Y_hat, 'k', alpha=0.9); plt.plot(X, Y_hatlow, 'r', alpha=0.9); plt.plot(X, Y_hathigh, 'r', alpha=0.9); # plt.xlabel('Date', fontsize=12) plt.ylabel('Price', fontsize=12) plt.title(code + " | " + str(dates[-1])[:11], fontsize=14) plt.legend([asset.iat[-1]], fontsize=12, loc=4) plt.grid(True) # plt.legend([code]); # plt.legend([code, 'Value center line', 'Value interval line']); # fig=plt.fig() # fig.figsize = [14,8] scale = 1.1 zp = zoompan.ZoomPan() figZoom = zp.zoom_factory(ax1, base_scale=scale) figPan = zp.pan_factory(ax1) ax2 = fig.add_subplot(323) # ax2.plot(asset) # ticks = ax2.get_xticks() ax2.set_xticklabels([dates[i] for i in (np.append(ticks[:-1], len(asset) - 1))], rotation=15) # plt.plot(X, Y_hat, 'k', alpha=0.9) n = 5 d = (-c_high + c_low) / n c = c_high while c <= c_low: Y = X * b + a - c plt.plot(X, Y, 'r', alpha=0.9); c = c + d # asset=asset.apply(lambda x:round(x/asset[:1],2)) ax2.plot(asset) # ax2.plot(asset1,'-r', linewidth=2) # plt.xlabel('Date', fontsize=12) plt.ylabel('Price', fontsize=12) plt.grid(True) # plt.title(code, fontsize=14) # plt.legend([code]) # 将Y-Y_hat股价偏离中枢线的距离单画出一张图显示,对其边界线之间的区域进行均分,大于0的区间为高估,小于0的区间为低估,0为价值中枢线。 ax3 = fig.add_subplot(322) # distance = (asset.values.T - Y_hat) distance = (asset.values.T - Y_hat)[0] if code.startswith('999') or code.startswith('399'): ax3.plot(asset) plt.plot(distance) ticks = ax3.get_xticks() ax3.set_xticklabels([dates[i] for i in (np.append(ticks[:-1], len(asset) - 1))], rotation=15) n = 5 d = (-c_high + c_low) / n c = c_high while c <= c_low: Y = X * b + a - c plt.plot(X, Y - Y_hat, 'r', alpha=0.9); c = c + d ax3.plot(asset) # plt.xlabel('Date', fontsize=12) plt.ylabel('Price-center price', fontsize=14) plt.grid(True) else: as3 = asset.apply(lambda x: round(x / asset[:1], 2)) ax3.plot(as3) ax3.plot(asset1, '-r', linewidth=2) plt.grid(True) zp3 = zoompan.ZoomPan() figZoom = zp3.zoom_factory(ax3, base_scale=scale) figPan = zp3.pan_factory(ax3) # plt.title(code, fontsize=14) # plt.legend([code]) # 统计出每个区域内各股价的频数,得到直方图,为了更精细的显示各个区域的频数,这里将整个边界区间分成100份。 ax4 = fig.add_subplot(325) log.info("assert:len:%s %s" % (len(asset.values.T - Y_hat), (asset.values.T - Y_hat)[0])) # distance = map(lambda x:int(x),(asset.values.T - Y_hat)/Y_hat*100) # now_distanse=int((asset.iat[-1]-Y_hat[-1])/Y_hat[-1]*100) # log.debug("dis:%s now:%s"%(distance[:2],now_distanse)) # log.debug("now_distanse:%s"%now_distanse) distance = (asset.values.T - Y_hat) now_distanse = asset.iat[-1] - Y_hat[-1] # distance = (asset.values.T-Y_hat)[0] pd.Series(distance).plot(kind='hist', stacked=True, bins=100) # plt.plot((asset.iat[-1].T-Y_hat),'b',alpha=0.9) plt.axvline(now_distanse, hold=None, label="1", color='red') # plt.axhline(now_distanse,hold=None,label="1",color='red') # plt.axvline(asset.iat[0],hold=None,label="1",color='red',linestyle="--") plt.xlabel('Undervalue ------------------------------------------> Overvalue', fontsize=12) plt.ylabel('Frequency', fontsize=14) # plt.title('Undervalue & Overvalue Statistical Chart', fontsize=14) plt.legend([code, asset.iat[-1], str(dates[-1])[5:11]], fontsize=12) plt.grid(True) # plt.show() # import os # print(os.path.abspath(os.path.curdir)) ax5 = fig.add_subplot(326) # fig.figsize=(5, 10) log.info("assert:len:%s %s" % (len(asset.values.T - Y_hat), (asset.values.T - Y_hat)[0])) # distance = map(lambda x:int(x),(asset.values.T - Y_hat)/Y_hat*100) distance = (asset.values.T - Y_hat) / Y_hat * 100 now_distanse = ((asset.iat[-1] - Y_hat[-1]) / Y_hat[-1] * 100) log.debug("dis:%s now:%s" % (distance[:2], now_distanse)) log.debug("now_distanse:%s" % now_distanse) # n, bins = np.histogram(distance, 50) # print n, bins[:2] pd.Series(distance).plot(kind='hist', stacked=True, bins=100) # plt.plot((asset.iat[-1].T-Y_hat),'b',alpha=0.9) plt.axvline(now_distanse, hold=None, label="1", color='red') # plt.axhline(now_distanse,hold=None,label="1",color='red') # plt.axvline(asset.iat[0],hold=None,label="1",color='red',linestyle="--") plt.xlabel('Undervalue ------------------------------------------> Overvalue', fontsize=14) plt.ylabel('Frequency', fontsize=12) # plt.title('Undervalue & Overvalue Statistical Chart', fontsize=14) plt.legend([code, asset.iat[-1]], fontsize=12) plt.grid(True) ax6 = fig.add_subplot(324) h = df.loc[:, ['open', 'close', 'high', 'low']] highp = h['high'].values lowp = h['low'].values openp = h['open'].values closep = h['close'].values lr = LinearRegression() x = np.atleast_2d(np.linspace(0, len(closep), len(closep))).T lr.fit(x, closep) LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False) xt = np.atleast_2d(np.linspace(0, len(closep) + 200, len(closep) + 200)).T yt = lr.predict(xt) bV = [] bP = [] for i in range(1, len(highp) - 1): if highp[i] <= highp[i - 1] and highp[i] < highp[i + 1] and lowp[i] <= lowp[i - 1] and lowp[i] < lowp[i + 1]: bV.append(lowp[i]) bP.append(i) d, p = LIS(bV) idx = [] for i in range(len(p)): idx.append(bP[p[i]]) lr = LinearRegression() X = np.atleast_2d(np.array(idx)).T Y = np.array(d) lr.fit(X, Y) estV = lr.predict(xt) ax6.plot(closep, linewidth=2) ax6.plot(idx, d, 'ko') ax6.plot(xt, estV, '-r', linewidth=3) ax6.plot(xt, yt, '-g', linewidth=3) plt.grid(True) # plt.tight_layout() zp2 = zoompan.ZoomPan() figZoom = zp2.zoom_factory(ax6, base_scale=scale) figPan = zp2.pan_factory(ax6) # plt.ion() plt.show(block=False)
def get_linear_model_histogram(code, ptype="f", dtype="d", start=None, end=None): # 399001','cyb':'zs399006','zxb':'zs399005 # code = '999999' # code = '601608' # code = '000002' # asset = ts.get_hist_data(code)['close'].sort_index(ascending=True) # df = tdd.get_tdx_Exp_day_to_df(code, 'f').sort_index(ascending=True) df = tdd.get_tdx_append_now_df(code, ptype, start, end).sort_index(ascending=True) if not dtype == "d": df = tdd.get_tdx_stock_period_to_type(df, dtype).sort_index(ascending=True) asset = df["close"] log.info("df:%s" % asset[:1]) asset = asset.dropna() dates = asset.index if not code.startswith("999") or not code.startswith("399"): if code[:1] in ["5", "6", "9"]: code2 = "999999" elif code[:1] in ["3"]: code2 = "399006" else: code2 = "399001" df1 = tdd.get_tdx_append_now_df(code2, ptype, start, end).sort_index(ascending=True) if not dtype == "d": df1 = tdd.get_tdx_stock_period_to_type(df1, dtype).sort_index(ascending=True) asset1 = df1.loc[asset.index, "close"] startv = asset1[:1] # asset_v=asset[:1] # print startv,asset_v asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2)) # print asset1[:4] # 画出价格随时间变化的图像 # _, ax = plt.subplots() # fig = plt.figure() fig = plt.figure(figsize=(16, 10)) # fig = plt.figure(figsize=(16, 10), dpi=72) # plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9) plt.subplots_adjust(left=0.05, bottom=0.08, right=0.95, top=0.95, wspace=0.15, hspace=0.25) # set (gca,'Position',[0,0,512,512]) # fig.set_size_inches(18.5, 10.5) # fig=plt.fig(figsize=(14,8)) ax1 = fig.add_subplot(321) # asset=asset.apply(lambda x:round( x/asset[:1],2)) ax1.plot(asset) # ax1.plot(asset1,'-r', linewidth=2) ticks = ax1.get_xticks() ax1.set_xticklabels([dates[i] for i in ticks[:-1]]) # Label x-axis with dates # 拟合 X = np.arange(len(asset)) x = sm.add_constant(X) model = regression.linear_model.OLS(asset, x).fit() a = model.params[0] b = model.params[1] # log.info("a:%s b:%s" % (a, b)) log.info("X:%s a:%s b:%s" % (len(asset), a, b)) Y_hat = X * b + a # 真实值-拟合值,差值最大最小作为价值波动区间 # 向下平移 i = (asset.values.T - Y_hat).argmin() c_low = X[i] * b + a - asset.values[i] Y_hatlow = X * b + a - c_low # 向上平移 i = (asset.values.T - Y_hat).argmax() c_high = X[i] * b + a - asset.values[i] Y_hathigh = X * b + a - c_high plt.plot(X, Y_hat, "k", alpha=0.9) plt.plot(X, Y_hatlow, "r", alpha=0.9) plt.plot(X, Y_hathigh, "r", alpha=0.9) plt.xlabel("Date", fontsize=14) plt.ylabel("Price", fontsize=14) plt.title(code, fontsize=14) plt.grid(True) # plt.legend([code]); # plt.legend([code, 'Value center line', 'Value interval line']); # fig=plt.fig() # fig.figsize = [14,8] scale = 1.1 zp = zoompan.ZoomPan() figZoom = zp.zoom_factory(ax1, base_scale=scale) figPan = zp.pan_factory(ax1) ax2 = fig.add_subplot(323) ticks = ax2.get_xticks() ax2.set_xticklabels([dates[i] for i in ticks[:-1]]) # plt.plot(X, Y_hat, 'k', alpha=0.9) n = 5 d = (-c_high + c_low) / n c = c_high while c <= c_low: Y = X * b + a - c plt.plot(X, Y, "r", alpha=0.9) c = c + d # asset=asset.apply(lambda x:round(x/asset[:1],2)) ax2.plot(asset) # ax2.plot(asset1,'-r', linewidth=2) plt.xlabel("Date", fontsize=14) plt.ylabel("Price", fontsize=14) plt.grid(True) # plt.title(code, fontsize=14) # plt.legend([code]) # 将Y-Y_hat股价偏离中枢线的距离单画出一张图显示,对其边界线之间的区域进行均分,大于0的区间为高估,小于0的区间为低估,0为价值中枢线。 ax3 = fig.add_subplot(322) # distance = (asset.values.T - Y_hat) distance = (asset.values.T - Y_hat)[0] if code.startswith("999") or code.startswith("399"): ax3.plot(asset) plt.plot(distance) ticks = ax3.get_xticks() ax3.set_xticklabels([dates[i] for i in ticks[:-1]]) n = 5 d = (-c_high + c_low) / n c = c_high while c <= c_low: Y = X * b + a - c plt.plot(X, Y - Y_hat, "r", alpha=0.9) c = c + d ax3.plot(asset) plt.xlabel("Date", fontsize=14) plt.ylabel("Price-center price", fontsize=14) plt.grid(True) else: as3 = asset.apply(lambda x: round(x / asset[:1], 2)) ax3.plot(as3) ax3.plot(asset1, "-r", linewidth=2) plt.grid(True) zp3 = zoompan.ZoomPan() figZoom = zp3.zoom_factory(ax3, base_scale=scale) figPan = zp3.pan_factory(ax3) # plt.title(code, fontsize=14) # plt.legend([code]) # 统计出每个区域内各股价的频数,得到直方图,为了更精细的显示各个区域的频数,这里将整个边界区间分成100份。 ax4 = fig.add_subplot(325) log.info("assert:len:%s %s" % (len(asset.values.T - Y_hat), (asset.values.T - Y_hat)[0])) # distance = map(lambda x:int(x),(asset.values.T - Y_hat)/Y_hat*100) # now_distanse=int((asset.iat[-1]-Y_hat[-1])/Y_hat[-1]*100) # log.debug("dis:%s now:%s"%(distance[:2],now_distanse)) # log.debug("now_distanse:%s"%now_distanse) distance = asset.values.T - Y_hat now_distanse = asset.iat[-1] - Y_hat[-1] # distance = (asset.values.T-Y_hat)[0] pd.Series(distance).plot(kind="hist", stacked=True, bins=100) # plt.plot((asset.iat[-1].T-Y_hat),'b',alpha=0.9) plt.axvline(now_distanse, hold=None, label="1", color="red") # plt.axhline(now_distanse,hold=None,label="1",color='red') # plt.axvline(asset.iat[0],hold=None,label="1",color='red',linestyle="--") plt.xlabel("Undervalue ------------------------------------------> Overvalue", fontsize=14) plt.ylabel("Frequency", fontsize=14) # plt.title('Undervalue & Overvalue Statistical Chart', fontsize=14) plt.legend([code, asset.iat[-1]]) plt.grid(True) # plt.show() # import os # print(os.path.abspath(os.path.curdir)) ax5 = fig.add_subplot(326) # fig.figsize=(5, 10) log.info("assert:len:%s %s" % (len(asset.values.T - Y_hat), (asset.values.T - Y_hat)[0])) # distance = map(lambda x:int(x),(asset.values.T - Y_hat)/Y_hat*100) distance = (asset.values.T - Y_hat) / Y_hat * 100 now_distanse = (asset.iat[-1] - Y_hat[-1]) / Y_hat[-1] * 100 log.debug("dis:%s now:%s" % (distance[:2], now_distanse)) log.debug("now_distanse:%s" % now_distanse) # n, bins = np.histogram(distance, 50) # print n, bins[:2] pd.Series(distance).plot(kind="hist", stacked=True, bins=100) # plt.plot((asset.iat[-1].T-Y_hat),'b',alpha=0.9) plt.axvline(now_distanse, hold=None, label="1", color="red") # plt.axhline(now_distanse,hold=None,label="1",color='red') # plt.axvline(asset.iat[0],hold=None,label="1",color='red',linestyle="--") plt.xlabel("Undervalue ------------------------------------------> Overvalue", fontsize=14) plt.ylabel("Frequency", fontsize=14) # plt.title('Undervalue & Overvalue Statistical Chart', fontsize=14) plt.legend([code, asset.iat[-1]]) plt.grid(True) ax6 = fig.add_subplot(324) h = df.loc[:, ["open", "close", "high", "low"]] highp = h["high"].values lowp = h["low"].values openp = h["open"].values closep = h["close"].values lr = LinearRegression() x = np.atleast_2d(np.linspace(0, len(closep), len(closep))).T lr.fit(x, closep) LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False) xt = np.atleast_2d(np.linspace(0, len(closep) + 200, len(closep) + 200)).T yt = lr.predict(xt) bV = [] bP = [] for i in range(1, len(highp) - 1): if highp[i] <= highp[i - 1] and highp[i] < highp[i + 1] and lowp[i] <= lowp[i - 1] and lowp[i] < lowp[i + 1]: bV.append(lowp[i]) bP.append(i) d, p = LIS(bV) idx = [] for i in range(len(p)): idx.append(bP[p[i]]) lr = LinearRegression() X = np.atleast_2d(np.array(idx)).T Y = np.array(d) lr.fit(X, Y) estV = lr.predict(xt) ax6.plot(closep, linewidth=2) ax6.plot(idx, d, "ko") ax6.plot(xt, estV, "-r", linewidth=3) ax6.plot(xt, yt, "-g", linewidth=3) plt.grid(True) # plt.tight_layout() zp2 = zoompan.ZoomPan() figZoom = zp2.zoom_factory(ax6, base_scale=scale) figPan = zp2.pan_factory(ax6) show()