コード例 #1
0
ファイル: das_stats.py プロジェクト: zdenekmaxa/DAS
def plot_stat(rows, cache):
    "Use matplotlib to plot DAS statistics"
    if  not PLOT_ALLOWED:
        raise Exception('Matplotlib is not available on the system')
    if  cache in ['cache', 'merge']: # cachein, cacheout, mergein, mergeout
        name_in  = '%sin' % cache
        name_out = '%sout' % cache
    else: # webip, webq, cliip, cliq
        name_in  = '%sip' % cache
        name_out = '%sq' % cache
    def format_date(date):
        "Format given date"
        val = str(date)
        return '%s-%s-%s' % (val[:4], val[4:6], val[6:8])
    date_range = [r['date'] for r in rows]
    formated_dates = [format_date(str(r['date'])) for r in rows]
    req_in  = [r[name_in] for r in rows]
    req_out = [r[name_out] for r in rows]

    plt.plot(date_range, req_in , 'ro-',
             date_range, req_out, 'gv-',
    )
    plt.grid(True)
    plt.axis([min(date_range), max(date_range), \
                0, max([max(req_in), max(req_out)])])
    plt.xticks(date_range, tuple(formated_dates), rotation=17)
#    plt.xlabel('dates [%s, %s]' % (date_range[0], date_range[-1]))
    plt.ylabel('DAS %s behavior' % cache)
    plt.savefig('das_%s.pdf' % cache, format='pdf', transparent=True)
    plt.close()
コード例 #2
0
def plot_comfort(fingers_org=range(1, 6, 1),
                 fingers_dst=range(1, 6, 1),
                 jumps=range(-12, 13, 1)):

    import seaborn
    from mpl_toolkits.mplot3d import Axes3D
    from pylab import plt

    xs, ys, zs, cs = calculate_comforts(fingers_org, fingers_dst, jumps)

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(xs, ys, zs, c=cs)
    ax.set_zlabel("Interval (half steps)", fontsize=15)
    ax.set_zlim(jumps[0], jumps[-1])
    # ax.set_zticks(jumps)

    plt.xticks(fingers_org)
    plt.xlim(fingers_org[0], fingers_org[-1])
    plt.xlabel("From finger", fontsize=15)

    plt.yticks(fingers_dst)
    plt.ylim(fingers_dst[0], fingers_dst[-1])
    plt.ylabel("To finger", fontsize=15)

    plt.title("Difficulty of finger passages", fontsize=25)

    plt.savefig('./figures/image.png', figsize=(16, 12), dpi=300)
    plt.show()
コード例 #3
0
    def run(self):

        plt.xticks([]), plt.yticks([])

        if self.mode == Mode.DISPLAY:
            self.animation = matplotlib.animation.FuncAnimation(self.fig, self.time_step, interval=60)

        elif self.mode == Mode.ON_KEY_PRESS:
            self.fig.canvas.mpl_connect('key_press_event', self.time_step)

        else:
            print("Creating video! Could need some time to complete!")

            ffmpeg_writer = matplotlib.animation.writers['ffmpeg']
            metadata = dict(title=self.video_name.split(".")[0], artist='Matplotlib',
                            comment='')
            writer = ffmpeg_writer(fps=15, metadata=metadata)

            n_frames = len(self.data)

            with writer.saving(self.fig, self.video_name, n_frames):
                for i in range(n_frames):
                    self.time_step()
                    writer.grab_frame()

        if self.mode != Mode.SAVE:
            plt.show()
コード例 #4
0
 def test_run(self):
     # 生成几何布朗运动市场环境
     me_gbm = MarketEnvironment('me_gbm', dt.datetime(2020, 1, 1))
     me_gbm.add_constant('initial_value', 36.0)
     me_gbm.add_constant('volatility', 0.2)
     me_gbm.add_constant('final_date', dt.datetime(2020, 12, 31))
     me_gbm.add_constant('currency', 'EUR')
     me_gbm.add_constant('frequency', 'M')
     me_gbm.add_constant('paths', 1000)
     csr = ConstantShortRate('csr', 0.06)
     me_gbm.add_curve('discount_curve', csr)
     # 生成几何布朗运动模拟类
     gbm = GeometricBrownianMotion('gbm', me_gbm)
     gbm.generate_time_grid()
     # 生成跳跃扩散市场环境
     me_jd = MarketEnvironment('me_jd', dt.datetime(2020, 1, 1))
     me_jd.add_constant('lambda', 0.3)
     me_jd.add_constant('mu', -0.75)
     me_jd.add_constant('delta', 0.1)
     me_jd.add_environment(me_gbm)
     # 生成跳跃扩散模拟类
     jd = JumpDiffusion('jd', me_jd)
     paths_3 = jd.get_instrument_values()
     jd.update(lamb=0.9)
     paths_4 = jd.get_instrument_values()
     # 绘制图形
     plt.figure(figsize=(10, 6))
     p1 = plt.plot(gbm.time_grid, paths_3[:, :10], 'b')
     p2 = plt.plot(gbm.time_grid, paths_4[:, :10], 'r-')
     lengend1 = plt.legend([p1[0], p2[0]],
                           ['low intensity', 'high intensity'],
                           loc=3)
     plt.gca().add_artist(lengend1)
     plt.xticks(rotation=30)
     plt.show()
コード例 #5
0
 def test_run(self):
     # 生成几何布朗运动市场环境
     me_gbm = MarketEnvironment('me_gbm', dt.datetime(2020, 1, 1))
     me_gbm.add_constant('initial_value', 36.0)
     me_gbm.add_constant('volatility', 0.2)
     me_gbm.add_constant('final_date', dt.datetime(2020, 12, 31))
     me_gbm.add_constant('currency', 'EUR')
     me_gbm.add_constant('frequency', 'M')
     me_gbm.add_constant('paths', 1000)
     csr = ConstantShortRate('csr', 0.06)
     me_gbm.add_curve('discount_curve', csr)
     # 生成几何布朗运动模拟类
     gbm = GeometricBrownianMotion('gbm', me_gbm)
     gbm.generate_time_grid()
     # 生成跳跃扩散市场环境
     me_srd = MarketEnvironment('me_srd', dt.datetime(2020, 1, 1))
     me_srd.add_constant('initial_value', .25)
     me_srd.add_constant('volatility', 0.05)
     me_srd.add_constant('final_date', dt.datetime(2020, 12, 31))
     me_srd.add_constant('currency', 'EUR')
     me_srd.add_constant('frequency', 'W')
     me_srd.add_constant('paths', 10000)
     # specific to simualation class
     me_srd.add_constant('kappa', 4.0)
     me_srd.add_constant('theta', 0.2)
     me_srd.add_curve('discount_curve', ConstantShortRate('r', 0.0))
     srd = SquareRootDiffusion('srd', me_srd)
     srd_paths = srd.get_instrument_values()[:, :10]
     plt.figure(figsize=(10, 6))
     plt.plot(srd.time_grid, srd.get_instrument_values()[:, :10])
     plt.axhline(me_srd.get_constant('theta'), color='r', ls='--', lw=2.0)
     plt.xticks(rotation=30)
     plt.show()
コード例 #6
0
ファイル: wsgi.py プロジェクト: tonglanli/jiebademo
def serve_css(name, length, keys, values):
    from pylab import plt, mpl
    mpl.rcParams['font.sans-serif'] = ['SimHei']
    mpl.rcParams['axes.unicode_minus'] = False
    from matplotlib.font_manager import FontProperties
    # font = FontProperties(fname="d:\Users\ll.tong\Desktop\msyh.ttf", size=12)
    font = FontProperties(fname="/usr/share/fonts/msyh.ttf", size=11)
    plt.xlabel(u'')
    plt.ylabel(u'出现次数',fontproperties=font)
    plt.title(u'词频统计',fontproperties=font)
    plt.grid()
    keys = keys.decode("utf-8").split(' ')
    values = values.split(' ')
    valuesInt = []
    for value in values:
        valuesInt.append(int(value))

    plt.xticks(range(int(length)), keys)
    plt.plot(range(int(length)), valuesInt)
    plt.xticks(rotation=defaultrotation, fontsize=9,fontproperties=font)
    plt.yticks(fontsize=10,fontproperties=font)
    name = name + str(datetime.now().date()).replace(':', '') + '.png'
    imgUrl = 'static/temp/' + name
    fig = matplotlib.pyplot.gcf()
    fig.set_size_inches(12.2, 2)
    plt.savefig(imgUrl, bbox_inches='tight', figsize=(20,4), dpi=100)
    plt.close()
    tempfile = static_file(name, root='./static/temp/')
    #os.remove(imgUrl)
    return tempfile
コード例 #7
0
 def test_run(self):
     plt.style.use('seaborn')
     mpl.rcParams['font.family'] = 'serif'
     # 生成市场环境
     me_gbm = MarketEnvironment('me_gbm', dt.datetime(2020, 1, 1))
     me_gbm.add_constant('initial_value', 36.0)
     me_gbm.add_constant('volatility', 0.2)
     me_gbm.add_constant('final_date', dt.datetime(2020, 12, 31))
     me_gbm.add_constant('currency', 'EUR')
     me_gbm.add_constant('frequency', 'M')
     me_gbm.add_constant('paths', 1000)
     csr = ConstantShortRate('csr', 0.06)
     me_gbm.add_curve('discount_curve', csr)
     # 生成几何布朗运动模拟类
     gbm = GeometricBrownianMotion('gbm', me_gbm)
     gbm.generate_time_grid()
     print('时间节点:{0};'.format(gbm.time_grid))
     paths_1 = gbm.get_instrument_values()
     print('paths_1: {0};'.format(paths_1.round(3)))
     gbm.update(volatility=0.5)
     paths_2 = gbm.get_instrument_values()
     # 可视化结果
     plt.figure(figsize=(10, 6))
     p1 = plt.plot(gbm.time_grid, paths_1[:, :10], 'b')
     p2 = plt.plot(gbm.time_grid, paths_2[:, :10], 'r-')
     legend1 = plt.legend([p1[0], p2[0]],
                          ['low volatility', 'high volatility'],
                          loc=2)
     plt.gca().add_artist(legend1)
     plt.xticks(rotation=30)
     plt.show()
コード例 #8
0
ファイル: plotcase.py プロジェクト: MikeJPlatt/plotcase-0.1.0
    def plot_on_graph (self):

        lineList = []
        lineList = self.ax.get_lines()

        i = 0
        for dataSet in self.data:
            plt.pause(self.pause)
            lineList[i].set_xdata(self.axis)
            lineList[i].set_ydata(self.data[i])
            plt.xticks(self.axis)
            i += 1  

        newscale = self.ax.get_lines()
        self.scaleList = []

        i = 0 
        for line in newscale:
            vis = newscale[i].get_visible()
            if vis:
                current = newscale[i].get_data()
                self.scaleList.extend(current[1])
            i += 1
            
        low = min(set(self.scaleList)) - (abs(min(set(self.scaleList))) * 0.15)
        high = max(set(self.scaleList)) + (abs(max(set(self.scaleList))) * 0.15)
        self.ax.set_ylim(low, high)
        plt.draw()
        
        self.lines = lineList
        self.lined = dict()
        for legline, origline in zip(self.leg.get_lines(), self.lines):
            legline.set_picker(5)  # 5 pts tolerance
            self.lined[legline] = origline          
コード例 #9
0
def plot_stat(rows, cache):
    "Use matplotlib to plot DAS statistics"
    if not PLOT_ALLOWED:
        raise Exception('Matplotlib is not available on the system')
    if cache in ['cache', 'merge']:  # cachein, cacheout, mergein, mergeout
        name_in = '%sin' % cache
        name_out = '%sout' % cache
    else:  # webip, webq, cliip, cliq
        name_in = '%sip' % cache
        name_out = '%sq' % cache

    def format_date(date):
        "Format given date"
        val = str(date)
        return '%s-%s-%s' % (val[:4], val[4:6], val[6:8])

    date_range = [r['date'] for r in rows]
    formated_dates = [format_date(str(r['date'])) for r in rows]
    req_in = [r[name_in] for r in rows]
    req_out = [r[name_out] for r in rows]

    plt.plot(date_range, req_in , 'ro-',\
             date_range, req_out, 'gv-',)
    plt.grid(True)
    plt.axis([min(date_range), max(date_range), \
                0, max([max(req_in), max(req_out)])])
    plt.xticks(date_range, tuple(formated_dates), rotation=17)
    #    plt.xlabel('dates [%s, %s]' % (date_range[0], date_range[-1]))
    plt.ylabel('DAS %s behavior' % cache)
    plt.savefig('das_%s.pdf' % cache, format='pdf', transparent=True)
    plt.close()
コード例 #10
0
 def _plot(self):
     p = plt.pcolor(self.matrix, cmap=self.cmap, vmin=self.vmin, vmax=self.vmax)
     plt.colorbar(p)
     plt.xlim((0, self.matrix.shape[0]))
     plt.ylim((0, self.matrix.shape[1]))
     if self.labels is not None:
         plt.xticks(numpy.arange(0.5, len(self.labels) + 0.5), self.labels, fontsize=self.fontsize, rotation=90)
         plt.yticks(numpy.arange(0.5, len(self.labels) + 0.5), self.labels, fontsize=self.fontsize)
コード例 #11
0
ファイル: utils.py プロジェクト: zhengkang86/CaNet
def plot_iou(checkpoint_dir, iou_list):
    x = range(0, len(iou_list))
    y = iou_list
    plt.switch_backend('agg')
    plt.plot(x, y, color='red', marker='o', label='IOU')
    plt.xticks(range(0, len(iou_list) + 3, (len(iou_list) + 10) // 10))
    plt.legend()
    plt.grid()
    plt.savefig(os.path.join(checkpoint_dir, 'iou_fig.pdf'))
    plt.close()
コード例 #12
0
ファイル: utils.py プロジェクト: zhengkang86/CaNet
def plot_loss(checkpoint_dir, loss_list, save_pred_every):
    x = range(0, len(loss_list) * save_pred_every, save_pred_every)
    y = loss_list
    plt.switch_backend('agg')
    plt.plot(x, y, color='blue', marker='o', label='Train loss')
    plt.xticks(range(0, len(loss_list) * save_pred_every + 3, (len(loss_list) * save_pred_every + 10) // 10))
    plt.legend()
    plt.grid()
    plt.savefig(os.path.join(checkpoint_dir, 'loss_fig.pdf'))
    plt.close()
 def savefig(self, fname):
     # Graph using the parameters
     plt.xlim(-1000,
              max(self.incomes) *
              1.05)  # make it a little bigger than needed
     plt.ylim(-5, 105)
     plt.legend(loc='lower center', fontsize=9)
     plt.xticks(rotation=20)
     plt.axes().get_xaxis().set_major_formatter(
         mp.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
     plt.savefig(fname)
コード例 #14
0
ファイル: train1.py プロジェクト: dante0shy/deepLearningCoSeg
def plot_precisonAndjac(checkpoint_dir, pre_list, jac_list):
    x = range(0, len(pre_list))
    y = pre_list
    y2 = jac_list
    plt.switch_backend('agg')
    plt.plot(x, y, color='red', marker='o', label='precision')
    plt.plot(x, y2, color='blue', marker='o', label='jaccard')
    plt.xticks(range(0, len(pre_list) + 3, (len(pre_list) + 10) // 10))
    plt.legend()
    plt.grid()
    plt.savefig(os.path.join(checkpoint_dir, 'precisionAndjac_fig1.pdf'))
    plt.close()
コード例 #15
0
    def _plot(self):
        length = len(self.data) + self.step
        if self.sortby is not None:
            inds = numpy.argsort(self.data[self.sortby].values())[::-1]
        else:
            inds = numpy.array(range(len(self.data[self.data.keys()[0]])))
        xticks_labels = numpy.array(self.data[self.data.keys()[0]].keys())[inds]
        for move, (label, sample) in enumerate(self.data.items()):
            color = _COLOR_CYCLE.next()
            index = numpy.arange(len(sample))
            plt.bar(length * index + move, numpy.array(sample.values())[inds], 1., alpha=self.alpha, color=color,
                    label=label)

        plt.xticks(length * numpy.arange(len(inds)), xticks_labels, rotation=90)
コード例 #16
0
ファイル: image.py プロジェクト: Apogentus/common
def dynamic_img_show(img,title_str='',fig_size=[14,8],hide_axes=True):
    '''Show image <img>. If called repeatedly within a cycle will dynamically redraw image.
    #DEMO
    import time

    for i in range(10):
        img = np.zeros([50,50])
        img[:i*5]=1
        dynamic_img_show(img,'iter=%s'%i)
        time.sleep(0.1)
    '''
    plt.clf()
    plt.title(title_str)
    plt.imshow(img)
    plt.xticks([]); plt.yticks([]);
    plt.gcf().set_size_inches(fig_size)
    display.display(plt.gcf())
    display.clear_output(wait=True)   
コード例 #17
0
def _plot_base(dep, val, deplim_small, xlim_small, xlabel):
    plt.subplot(1, 2, 1)
    plt.plot(val, dep)
    plt.gca().invert_yaxis()
    plt.grid('on')
    plt.ylabel('depth/km')
    plt.xlabel(xlabel)
    locs, labels = plt.xticks()
    plt.setp(labels, rotation=-45)

    plt.subplot(1, 2, 2)
    plt.plot(val, dep)
    plt.gca().invert_yaxis()
    plt.grid('on')
    plt.ylim(deplim_small)
    plt.xlim(xlim_small)
    plt.xlabel(xlabel)
    locs, labels = plt.xticks()
    plt.setp(labels, rotation=-45)
コード例 #18
0
def _plot_base(dep, val, deplim_small, xlim_small, xlabel):
    plt.subplot(1,2,1)
    plt.plot(val, dep)
    plt.gca().invert_yaxis()
    plt.grid('on')
    plt.ylabel('depth/km')
    plt.xlabel(xlabel)
    locs, labels = plt.xticks()
    plt.setp(labels, rotation=-45)

    plt.subplot(1,2,2)
    plt.plot(val, dep)
    plt.gca().invert_yaxis()
    plt.grid('on')
    plt.ylim(deplim_small)
    plt.xlim(xlim_small)
    plt.xlabel(xlabel)
    locs, labels = plt.xticks()
    plt.setp(labels, rotation=-45)
コード例 #19
0
ファイル: visualize.py プロジェクト: isall/unmixing
    def plot_spectral_profile(self,
                              points,
                              dd=False,
                              scale=1,
                              domain=np.array([1, 2, 3, 4, 5, 7]),
                              labels=None,
                              xlab=None,
                              ylab=None,
                              ylim=(0, None),
                              lloc='upper left',
                              **kwargs):
        '''
        Assumes the domain is the Landsat TM/ETM+ bands minus the thermal IR
        channel (bands 1-5 and 7). If the spectra are in Landsat surface
        reflectance units, they should be scaled by 0.01 to get reflectance
        as a percentage (by 0.0001 to get it as a proportion).
        '''
        assert not self.__raveled__, 'Cannot do this when the input array is raveled'
        spectra = self.__spectra__(points, dd, scale, domain, self.__nodata__)
        xs = range(1, domain.shape[0] + 1)

        # Truncate the spectra if necessary
        if len(xs) != spectra.shape[1]:
            spectra = spectra[:, 0:len(xs)]

        # Plot as lines
        lines = plot(xs, spectra.transpose(), linewidth=2, **kwargs)

        # Set the x-axis tick labels (e.g., skip band 6)
        plt.xticks(xs, domain)
        plt.ylim(ylim)

        if xlab is not None:
            plt.xlabel(xlab)

        if ylab is not None:
            plt.ylabel(ylab)

        if labels is not None:
            plt.legend(lines, labels, loc=lloc, frameon=False)

        plt.show()
コード例 #20
0
def show_prediction_result(image, label_image, clf):
    size = (8, 8)
    plt.figure(figsize=(15, 10))
    plt.imshow(image, cmap='gray_r')
    candidates = []
    predictions = []
    for region in regionprops(label_image):
        # skip small images
        #     if region.area < 100:
        #         continue
        # draw rectangle around segmented coins
        minr, minc, maxr, maxc = region.bbox
        # make regions square
        maxwidth = np.max([maxr - minr, maxc - minc])
        minr, maxr = int(0.5 * ((maxr + minr) - maxwidth)) - 3, int(0.5 * ((maxr + minr) + maxwidth)) + 3
        minc, maxc = int(0.5 * ((maxc + minc) - maxwidth)) - 3, int(0.5 * ((maxc + minc) + maxwidth)) + 3
        rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                                  fill=False, edgecolor='red', linewidth=2, alpha=0.2)
        plt.gca().add_patch(rect)
        # predict digit
        candidate = image[minr:maxr, minc:maxc]
        candidate = np.array(imresize(candidate, size), dtype=float)
        # invert
        # candidate = np.max(candidate) - candidate
        #     print im
        # rescale to 16 in integer
        candidate = (candidate - np.min(candidate))
        if np.max(candidate) == 0:
            continue
        candidate /= np.max(candidate)
        candidate[candidate < 0.2] = 0.0
        candidate *= 16
        candidate = np.array(candidate, dtype=int)
        prediction = clf.predict(candidate.reshape(-1))
        candidates.append(candidate)
        predictions.append(prediction)
        plt.text(minc - 10, minr - 10, "{}".format(prediction), fontsize=50)
    plt.xticks([], [])
    plt.yticks([], [])
    plt.tight_layout()
    plt.show()
    return candidates, predictions
コード例 #21
0
def pos_firmA_over_pos_firmB(file_name, folder=None):

    if folder is None:
        folder = "data/figures"

    os.makedirs(folder, exist_ok=True)

    bkp = backup.RunBackup.load(file_name=file_name)

    pos = bkp.positions[-1000:]

    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax.scatter(pos[:, 0], pos[:, 1], color="black", alpha=0.05)
    ax.axvline(0.5, color="white", linewidth=0.5, linestyle="--")
    ax.axhline(0.5, color="white", linewidth=0.5, linestyle="--")

    plt.xlim(-1, bkp.parameters.n_positions)
    plt.ylim(-1, bkp.parameters.n_positions)
    plt.xticks(
        range(0, bkp.parameters.n_positions + 1,
              round(bkp.parameters.n_positions / 5)))
    plt.yticks(
        range(0, bkp.parameters.n_positions + 1,
              round(bkp.parameters.n_positions / 5)))

    plt.xlabel("Position A")
    plt.ylabel("Position B")

    plt.text(0.005,
             0.005,
             file_name,
             transform=fig.transFigure,
             fontsize='x-small',
             color='0.5')

    plt.title("$r={:.2f}$".format(bkp.field_of_view / 2))
    ax.set_aspect(1)
    plt.tight_layout()
    plt.savefig("{}/{}_evo_positions.pdf".format(folder, file_name))
    plt.show()
コード例 #22
0
def show_prediction_result(image, label_image, clf):
    size = (8, 8)
    plt.figure(figsize=(15, 10))
    plt.imshow(image, cmap='gray_r')
    candidates = []
    predictions = []
    for region in regionprops(label_image):
        # skip small images
        #     if region.area < 100:
        #         continue
        # draw rectangle around segmented coins
        minr, minc, maxr, maxc = region.bbox
        # make regions square
        maxwidth = np.max([maxr - minr, maxc - minc])
        minr, maxr = int(0.5 * ((maxr + minr) - maxwidth)) - 3, int(0.5 * ((maxr + minr) + maxwidth)) + 3
        minc, maxc = int(0.5 * ((maxc + minc) - maxwidth)) - 3, int(0.5 * ((maxc + minc) + maxwidth)) + 3
        rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                                  fill=False, edgecolor='red', linewidth=2, alpha=0.2)
        plt.gca().add_patch(rect)
        # predict digit
        candidate = image[minr:maxr, minc:maxc]
        candidate = np.array(imresize(candidate, size), dtype=float)
        # invert
        # candidate = np.max(candidate) - candidate
        #     print im
        # rescale to 16 in integer
        candidate = (candidate - np.min(candidate))
        if np.max(candidate) == 0:
            continue
        candidate /= np.max(candidate)
        candidate[candidate < 0.2] = 0.0
        candidate *= 16
        candidate = np.array(candidate, dtype=int)
        prediction = clf.predict(candidate.reshape(-1))
        candidates.append(candidate)
        predictions.append(prediction)
        plt.text(minc - 10, minr - 10, "{}".format(prediction), fontsize=50)
    plt.xticks([], [])
    plt.yticks([], [])
    plt.tight_layout()
    plt.show()
    return candidates, predictions
コード例 #23
0
def pos_firmA_over_pos_firmB(backup, fig_name):

    os.makedirs(os.path.dirname(fig_name), exist_ok=True)

    position_max = backup.parameters.n_positions - 1

    pos = backup.positions[-1000:] / position_max

    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax.scatter(pos[:, 0], pos[:, 1], color="black", alpha=0.05, zorder=10)
    ax.axvline(0.5, color="0.5", linewidth=0.5, linestyle="--", zorder=1)
    ax.axhline(0.5, color="0.5", linewidth=0.5, linestyle="--", zorder=1)

    plt.xlim(0, 1)
    plt.ylim(0, 1)
    plt.xticks((0, 0.5, 1))
    plt.yticks((0, 0.5, 1))

    plt.xlabel("Position $a$")
    plt.ylabel("Position $b$")

    for tick in ax.get_xticklabels():
        tick.set_fontsize("small")
    for tick in ax.get_yticklabels():
        tick.set_fontsize("small")

    plt.title("$r={:.2f}$".format(backup.parameters.r))
    ax.set_aspect(1)

    # Cut margins
    plt.tight_layout()

    # Save fig
    plt.savefig(fig_name)

    plt.close()
コード例 #24
0
    def plot3Axes(self,
                  figNum,
                  time,
                  data,
                  title='',
                  units='',
                  xlabel='Time (s)',
                  options='',
                  xlim=None,
                  ylim=None):
        fig = plt.figure(figNum)
        #     plt.cla()

        if title:
            self.title = title
        if units:
            self.units = units
        if self.preTitle:
            fig.canvas.set_window_title("Figure %d - %s" %
                                        (figNum, self.preTitle))
        if not figNum in self.sharex.keys():
            self.sharex[figNum] = plt.subplot(3, 1, 1)
#             plt.plot(time, data[:,0], options)

# Plot 1
        subplt = plt.subplot(3, 1, 1, sharex=self.sharex[figNum])
        #         plt.hold(True);
        plt.grid(True)
        plt.title("%s" % (self.title))
        plt.ylabel('(%s)' % (self.units))
        plt.xlabel(xlabel)
        plt.margins(0.04)
        if xlim:
            subplt.set_xlim(xlim)
        if ylim:
            subplt.set_ylim(ylim)
        if self.timeIsUtc:
            dates = [dt.datetime.fromtimestamp(ts) for ts in time]
            datenums = md.date2num(dates)
            #         plt.subplots_adjust(bottom=0.2)
            plt.xticks(rotation=25)
            ax = plt.gca()
            if self.timeIsUtc == 2:
                xfmt = md.DateFormatter('%H:%M:%S.%f')
            else:
                xfmt = md.DateFormatter('%H:%M:%S')
            ax.xaxis.set_major_formatter(xfmt)
            plt.plot(datenums, data[:, 0], options)
        else:
            plt.plot(time, data[:, 0], options)

        # Plot 2
        subplt = plt.subplot(3, 1, 2, sharex=self.sharex[figNum])
        #         plt.hold(True);
        plt.grid(True)
        plt.ylabel('(%s)' % (self.units))
        plt.xlabel(xlabel)
        plt.margins(0.04)
        if xlim:
            subplt.set_xlim(xlim)
        if ylim:
            subplt.set_ylim(ylim)
        if self.timeIsUtc:
            dates = [dt.datetime.fromtimestamp(ts) for ts in time]
            datenums = md.date2num(dates)
            #         plt.subplots_adjust(bottom=0.2)
            plt.xticks(rotation=25)
            ax = plt.gca()
            if self.timeIsUtc == 2:
                xfmt = md.DateFormatter('%H:%M:%S.%f')
            else:
                xfmt = md.DateFormatter('%H:%M:%S')
            ax.xaxis.set_major_formatter(xfmt)
            plt.plot(datenums, data[:, 1], options)
        else:
            plt.plot(time, data[:, 1], options)

        # Plot 3
        subplt = plt.subplot(3, 1, 3, sharex=self.sharex[figNum])
        #         plt.hold(True);
        plt.grid(True)
        plt.ylabel('(%s)' % (self.units))
        plt.xlabel(xlabel)
        plt.margins(0.04)
        if xlim:
            subplt.set_xlim(xlim)
        if ylim:
            subplt.set_ylim(ylim)
        if self.timeIsUtc:
            dates = [dt.datetime.fromtimestamp(ts) for ts in time]
            datenums = md.date2num(dates)
            #         plt.subplots_adjust(bottom=0.2)
            plt.xticks(rotation=25)
            ax = plt.gca()
            if self.timeIsUtc == 2:
                xfmt = md.DateFormatter('%H:%M:%S.%f')
            else:
                xfmt = md.DateFormatter('%H:%M:%S')
            ax.xaxis.set_major_formatter(xfmt)
            plt.plot(datenums, data[:, 2], options)
        else:
            plt.plot(time, data[:, 2], options)

    #         legend(['desire','actual','e/10','e2/10'])
        return fig
コード例 #25
0
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)
コード例 #26
0
sub_df = df_netens
# here we omit leadtime 0 (because we dont need and it makes the plots confusing, especially for corr)
sub_df = sub_df[sub_df['leadtime'] != 0]
plt.subplot(3, 1, 1)
sns.lineplot('n_ens', 'rmse_ensmean_netens', data=sub_df, hue='leadtime')
sns.lineplot('n_ens',
             'spread_netens',
             data=sub_df,
             hue='leadtime',
             style=True,
             dashes=[(2, 2)],
             legend=False)
plt.legend(bbox_to_anchor=(0, 1.1), loc=2, borderaxespad=0., ncol=5)
plt.xlabel('')
plt.ylabel('rmse (solid) \n spread (dashed) [$m^2/s^2$]')
plt.xticks(np.arange(2, 21, 2))
sns.despine()
plt.subplot(3, 1, 2)
sns.lineplot('n_ens', 'corr_netens', data=sub_df, hue='leadtime')
plt.legend(bbox_to_anchor=(0, 1.1), loc=2, borderaxespad=0., ncol=5)
plt.xlabel('')
plt.ylabel('spread error correlation')
plt.xticks(np.arange(2, 21, 2))
sns.despine()
plt.subplot(3, 1, 3)
sns.lineplot('n_ens', 'crps_netens', data=sub_df, hue='leadtime')
plt.legend(bbox_to_anchor=(0, 1.1), loc=2, borderaxespad=0., ncol=5)
plt.xlabel('$n_{ens}$')
plt.ylabel('crps [$m^2/s^2$]')
plt.xticks(np.arange(2, 21, 2))
sns.despine()
final_df['mse'] = (final_df['truth'] - final_df['prediction'])**2
final_df['abse'] = (final_df['truth'] - final_df['prediction']).abs()
final_df['persistence_mse'] = (final_df['persistence'] - final_df['truth'])**2
final_df['persistence_abse'] = (final_df['persistence'] -
                                final_df['truth']).abs()

final_df['abse_diff'] = final_df['abse'] - final_df['persistence_abse']

#%%------------------- now make plots
sns.set_context('notebook', font_scale=1.0, rc={"lines.linewidth": 3.})
sns.set_palette('colorblind')
plt.rcParams['savefig.bbox'] = 'tight'
# make plots lead_hour vs X

# if wen wanto to use the built-in bootstrapping methods form seaborn for computing the uncertainty
# in RMSE, we canot use the standard method, because one cannot average RMSE.
# however, we can do it vie the MSE: we put the MSE in the plitting fucntion, and then we dont use the standrad
# estimator, but a costum one, that computes the squareroot of the mean. we can pass it in as a lambda function
ci = 90

plt.figure(figsize=(7, 4))
sns.barplot('leave_out_var',
            'abse',
            data=final_df,
            ci=ci,
            color=sns.color_palette()[0])
plt.ylabel('MAE relative production loss [%]')
sns.despine()
plt.xticks(rotation=90)
plt.savefig('plots/abse_vs_left_out_var.pdf')
コード例 #28
0
ファイル: graphs_tfidf.py プロジェクト: pegahani/REUS
        label="w.r.t extractive resume")

plt.gca().yaxis.grid(True)
plt.xlim(0, 105)
plt.ylim(0, 1.2)

plt.legend()

plt.subplot(2, 1, 2)
plt.title(
    'NMF topic modelling results vs abstractive and extractive summaries',
    fontproperties=font_prop)
plt.xlabel('scenario-based meetings', fontproperties=font_prop)
plt.ylabel('accuracy', fontproperties=font_prop)

plt.xticks(x1, nucleotides)

plt.bar(x2, [item[0] for item in counts_nmf],
        width=1.0,
        color="#87CEEB",
        label="w.r.t abstractive resume")
plt.bar(x1, [item[1] for item in counts_nmf],
        width=1.0,
        color="#F4A460",
        label="w.r.t extractive resume")

plt.gca().yaxis.grid(True)
plt.xlim(0, 105)
plt.ylim(0, 1.2)

plt.legend()