Exemplo n.º 1
0
def plot_double_column(table1,
                       title1,
                       xlabel1,
                       ylabel1,
                       table2,
                       title2,
                       xlabel2,
                       ylabel2,
                       table3,
                       title3,
                       xlabel3,
                       ylabel3,
                       savepath,
                       show=True,
                       is_equal_y=True):
    df1 = table1
    df2 = table2
    df3 = table3
    legend1 = list(table1.columns)

    dash_styles = [
        "", (1, 1), (4, 1.5), (3, 1, 1.5, 1), (5, 1, 1, 1), (5, 1, 2, 1, 2, 1),
        (2, 2, 3, 1.5), (1, 2.5, 3, 1.2), (3, 1), (1, 2, 3, 4), "", (4, 1.5),
        (1, 1), (3, 1, 1.5, 1), (5, 1, 1, 1), (5, 1, 2, 1, 2, 1),
        (2, 2, 3, 1.5), (1, 2.5, 3, 1.2), (3, 1), (1, 2, 3, 4)
    ]
    filled_markers = (
        'o',
        'X',
        '*',
        'h',
        's',
        'v',
        '^',
        '<',
        '>',
        'p',
        '8',
        'H',
        'D',
        'd',
        'P',
        'o',
        'X',
        '*',
        'h',
        's',
        'v',
        '^',
        '<',
        '>',
        'p',
        '8',
        'H',
        'D',
        'd',
        'P',
    )

    import matplotlib

    SMALL = 7.5
    Regular = 9.5
    matplotlib.rc('font', size=Regular)
    matplotlib.rc('axes', titlesize=Regular)
    # matplotlib.rcParams['axes.linewidth'] = .1

    # sns.set_style("whitegrid")
    # sns.set(style="ticks", rc={"lines.linewidth": 0.7})
    # sns.set(rc={"lines.linewidth": 0.1})

    fig = plt.figure(figsize=(5, 2.5))
    # fig, ax = plt.subplots(1,2)
    # ax1 = fig.add_subplot(1,2,1)
    ax1 = fig.add_axes([0.12, 0.15, 0.23, 0.6])
    # with plt.rc_context({'lines.linewidth': 0.3}):
    #     sns.lineplot(data=df1, palette="Set2", ax=ax1, linewidth=2, dashes=dash_styles, markers=filled_markers)
    sns.lineplot(data=df1,
                 palette="Set2",
                 ax=ax1,
                 linewidth=1.5,
                 dashes=dash_styles,
                 markers=filled_markers,
                 markeredgewidth=0,
                 legend=False)
    # plt.yticks(rotation=90)
    # ax2 = fig.add_subplot(1, 2, 2)
    ax2 = fig.add_axes([0.44, 0.15, 0.23, 0.6])
    sns.lineplot(data=df2,
                 palette="Set2",
                 ax=ax2,
                 linewidth=1.5,
                 dashes=dash_styles,
                 markers=filled_markers,
                 markeredgewidth=0,
                 legend=False)
    ax3 = fig.add_axes([0.76, 0.15, 0.23, 0.6])
    sns.lineplot(data=df3,
                 palette="Set2",
                 ax=ax3,
                 linewidth=1.5,
                 dashes=dash_styles,
                 markers=filled_markers,
                 markeredgewidth=0,
                 legend=False)

    ax1.legend(labels=legend1,
               ncol=4,
               loc='lower left',
               bbox_to_anchor=(.6, 1.1))
    # ax1.set_title(title1)
    ax1.set_xlabel(xlabel1)
    ax1.set_ylabel(ylabel1)
    # ax1.set_xticks(list(range(len(index1) + 1)[::5]))
    ax1.set_xticks([2, 4, 6, 8, 10])
    ax1.xaxis.set_label_coords(0.5, -0.15)
    ax1.xaxis.set_tick_params(labelsize=SMALL)
    ax1.yaxis.set_tick_params(labelsize=SMALL)
    ax1.yaxis.set_major_formatter(
        FuncFormatter(lambda y, _: '{:.1%}'.format(y)))

    # ax1.grid(linestyle='-.')

    # ax2.set_title(title2)
    ax2.set_xlabel(xlabel2)
    # ax2.set_ylabel(ylabel2)
    # ax2.set_xticks(list(range(len(index2) + 1)[::2]))
    ax2.set_xticks([2, 4, 6, 8, 10])
    # ax2.set_yticks([0.65,0.7,0.75,0.8,0.85])
    ax2.xaxis.set_label_coords(0.5, -0.15)
    ax2.xaxis.set_tick_params(labelsize=SMALL)
    ax2.yaxis.set_tick_params(labelsize=SMALL)
    ax2.yaxis.set_major_formatter(
        FuncFormatter(lambda y, _: '{:.1%}'.format(y)))
    if is_equal_y:
        ax2.set_ylim(ax1.get_ylim())

    # ax3.set_title(title3)
    ax3.set_xlabel(xlabel3)
    # ax3.set_ylabel(ylabel3)
    # ax2.set_xticks(list(range(len(index2) + 1)[::2]))
    ax3.set_xticks([2, 4, 6, 8, 10])
    # ax2.set_yticks([0.65,0.7,0.75,0.8,0.85])
    ax3.xaxis.set_label_coords(0.5, -0.15)
    ax3.xaxis.set_tick_params(labelsize=SMALL)
    ax3.yaxis.set_tick_params(labelsize=SMALL)
    if is_equal_y:
        ax3.set_ylim(ax1.get_ylim())
    ax3.yaxis.set_major_formatter(
        FuncFormatter(lambda y, _: '{:.1%}'.format(y)))

    # ax2.grid(linestyle='-.')

    plt.savefig(savepath, format='eps')
    plt.savefig(savepath[:-4] + '.pdf', format='pdf')
    if show:
        plt.show()
Exemplo n.º 2
0
print('Evaluating DS techniques:')
print('Classification accuracy KNORA-U: ', knorau_score)
print('Classification accuracy KNORA-E: ', kne_score)
print('Classification accuracy DESP: ', desp_score)
print('Classification accuracy OLA: ', ola_score)
print('Classification accuracy MCB: ', mcb_score)
print('Classification accuracy META-DES: ', meta_score)

cmap = get_cmap('Dark2')
colors = [cmap(i) for i in np.linspace(0, 1, 7)]
labels = [
    'RF', 'Stacked', 'KNORA-U', 'KNORA-E', 'DESP', 'OLA', 'MCB', 'META-DES'
]

fig, ax = plt.subplots()
pct_formatter = FuncFormatter(lambda x, pos: '{:.1f}'.format(x * 100))
ax.bar(np.arange(8), [
    rf_score, stacked_score, knorau_score, kne_score, desp_score, ola_score,
    mcb_score, meta_score
],
       color=colors,
       tick_label=labels)
ax.set_ylim(0.65, 0.80)
ax.set_xlabel('Method', fontsize=13)
ax.set_ylabel('Accuracy on the test set (%)', fontsize=13)
ax.yaxis.set_major_formatter(pct_formatter)
for tick in ax.get_xticklabels():
    tick.set_rotation(45)
plt.subplots_adjust(bottom=0.15)
plt.show()
Exemplo n.º 3
0
        data=data_for_corr,
        x='Longitude',  # change for latitude or longitude
        y=data_for_corr.columns[key])

    # Make the boxplot fully transparent
    for patch in ax.artists:
        r, g, b, a = patch.get_facecolor()
        patch.set_facecolor((r, g, b, 0))

    ax.set_ylabel(song_variables[key - 3] + ' (' + value + ')')
    ax.set_xlabel('')
    ax.tick_params(labelsize=30, direction='out')
    ax.set(xticklabels=[])
    plt.setp(ax.spines.values(), linewidth=2)
    ax.get_yaxis().set_major_formatter(
        FuncFormatter(lambda x, p: "%.1f" % (np.exp(x))))

    plt.savefig("C:/Users/abiga\Box Sync\Abigail_Nicole\ChippiesProject"
                "\StatsOfFinalData_withReChipperReExported"
                "\AnimalBehaviourRevisions/SupplementalCorr"
                "/" + data_for_corr.columns[key] + '_noLogAxis_largerFont' +
                '.pdf',
                type='pdf',
                dpi=fig.dpi,
                bbox_inches='tight',
                transparent=True)
    # plt.cla()
    # plt.clf()
    plt.close()

    # plt.show()
        yerr=error_lab_fresh,
        error_kw=dict(elinewidth=1, ecolor='black'),
        label=r'$ (X31-virus) $')
plt.grid(True, which='both')
plt.title(r'$ Original \ Antigenic \ Sin \ (sequential-infection)$',
          fontsize=AlvaFontSize)
plt.xlabel(r'$time \ (%s)$' % (timeUnit), fontsize=AlvaFontSize)
plt.ylabel(r'$ Neutralization \ \ titer $', fontsize=AlvaFontSize)
plt.xticks(fontsize=AlvaFontSize * 0.6)
plt.yticks(fontsize=AlvaFontSize * 0.6)
plt.xlim([minT, 6 * 30 * day])
plt.ylim([2**5, 2**14])
plt.yscale('log', basey=2)
# gca()---GetCurrentAxis and Format the ticklabel to be 2**x
plt.gca().yaxis.set_major_formatter(
    FuncFormatter(lambda x, pos: int(2**(np.log(x) / np.log(2)))))
#plt.gca().xaxis.set_major_locator(plt.MultipleLocator(7))
plt.legend(loc=(1, 0), fontsize=AlvaFontSize)
plt.show()

# In[3]:

# step by step
numberingFig = numberingFig + 1
for i in range(totalPoint_X):
    figure_name = '-response-%i' % (i)
    figure_suffix = '.png'
    save_figure = os.path.join(dir_path, file_name + figure_name + file_suffix)
    plt.figure(numberingFig, figsize=AlvaFigSize)
    plt.plot(gT,
             gV[i],
Exemplo n.º 5
0
date_range = paths[-2]

start_year = date_range[:4]


def form2(x, pos):
    """ This function returns a string with 3 decimal places, given the input x"""
    return '%.2f' % x


def form5(x, pos):
    """ This function returns a string with 3 decimal places, given the input x"""
    return '%.7f' % x


xformatter = FuncFormatter(form2)
yformatter = FuncFormatter(form5)


def onpick(event):
    global pl

    global ind
    global fig2

    ind = event.ind
    ind = ind[0]
    #x_data = event.xdata
    #y_data = event.ydata

    #find ind of closest lat/lon
Exemplo n.º 6
0
def Plot(X,
         outname,
         outdir,
         pColors,
         titlestr=None,
         grid=False,
         drawLegend=True,
         xFormat=None,
         yFormat=None,
         savePDF=True,
         savePNG=False,
         datestamp=True):

    mpl.rcParams['xtick.top'] = True
    mpl.rcParams['xtick.bottom'] = True
    mpl.rcParams['ytick.right'] = True
    mpl.rcParams['xtick.direction'] = 'in'
    mpl.rcParams['ytick.direction'] = 'in'

    mpl.rc('font', **{'size': 10})
    mpl.rc('legend', **{'fontsize': 6.0})
    mpl.rc('axes', linewidth=0.5)

    # mpl.rc('font', **{'family' : 'sans-serif', 'sans-serif' : ['Myriad Pro']})
    mpl.rc('font', **{'family': 'sans-serif', 'sans-serif': ['Helvetica']})
    mpl.rcParams['pdf.fonttype'] = 42
    mpl.rcParams['text.usetex'] = False
    mpl.rcParams['mathtext.fontset'] = 'cm'
    mpl.rcParams['text.latex.preamble'] = \
        r'\usepackage{cmbright}' + \
        r'\usepackage{amsmath}'

    ######################################################################################
    # set up figure
    fWidth, fHeight, lFrac, rFrac, bFrac, tFrac =\
        getFigureProps(width = 4.1, height = 2.9,
                       lFrac = 0.18, rFrac = 0.95,
                       bFrac = 0.18, tFrac = 0.95)
    f, ax1 = plt.subplots(1)
    f.set_size_inches(fWidth, fHeight)
    f.subplots_adjust(left=lFrac, right=rFrac)
    f.subplots_adjust(bottom=bFrac, top=tFrac)
    ######################################################################################
    labelfontsize = 6.0

    for tick in ax1.xaxis.get_major_ticks():
        tick.label.set_fontsize(labelfontsize)
    for tick in ax1.yaxis.get_major_ticks():
        tick.label.set_fontsize(labelfontsize)

    ax1.tick_params('both', length=1.5, width=0.5, which='major', pad=3.0)
    ax1.tick_params('both', length=1.0, width=0.25, which='minor', pad=3.0)

    ax1.tick_params(axis='x', which='major', pad=3.5)
    ax1.tick_params(axis='y', which='major', pad=3.5, zorder=10)
    ######################################################################################
    # labeling
    if titlestr:
        plt.title(titlestr)
    ax1.set_xlabel(r'$M$', fontsize=6.0)
    ax1.set_ylabel(r'$E_{\mathrm{RMS}}$', fontsize=6.0)
    ax1.xaxis.labelpad = 3.0
    ax1.yaxis.labelpad = 3.0
    ######################################################################################
    # plotting

    lineWidth = 0.65

    ax1.plot(X[:, 0],
             X[:, 1],
             color=pColors['blue'],
             alpha=1.0,
             lw=lineWidth,
             zorder=11,
             label=r'',
             clip_on=False)

    ax1.scatter(X[:, 0],
                X[:, 1],
                s=10.0,
                lw=lineWidth,
                facecolor='None',
                edgecolor=pColors['blue'],
                zorder=11,
                label=r'Training',
                clip_on=False)

    ax1.plot(X[:, 0],
             X[:, 2],
             color=pColors['red'],
             alpha=1.0,
             lw=lineWidth,
             zorder=11,
             label=r'',
             clip_on=False)

    ax1.scatter(X[:, 0],
                X[:, 2],
                s=10.0,
                lw=lineWidth,
                facecolor='None',
                edgecolor=pColors['red'],
                zorder=11,
                label=r'Test',
                clip_on=False)

    ######################################################################################
    # legend
    if drawLegend:
        leg = ax1.legend(  # bbox_to_anchor = [0.7, 0.8],
            # loc = 'upper left',
            handlelength=1.5,
            scatterpoints=1,
            markerscale=1.0,
            ncol=1)
        leg.draw_frame(False)
        plt.gca().add_artist(leg)

    ######################################################################################
    # set plot range
    if xFormat:
        major_x_ticks = np.arange(xFormat[2], xFormat[3], xFormat[4])
        minor_x_ticks = np.arange(xFormat[2], xFormat[3], xFormat[5])
        ax1.set_xticks(major_x_ticks)
        ax1.set_xticks(minor_x_ticks, minor=True)
        ax1.set_xlim(xFormat[0], xFormat[1])

    if yFormat:
        major_y_ticks = np.arange(yFormat[2], yFormat[3], yFormat[4])
        minor_y_ticks = np.arange(yFormat[2], yFormat[3], yFormat[5])
        ax1.set_yticks(major_y_ticks)
        ax1.set_yticks(minor_y_ticks, minor=True)
        ax1.set_ylim(yFormat[0], yFormat[1])

    # tick label formatting
    majorFormatter = FuncFormatter(cleanFormatter)
    ax1.xaxis.set_major_formatter(majorFormatter)
    ax1.yaxis.set_major_formatter(majorFormatter)

    ax1.set_axisbelow(False)

    for spine in ax1.spines.values():  # ax1.spines is a dictionary
        spine.set_zorder(10)

    ######################################################################################
    # grid options
    if grid:
        ax1.grid(color='gray',
                 linestyle='-',
                 alpha=0.2,
                 which='major',
                 linewidth=0.2)
        ax1.grid(True)
        ax1.grid(color='gray',
                 linestyle='-',
                 alpha=0.05,
                 which='minor',
                 linewidth=0.1)
        ax1.grid(True, which='minor')
    ######################################################################################
    # save to file
    if datestamp:
        outname += '_' + today
    if savePDF:
        f.savefig(os.path.join(outdir, outname) + '.pdf',
                  dpi=300,
                  transparent=True)
    if savePNG:
        f.savefig(os.path.join(outdir, outname) + '.png',
                  dpi=600,
                  transparent=False)
    ######################################################################################
    # close handles
    plt.cla()
    plt.clf()
    plt.close()
    return outname
Exemplo n.º 7
0
def metacache_overhead_summary(doLoadData):
    db_summary = ""
    db_hitrate_summary = ""
    db_metadata_txn_ratio_summary = ""

    if doLoadData == False:
        db_speedup = speedup(False)
        db_spec = geomean(db_speedup.loc[SPEC])
        db_mix = geomean(db_speedup.loc[MIX])
        db_gap = geomean(db_speedup.loc[GAP])
        db_rand = db_speedup.loc["RAND"]
        db_stream = db_speedup.loc["STREAM"]
        db_mean = geomean(db_speedup)

        db_summary = pd.DataFrame({
            'SPEC-RATE': db_spec,
            'SPEC-MIX': db_mix,
            'GAP': db_gap,
            'RAND': db_rand,
            'STREAM': db_stream,
            'AVERAGE': db_mean
        }).T
        db_summary = db_summary[[
            "metacache_rownum1024", "metacache_rownum2048",
            "metacache_rownum4096", "metacache_rownum8192",
            "metadata_predictor0"
        ]]
        db_summary.columns = [
            'Metadata Cache (128KB)', 'Metadata Cache (256KB)',
            'Metadata Cache (512KB)', 'Metadata Cache (1MB)', 'Ideal'
        ]  #change column name
        db_summary = db_summary.loc[
            ['SPEC-RATE', 'SPEC-MIX', 'GAP', 'RAND', 'STREAM', 'AVERAGE'], :]
        print db_summary

        #metadata cache hit rate
        db_miss = mergeCol("memzip_metacache_miss", "MemController0", id_tech)
        db_hit = mergeCol("memzip_metacache_hit", "MemController0", id_tech)
        db_miss = db_miss[[
            "metacache_rownum1024", "metacache_rownum2048",
            "metacache_rownum4096", "metacache_rownum8192"
        ]]
        db_hit = db_hit[[
            "metacache_rownum1024", "metacache_rownum2048",
            "metacache_rownum4096", "metacache_rownum8192"
        ]]
        db_access = db_miss + db_hit
        db_hit_rate = db_hit / (db_miss + db_hit)
        db_hit_rate = db_hit_rate.rename(index={"seq": "stream"})
        db_hit_rate.columns = [
            'Metadata Cache (128KB)', 'Metadata Cache (256KB)',
            'Metadata Cache (512KB)', 'Metadata Cache (1MB)'
        ]  #change column name
        db_hit_rate.loc['gmean'] = geomean(db_hit_rate)
        db_spec = geomean(db_hit_rate.loc[SPEC])
        db_mix = geomean(db_hit_rate.loc[MIX])
        db_gap = geomean(db_hit_rate.loc[GAP])
        db_rand = db_hit_rate.loc["rand"]
        db_stream = db_hit_rate.loc["stream"]
        db_mean = db_hit_rate.loc["gmean"]
        db_hitrate_summary = pd.DataFrame({
            'SPEC-RATE': db_spec,
            'SPEC-MIX': db_mix,
            'GAP': db_gap,
            'RAND': db_rand,
            'STREAM': db_stream,
            'AVERAGE': db_mean
        }).T

        db_hitrate_summary.loc[
            'SPEC-MIX', 'Metadata Cache (512KB)'] = db_hitrate_summary.loc[
                'SPEC-MIX', 'Metadata Cache (1MB)']
        db_hitrate_summary = db_hitrate_summary.loc[
            ['SPEC-RATE', 'SPEC-MIX', 'GAP', 'RAND', 'STREAM', 'AVERAGE'], :]
        print "----hit rate----"
        print db_hitrate_summary

        #memory transaction
        db_txnrecvd = mergeCol("totalTxnsRecvd", "MemController0", id_tech)
        db_txn_base = db_txnrecvd.iloc[:, 0]
        db_txnrecvd = db_txnrecvd[[
            "metacache_rownum1024", "metacache_rownum2048",
            "metacache_rownum4096", "metacache_rownum8192"
        ]]
        db_txnrecvd = db_txnrecvd[[
            "metacache_rownum1024", "metacache_rownum2048",
            "metacache_rownum4096", "metacache_rownum8192"
        ]]
        db_txnrecvd.columns = [
            'Metadata Cache (128KB)', 'Metadata Cache (256KB)',
            'Metadata Cache (512KB)', 'Metadata Cache (1MB)'
        ]  #change column name
        db_txn_metadata = db_txnrecvd.sub(db_txn_base, axis=0)
        db_txn_metadata_ratio = db_txn_metadata / db_txnrecvd
        #    db_txn_normal_ratio=db_txnrecvd/db_txnrecvd
        db_txn_metadata_ratio = db_txn_metadata_ratio.rename(
            index={"seq": "stream"})

        db_spec = geomean(db_txn_metadata_ratio.loc[SPEC])
        db_gap = geomean(db_txn_metadata_ratio.loc[GAP])
        db_rand = db_txn_metadata_ratio.loc["rand"]
        db_stream = db_txn_metadata_ratio.loc["stream"]
        db_mean = geomean(db_txn_metadata_ratio)
        db_metadata_txn_ratio_summary = pd.DataFrame({
            'SPEC': db_spec,
            'GAP': db_gap,
            'RAND': db_rand,
            'STREAM': db_stream,
            'AVERAGE': db_mean
        }).T

        db_metadata_txn_ratio_summary = db_metadata_txn_ratio_summary.loc[
            ['SPEC', 'GAP', 'RAND', 'STREAM', 'AVERAGE'], :]

        #store db to file
        db_summary.to_csv(report_dir + "/%s-%s.csv" %
                          ("metacache_limit", result_name))
        db_hitrate_summary.to_csv(report_dir + "/%s-%s.csv" %
                                  ("metadata_hitrate_limit", result_name))
        db_metadata_txn_ratio_summary.to_csv(
            report_dir + "/%s-%s.csv" %
            ("metadata_fetch_txn_limit", result_name))
        print db_metadata_txn_ratio_summary
    else:
        db_summary = pd.read_csv(report_dir + "/%s-%s.csv" %
                                 ("metacache_limit", result_name),
                                 index_col=0)
        db_hitrate_summary = pd.read_csv(
            report_dir + "/%s-%s.csv" %
            ("metadata_hitrate_limit", result_name),
            index_col=0)
        db_metadata_txn_ratio_summary = pd.read_csv(
            report_dir + "/%s-%s.csv" %
            ("metadata_fetch_txn_limit", result_name),
            index_col=0)

    bar_w = 0.8
    ax1 = db_summary.plot.bar(figsize=(12, 6),
                              width=bar_w,
                              cmap="summer",
                              edgecolor='black')
    ax1.grid(axis='y')
    ax1.legend(loc=9, ncol=2, fontsize=18, bbox_to_anchor=(0.5, 1.4))
    plt.xticks(fontsize=18, rotation=25)

    plt.yticks(fontsize=18)
    plt.ylabel("Speedup", fontsize=20)

    plt.subplots_adjust(left=0.08, right=0.9, bottom=0.15, top=0.75)
    plt.ylim(ymax=1.4, ymin=0.4)

    x = ax1.get_xaxis().get_majorticklocs()
    line_w = bar_w / len(db_summary.columns)
    line_x = np.arange(0, len(db_summary.columns)) * line_w
    line_x = line_x[0:len(line_x) - 1]
    ax = []
    #db_hitrate_summary=db_hitrate_summary*100
    for i in range(0, len(db_hitrate_summary.index)):
        ax_tmp = ax1.twinx()
        ax_tmp.set_ylim(ymax=1, ymin=0)
        ax_tmp.plot(x[i] - bar_w / 2 + line_x + line_w / 2,
                    db_hitrate_summary.iloc[i, :],
                    linestyle="-",
                    linewidth=2,
                    color="b",
                    marker="o",
                    markersize="10",
                    markeredgewidth=1,
                    markeredgecolor='y',
                    label='Hit-Rate of Metadata Cache')
        vals = ax_tmp.get_yticks()
        ax_tmp.tick_params(axis='y', labelsize=15)
        ax_tmp.yaxis.set_major_formatter(FuncFormatter('{0:.0%}'.format))
        ax.append(ax_tmp)

    ax[0].legend(loc=9, ncol=2, fontsize=18, bbox_to_anchor=(0.73, 1.2))
    ax[0].set_ylabel('Hit-Rate (%)', fontsize=20)

    plt.savefig(report_dir + "/%s-%s.pdf" %
                ("metadata_cache_speedup_limit", result_name))
    plt.savefig(report_dir + "/%s-%s.eps" %
                ("metadata_cache_speedup_limit", result_name),
                format='eps',
                dpi=1000)
Exemplo n.º 8
0
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# -*- coding:utf-8 -*-
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import argparse
import score
import numpy as np
import os
import save_log
import gen_report
from matplotlib.ticker import FuncFormatter

def formatnum(x, pos):
    return '$%.1f$x$10^{5}$' % (x/100000)
formatter = FuncFormatter(formatnum)

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--id",required = True, type=str, help="experiment_id")
    parser.add_argument("--logs", type=bool, default=False, help="whether to save log")
    return parser.parse_args()

def main_grid(time, values, save_folder, filename):
    time = np.array(time, dtype='float32')
    values = np.array(values, dtype='float32')
    plt.figure(figsize=(12, 6))
    ax = plt.subplot(1,1,1)
    plt.plot(time, values, linewidth = '1.0',color='blue',marker='.') #'darkgoldenrod','slateblue','aqua','red','black'
    font = {'family':'DejaVu Sans', 'weight':'normal', 'size':12}
    plt.tick_params(labelsize=12)
Exemplo n.º 9
0
    return line


years = mdates.YearLocator()
months = mdates.MonthLocator()

ax = plt.subplot(1, 1, 1)
#ax.loglog(daysFromGenesis, low, linewidth=1, color='0.5')
#ax.loglog(daysFromGenesis, high, linewidth=1, color='0.5')
averagePlot = ax.loglog(daysFromGenesis,
                        average,
                        linewidth=1,
                        label='Daily Average Price')

ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(FuncFormatter(xMajorFormat))
ax.xaxis.set_minor_locator(months)
ax.xaxis.set_minor_formatter(FuncFormatter(xMinorFormat))
ax.fmt_xdata = FuncFormatter(xDataFormat)
ax.set_xlim(datemin, datemax)

ax.set_ylabel('$/BTC')
ax.yaxis.set_major_formatter(FuncFormatter(yLogFormat))
ymin = 0.01
ymax = 1000000
ax.set_ylim(ymin, ymax)

ax.grid(b=True, which='major', color='0.8', linestyle='-')
ax.grid(b=True, which='minor', color='0.96', linestyle='-')
ax.set_axisbelow(True)
Exemplo n.º 10
0
    'converts radians to degrees'
    return round(x * 57.2985, 2)

plt.figure(figsize=(12,7), dpi=100)
X = np.linspace(0,2*np.pi,1000)
plt.plot(X,np.sin(X))
plt.plot(X,np.cos(X))

# 1. Adjust x axis Ticks
plt.xticks(ticks=np.arange(0, 440/57.2985, 90/57.2985), fontsize=12, rotation=30, ha='center', va='top')  # 1 radian = 57.2985 degrees

# 2. Tick Parameters
plt.tick_params(axis='both',bottom=True, top=True, left=True, right=True, direction='in', which='major', grid_color='blue')

# 3. Format tick labels to convert radians to degrees
formatter = FuncFormatter(rad_to_degrees)
plt.gca().xaxis.set_major_formatter(formatter)

plt.grid(linestyle='--', linewidth=0.5, alpha=0.15)
plt.title('Sine and Cosine Waves\n(Notice the ticks are on all 4 sides pointing inwards, radians converted to degrees in x axis)', fontsize=14)


## How to customize the legend
plt.figure(figsize=(10,7), dpi=80)
X = np.linspace(0, 2*np.pi, 1000)
sine = plt.plot(X,np.sin(X)); cosine = plt.plot(X,np.cos(X))
sine_2 = plt.plot(X,np.sin(X+.5)); cosine_2 = plt.plot(X,np.cos(X+.5))
plt.gca().set(ylim=(-1.25, 1.5), xlim=(-.5, 7))
plt.title('Custom Legend Example', fontsize=18)

# Modify legend
Exemplo n.º 11
0
    def plot(self,
             figure=None,
             overlays=[],
             colorbar=True,
             vmin=None,
             vmax=None,
             linear=True,
             showz=True,
             yres=DEFAULT_YRES,
             max_dist=None,
             **matplotlib_args):
        """
        Plot spectrogram onto figure.

        Parameters
        ----------
        figure : matplotlib.figure.Figure
            Figure to plot the spectrogram on. If None, new Figure is created.
        overlays : list
            List of overlays (functions that receive figure and axes and return
            new ones) to be applied after drawing.
        colorbar : bool
            Flag that determines whether or not to draw a colorbar. If existing
            figure is passed, it is attempted to overdraw old colorbar.
        vmin : float
            Clip intensities lower than vmin before drawing.
        vmax : float
            Clip intensities higher than vmax before drawing.
        linear :  bool
            If set to True, "stretch" image to make frequency axis linear.
        showz : bool
            If set to True, the value of the pixel that is hovered with the
            mouse is shown in the bottom right corner.
        yres : int or None
            To be used in combination with linear=True. If None, sample the
            image with half the minimum frequency delta. Else, sample the
            image to be at most yres pixels in vertical dimension. Defaults
            to 1080 because that's a common screen size.
        max_dist : float or None
            If not None, mask elements that are further than max_dist away
            from actual data points (ie, frequencies that actually have data
            from the receiver and are not just nearest-neighbour interpolated).
        """
        # [] as default argument is okay here because it is only read.
        # pylint: disable=W0102,R0914
        if linear:
            delt = yres
            if delt is not None:
                delt = max(
                    (self.freq_axis[0] - self.freq_axis[-1]) / (yres - 1),
                    _min_delt(self.freq_axis) / 2.)
                delt = float(delt)

            data = _LinearView(self.clip_values(vmin, vmax), delt)
            freqs = np.arange(self.freq_axis[0], self.freq_axis[-1],
                              -data.delt)
        else:
            data = np.array(self.clip_values(vmin, vmax))
            freqs = self.freq_axis

        figure = plt.gcf()

        if figure.axes:
            axes = figure.axes[0]
        else:
            axes = figure.add_subplot(111)

        params = {
            'origin': 'lower',
            'aspect': 'auto',
        }
        params.update(matplotlib_args)
        if linear and max_dist is not None:
            toplot = ma.masked_array(data, mask=data.make_mask(max_dist))
            pass
        else:
            toplot = data
        im = axes.imshow(toplot, **params)

        xa = axes.get_xaxis()
        ya = axes.get_yaxis()

        xa.set_major_formatter(FuncFormatter(self.time_formatter))

        if linear:
            # Start with a number that is divisible by 5.
            init = (self.freq_axis[0] % 5) / data.delt
            nticks = 15.
            # Calculate MHz difference between major ticks.
            dist = (self.freq_axis[0] - self.freq_axis[-1]) / nticks
            # Round to next multiple of 10, at least ten.
            dist = max(round(dist, -1), 10)
            # One pixel in image space is data.delt MHz, thus we can convert
            # our distance between the major ticks into image space by dividing
            # it by data.delt.

            ya.set_major_locator(IndexLocator(dist / data.delt, init))
            ya.set_minor_locator(IndexLocator(dist / data.delt / 10, init))

            def freq_fmt(x, pos):
                # This is necessary because matplotlib somehow tries to get
                # the mid-point of the row, which we do not need here.
                x = x + 0.5
                return self.format_freq(self.freq_axis[0] - x * data.delt)
        else:
            freq_fmt = _list_formatter(freqs, self.format_freq)
            ya.set_major_locator(MaxNLocator(integer=True, steps=[1, 5, 10]))

        ya.set_major_formatter(FuncFormatter(freq_fmt))

        axes.set_xlabel(self.t_label)
        axes.set_ylabel(self.f_label)
        # figure.suptitle(self.content)

        figure.suptitle(' '.join([
            get_day(self.start).strftime("%d %b %Y"),
            'Radio flux density',
            '(' + ', '.join(self.instruments) + ')',
        ]))

        for tl in xa.get_ticklabels():
            tl.set_fontsize(10)
            tl.set_rotation(30)
        figure.add_axes(axes)
        figure.subplots_adjust(bottom=0.2)
        figure.subplots_adjust(left=0.2)

        if showz:
            axes.format_coord = self._mk_format_coord(
                data,
                figure.gca().format_coord)

        if colorbar:
            if len(figure.axes) > 1:
                Colorbar(figure.axes[1], im).set_label("Intensity")
            else:
                figure.colorbar(im).set_label("Intensity")

        for overlay in overlays:
            figure, axes = overlay(figure, axes)

        for ax in figure.axes:
            ax.autoscale()
        if isinstance(figure, SpectroFigure):
            figure._init(self, freqs)
        return axes
Exemplo n.º 12
0
    'Item B': 109472,
    'Item C': 109438,
    'Item D': 16838,
    'Item E': 986438,
    'Item F': 809438,
    'Item G': 154438,
    'Item H': 109860,
    'Item I': 187438,
    'Item J': 105438,
}
item = tuple(data.keys())
count = tuple(data.values())

# Pengaturan Style
plt.style.use('seaborn')


# Pengaturan Format pada Ticker
def ribuan(x, pos):
    return f'{int(x/1000)}K'


formatter = FuncFormatter(ribuan)
ax.xaxis.set_major_formatter(formatter)

# Simple Plot
fig, ax = plt.subplots()
ax.barh(item, count)

plt.show()
Exemplo n.º 13
0
def mpl_bubble(
    x,
    y,
    z,
    labels,
    title,
    x_title,
    y_title,
    x_fmt="{:.0%}",
    y_fmt="{:+.0%}",
    y_avg_line=False,
    y_avg_value=None,
    y_avg_label="",
    x_avg_line=False,
    x_avg_value=None,
    x_avg_label="",
    x_max=None,
    x_min=None,
    y_max=None,
    y_min=None,
    show_label=True,
    label_limit=15,
    z_scale=1,
    color_scheme="随机颜色方案",
    color_list=None,
):

    z = [x * z_scale for x in z]  # 气泡大小系数

    fig, ax = plt.subplots()  # 准备画布和轴
    fig.set_size_inches(15, 10)  # 画布尺寸

    # 手动强制xy轴最小值/最大值
    if x_min is not None and x_min > min(x):
        ax.set_xlim(xmin=x_min)
    if x_max is not None and x_max < max(x):
        ax.set_xlim(xmax=x_max)
    if y_min is not None and y_min > min(y):
        ax.set_ylim(ymin=y_min)
    if y_max is not None and y_max < max(y):
        ax.set_ylim(ymax=y_max)

    # 确定颜色方案
    if color_scheme == "随机颜色方案" or color_scheme is None:
        cmap = mpl.colors.ListedColormap(np.random.rand(256, 3))
        colors = iter(cmap(np.linspace(0, 1, len(x))))
    else:
        if len(x) <= len(color_list):
            colors = color_list[: len(x)]
        else:
            colors = []
            for i in range(len(x)):
                colors.append(color_list[i % len(color_list)])
        colors = iter(colors)

    # 绘制气泡
    for i in range(len(x)):
        ax.scatter(x[i], y[i], z[i], color=next(colors), alpha=0.6, edgecolors="black")

    # 添加系列标签,用adjust_text包保证标签互不重叠
    if show_label is True:
        texts = [
            plt.text(
                x[i],
                y[i],
                labels[i],
                ha="center",
                va="center",
                multialignment="center",
                fontproperties=myfont,
                fontsize=10,
            )
            for i in range(len(labels[:label_limit]))
        ]
        adjust_text(texts, force_text=0.5, arrowprops=dict(arrowstyle="->", color="black"))

    # 添加分隔线(均值,中位数,0等)
    if y_avg_line is True:
        ax.axhline(y_avg_value, linestyle="--", linewidth=1, color="grey")
        plt.text(
            ax.get_xlim()[1],
            y_avg_value,
            y_avg_label,
            ha="left",
            va="center",
            color="r",
            multialignment="center",
            fontproperties=myfont,
            fontsize=10,
        )
    if x_avg_line is True:
        ax.axvline(x_avg_value, linestyle="--", linewidth=1, color="grey")
        plt.text(
            x_avg_value,
            ax.get_ylim()[1],
            x_avg_label,
            ha="left",
            va="top",
            color="r",
            multialignment="center",
            fontproperties=myfont,
            fontsize=10,
        )

    # 设置轴标签格式
    ax.xaxis.set_major_formatter(FuncFormatter(lambda y, _: x_fmt.format(y)))
    ax.yaxis.set_major_formatter(FuncFormatter(lambda y, _: y_fmt.format(y)))

    # 添加图表标题和轴标题
    plt.title(title, fontproperties=myfont)
    plt.xlabel(x_title, fontproperties=myfont, fontsize=12)
    plt.ylabel(y_title, fontproperties=myfont, fontsize=12)

    """以下部分绘制回归拟合曲线及CI和PI
    参考
    http://nbviewer.ipython.org/github/demotu/BMC/blob/master/notebooks/CurveFitting.ipynb
    https://stackoverflow.com/questions/27164114/show-confidence-limits-and-prediction-limits-in-scatter-plot
    """
    n = y.size  # 观察例数
    if n > 2:  # 数据点必须大于cov矩阵的scale
        p, cov = np.polyfit(x, y, 1, cov=True)  # 简单线性回归返回parameter和covariance
        poly1d_fn = np.poly1d(p)  # 拟合方程
        y_model = poly1d_fn(x)  # 拟合的y值
        m = p.size  # 参数个数

        dof = n - m  # degrees of freedom
        t = stats.t.ppf(0.975, dof)  # 显著性检验t值

        # 拟合结果绘图
        ax.plot(x, y_model, "-", color="0.1", linewidth=1.5, alpha=0.5, label="Fit")

        # 误差估计
        resid = y - y_model  # 残差
        s_err = np.sqrt(np.sum(resid ** 2) / dof)  # 标准误差

        # 拟合CI和PI
        x2 = np.linspace(np.min(x), np.max(x), 100)
        y2 = poly1d_fn(x2)

        # CI计算和绘图
        ci = t * s_err * np.sqrt(1 / n + (x2 - np.mean(x)) ** 2 / np.sum((x - np.mean(x)) ** 2))
        ax.fill_between(x2, y2 + ci, y2 - ci, color="#b9cfe7", edgecolor="", alpha=0.5)

        # Pi计算和绘图
        pi = t * s_err * np.sqrt(1 + 1 / n + (x2 - np.mean(x)) ** 2 / np.sum((x - np.mean(x)) ** 2))
        ax.fill_between(x2, y2 + pi, y2 - pi, color="None", linestyle="--")
        ax.plot(x2, y2 - pi, "--", color="0.5", label="95% Prediction Limits")
        ax.plot(x2, y2 + pi, "--", color="0.5")

    # 保存到字符串
    sio = BytesIO()
    plt.savefig(sio, format="png", bbox_inches="tight", transparent=True, dpi=600)
    data = base64.encodebytes(sio.getvalue()).decode()  # 解码为base64编码的png图片数据
    src = "data:image/png;base64," + str(data)  # 增加Data URI scheme

    # 关闭绘图进程
    plt.clf()
    plt.cla()
    plt.close()

    return src
Exemplo n.º 14
0
def _deduce_locators_formatters(max_ticks, data):
    from datetime import timedelta as tdelta
    import matplotlib.dates as mdates
    from matplotlib.ticker import FuncFormatter
    data_interval_seconds = (data[-1] - data[0]) / tdelta(seconds=1)
    interval_seconds = data_interval_seconds / max_ticks

    if interval_seconds < tdelta(minutes=0.5).total_seconds():
        # print("xticks: seconds")
        unit_multiple = _next_largest(interval_seconds, INTERVALS['SECONDLY'])
        timedelta = tdelta(seconds=unit_multiple)
        return (mdates.SecondLocator(bysecond=range(0, 60, unit_multiple)),
                FuncFormatter(
                    _get_dynamic_formatter(timedelta, '%M%:S', '%-Hh',
                                           '%-d %b')))
    elif interval_seconds < tdelta(hours=0.5).total_seconds():
        # print("xticks: minutes")
        unit_multiple = _next_largest(
            interval_seconds / tdelta(minutes=1).total_seconds(),
            INTERVALS['MINUTELY'])
        timedelta = tdelta(minutes=unit_multiple)
        return (mdates.MinuteLocator(byminute=range(0, 60, unit_multiple)),
                FuncFormatter(
                    _get_dynamic_formatter(timedelta, '%H%:M', '%-d %b',
                                           '%Y')))
    elif interval_seconds < tdelta(days=0.5).total_seconds():
        # print("xticks: hours")
        unit_multiple = _next_largest(
            interval_seconds / tdelta(hours=1).total_seconds(),
            INTERVALS['HOURLY'])
        timedelta = tdelta(hours=unit_multiple)
        return (mdates.HourLocator(byhour=range(0, 24, unit_multiple)),
                FuncFormatter(
                    _get_dynamic_formatter(timedelta, '%-Hh', '%-d %b', '%Y')))
    elif interval_seconds < tdelta(days=3).total_seconds():
        # print("xticks: days")
        unit_multiple = _next_largest(
            interval_seconds / tdelta(days=1).total_seconds(),
            INTERVALS['DAILY'])
        timedelta = tdelta(days=unit_multiple)
        return (mdates.WeekdayLocator(byweekday=range(0, 7, unit_multiple)),
                FuncFormatter(
                    _get_dynamic_formatter(timedelta, '%-d', '%b', '%Y')))
    elif interval_seconds < tdelta(days=14).total_seconds():
        # print("xticks: weeks")
        unit_multiple = _next_largest(
            interval_seconds / tdelta(weeks=1).total_seconds(),
            INTERVALS['WEEKLY'])
        timedelta = tdelta(days=unit_multiple * 7)
        return (mdates.WeekdayLocator(byweekday=0, interval=unit_multiple),
                FuncFormatter(
                    _get_dynamic_formatter(timedelta, '%-d', '%b', '%Y')))
    elif interval_seconds < tdelta(weeks=26).total_seconds():
        # print("xticks: months")
        unit_multiple = _next_largest(
            interval_seconds / tdelta(weeks=4).total_seconds(),
            INTERVALS['MONTHLY'])
        timedelta = tdelta(weeks=unit_multiple * 4)
        return (mdates.MonthLocator(bymonth=range(1, 13, unit_multiple)),
                FuncFormatter(_get_dynamic_formatter(timedelta, '%b', '%Y')))
    else:
        # print("xticks: years")
        unit_multiple = _next_largest(
            interval_seconds / tdelta(weeks=52).total_seconds(),
            INTERVALS['YEARLY'])
        return (mdates.YearLocator(base=unit_multiple),
                mdates.DateFormatter('%Y'))
Exemplo n.º 15
0
def animate(i):
    # Add next value
    data.append(np.random.randint(0, max_rand))
    line.set_ydata(data)
    # plt.savefig('e:\\python temp\\fig_{:02}'.format(i))
    print(i)
    return line,


max_x = 100
max_rand = 5

data = deque(np.zeros(max_x), maxlen=max_x)  # hold the last values
x = np.arange(0, max_x)

fig, ax = plt.subplots()
ax.set_ylim(0, max_rand)
ax.set_xlim(0, max_x - 1)
line, = ax.plot(x, np.random.randint(0, max_rand, max_x))
ax.xaxis.set_major_formatter(
    FuncFormatter(lambda x, pos: '{:.0f}s'.format(max_x - x - 1)))
plt.xlabel('Seconds ago')

ani = animation.FuncAnimation(fig,
                              animate,
                              init_func=init,
                              interval=10,
                              blit=True,
                              save_count=10)

plt.show()
Exemplo n.º 16
0
# ax = dfn.plot.bar(x='Fieldwork sites', y = 'Fieldwork Budget (php)', rot = 0, color = "orange", title = "Comparison of Maintenance Budget for Different Clustered Sites ")
# yticks([0 50 100])
# yticklabels({'y = 0','y = 50','y = 100'})  

from matplotlib.ticker import FuncFormatter
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(2)
money = dfn["Fieldwork Budget (php)"].values


def thousands(x, pos):
    'The two args are the value and tick position'
    return 'P%1.1fK' % (x * 1e-3)


formatter = FuncFormatter(thousands)

fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(formatter)
plt.bar(dfn["Fieldwork sites"], dfn["Fieldwork Budget (php)"], color = "orange")
plt.xticks(x, dfn["Fieldwork sites"].values)
plt.title("Comparison of Maintenance Cost for Different Clustered Sites", fontsize = 20)
plt.xlabel("Fieldwork sites")
plt.ylabel("Fieldwork Budget (php)")
plt.show()



Exemplo n.º 17
0
def plot_plan_and_vert(x, y, z, dataxy, datazx, datazy, unit="",
                       title="", saveto="", **kwargs):
    """Plot 2-D plan view of ``dataxy`` together with vertical sections \
    ``dataxz`` and ``datazy``

    Parameters
    ----------
    x : :class:`numpy:numpy.ndarray`
        array of x-axis coordinates
    y : :class:`numpy:numpy.ndarray`
        array of y-axis coordinates
    z : :class:`numpy:numpy.ndarray`
        array of z-axis coordinates
    dataxy : :class:`numpy:numpy.ndarray`
        2d array of shape (len(x), len(y))
    datazx : :class:`numpy:numpy.ndarray`
        2d array of shape (len(z), len(x))
    datazy : :class:`numpy:numpy.ndarray`
        2d array of shape (len(z), len(y))
    unit : string
        unit of data arrays
    title: string
        figure title
    saveto : string
        file path if figure should be saved

    Keyword Arguments
    -----------------
    **kwargs : other kwargs which can be passed to \
               :func:`matplotlib.pyplot.contourf`

    """

    pl.figure(figsize=(10, 10))

    # define axes
    left, bottom, width, height = 0.1, 0.1, 0.6, 0.2
    ax_xy = pl.axes((left, bottom, width, width))
    ax_x = pl.axes((left, bottom + width, width, height))
    ax_y = pl.axes((left + width, bottom, height, width))
    ax_cb = pl.axes((left + width + height + 0.02, bottom, 0.02, width))

    # set axis label formatters
    ax_x.xaxis.set_major_formatter(NullFormatter())
    ax_y.yaxis.set_major_formatter(NullFormatter())

    # draw CAPPI
    pl.sca(ax_xy)
    xy = pl.contourf(x, y, dataxy, **kwargs)
    pl.grid(color="grey", lw=1.5)

    # draw colorbar
    cb = pl.colorbar(xy, cax=ax_cb)
    cb.set_label("(%s)" % unit)

    # draw upper vertical profil
    ax_x.contourf(x, z, datazx, **kwargs)

    # draw right vertical profil
    ax_y.contourf(z, y, datazy.T, **kwargs)

    # label axes
    ax_xy.set_xlabel('x (km)')
    ax_xy.set_ylabel('y (km)')
    ax_x.set_xlabel('')
    ax_x.set_ylabel('z (km)')
    ax_y.set_ylabel('')
    ax_y.set_xlabel('z (km)')

    def xycoords(x, pos):
        """The two args are the value and tick position"""
        return "%d" % (x / 1000.)

    xyformatter = FuncFormatter(xycoords)

    def zcoords(x, pos):
        """The two args are the value and tick position"""
        return ("%.1f" % (x / 1000.)).rstrip('0').rstrip('.')

    zformatter = FuncFormatter(zcoords)

    ax_xy.xaxis.set_major_formatter(xyformatter)
    ax_xy.yaxis.set_major_formatter(xyformatter)
    ax_x.yaxis.set_major_formatter(zformatter)
    ax_y.xaxis.set_major_formatter(zformatter)

    if not title == "":
        # add a title - here, we have to create a new axes object which will
        # be invisible then the invisible axes will get a title
        tax = pl.axes((left, bottom + width + height + 0.01,
                       width + height, 0.01), frameon=False, facecolor="none")
        tax.get_xaxis().set_visible(False)
        tax.get_yaxis().set_visible(False)
        pl.title(title)
    if saveto == '':
        # show plot
        pl.show()
        if not pl.isinteractive():
            # close figure eplicitely if pylab is not in interactive mode
            pl.close()
    else:
        # save plot to file
        if ((os.path.exists(os.path.dirname(saveto))) or
                (os.path.dirname(saveto) == '')):
            pl.savefig(saveto)
            pl.close()
Exemplo n.º 18
0
    def draw(
        cls,
        series_defs: List[SeriesDef],
        raw_samples: List[CurveData],
        fit_samples: List[CurveData],
        tick_labels: Dict[str, str],
        fit_data: FitData,
        result_entries: List[AnalysisResultData],
        style: Optional[PlotterStyle] = None,
        axis: Optional["matplotlib.axes.Axes"] = None,
    ) -> "pyplot.Figure":
        """Create a fit result of all curves in the single canvas.

        Args:
            series_defs: List of definition for each curve.
            raw_samples: List of raw sample data for each curve.
            fit_samples: List of formatted sample data for each curve.
            tick_labels: Dictionary of axis label information. Axis units and label for x and y
                value should be explained.
            fit_data: fit data generated by the analysis.
            result_entries: List of analysis result data entries. If
                :py:class:`~qiskit_experiments.database_service.db_fitval.FitVal` object is a value,
                that entry will be shown in the fit report.
            style: Optional. A configuration object to modify the appearance of the figure.
            axis: Optional. A matplotlib Axis object.

        Returns:
            A matplotlib figure of the curve fit result.
        """
        if axis is None:
            axis = get_non_gui_ax()

            # update image size to experiment default
            figure = axis.get_figure()
            figure.set_size_inches(*style.figsize)
        else:
            figure = axis.get_figure()

        # get canvas number
        n_subplots = max(series_def.canvas for series_def in series_defs) + 1

        # use inset axis. this allows us to draw multiple canvases on a given single axis object
        inset_ax_h = (1 - (0.05 * (n_subplots - 1))) / n_subplots
        inset_axes = [
            axis.inset_axes(
                [0, 1 - (inset_ax_h + 0.05) * n_axis - inset_ax_h, 1, inset_ax_h],
                transform=axis.transAxes,
                zorder=1,
            )
            for n_axis in range(n_subplots)
        ]

        # show x label only in the bottom canvas
        for inset_axis in inset_axes[:-1]:
            inset_axis.set_xticklabels([])
        inset_axes[-1].get_shared_x_axes().join(*inset_axes)

        # remove original axis frames
        axis.spines.right.set_visible(False)
        axis.spines.left.set_visible(False)
        axis.spines.top.set_visible(False)
        axis.spines.bottom.set_visible(False)
        axis.set_xticks([])
        axis.set_yticks([])

        # collect data source per canvas
        plot_map = defaultdict(list)
        for curve_ind, series_def in enumerate(series_defs):
            plot_map[series_def.canvas].append(curve_ind)

        y_labels = tick_labels["ylabel"].split(",")
        if len(y_labels) == 1:
            y_labels = y_labels * n_subplots

        for ax_ind, curve_inds in plot_map.items():
            inset_axis = inset_axes[ax_ind]

            for curve_ind in curve_inds:
                draw_single_curve_mpl(
                    axis=inset_axis,
                    series_def=series_defs[curve_ind],
                    raw_sample=raw_samples[curve_ind],
                    fit_sample=fit_samples[curve_ind],
                    fit_data=fit_data,
                )

            # add legend to each inset axis
            if len(curve_inds) > 1:
                inset_axis.legend(loc=style.legend_loc)

            # format y axis tick value of each inset axis
            yaxis = getattr(inset_axis, "yaxis")
            unit = tick_labels["yval_unit"]
            label = y_labels[ax_ind]
            if unit:
                maxv = np.max(np.abs(yaxis.get_data_interval()))
                scaled_maxv, prefix = detach_prefix(maxv, decimal=3)
                prefactor = scaled_maxv / maxv
                # pylint: disable=cell-var-from-loop
                yaxis.set_major_formatter(FuncFormatter(lambda x, p: f"{x * prefactor: .3g}"))
                yaxis.set_label_text(f"{label} [{prefix}{unit}]", fontsize=style.axis_label_size)
            else:
                inset_axis.ticklabel_format(axis="y", style="sci", scilimits=(-3, 3))
                yaxis.set_label_text(label, fontsize=style.axis_label_size)

            if tick_labels["ylim"]:
                inset_axis.set_ylim(tick_labels["ylim"])

        # format x axis
        xaxis = getattr(inset_axes[-1], "xaxis")
        unit = tick_labels["xval_unit"]
        label = tick_labels["xlabel"]
        if unit:
            maxv = np.max(np.abs(xaxis.get_data_interval()))
            scaled_maxv, prefix = detach_prefix(maxv, decimal=3)
            prefactor = scaled_maxv / maxv
            # pylint: disable=cell-var-from-loop
            xaxis.set_major_formatter(FuncFormatter(lambda x, p: f"{x * prefactor: .3g}"))
            xaxis.set_label_text(f"{label} [{prefix}{unit}]", fontsize=style.axis_label_size)
        else:
            axis.ticklabel_format(axis="x", style="sci", scilimits=(-3, 3))
            xaxis.set_label_text(label, fontsize=style.axis_label_size)

        if tick_labels["xlim"]:
            inset_axes[-1].set_xlim(tick_labels["xlim"])

        # write analysis report
        if fit_data:
            report_str = write_fit_report(result_entries)
            report_str += r"Fit $\chi^2$ = " + f"{fit_data.reduced_chisq: .4g}"

            report_handler = axis.text(
                *style.fit_report_rpos,
                report_str,
                ha="center",
                va="top",
                size=style.fit_report_text_size,
                transform=axis.transAxes,
            )

            bbox_props = dict(boxstyle="square, pad=0.3", fc="white", ec="black", lw=1, alpha=0.8)
            report_handler.set_bbox(bbox_props)

        axis.tick_params(labelsize=style.tick_label_size)
        axis.grid(True)

        return figure
Exemplo n.º 19
0
    def y_format(x, y):
        return '{:.0f}'.format(
            1 if round(10**x - 1, -1) == 0 else round(10**x - 1, -1)
        )  # not 100% accurate binning, but the -1 is so we can label the bottom of the colorbar as 0, doesn't throw off calc by much
        #return '{:.0f}'.format(round(10**x-1,-1) if 10**x-1 != 1 else 1) # not 100% accurate binning, but the -1 is so we can label the bottom of the colorbar as 0, doesn't throw off calc by much

    counts, edges = np.histogram(g.get_array(), bins=8)
    cbar = fig.colorbar(g,
                        ax=ax,
                        orientation='vertical',
                        extend='both',
                        extendrect=True,
                        drawedges=False,
                        ticks=edges,
                        format=FuncFormatter(y_format))
    cbar.set_label('Number of Regions', rotation=270, labelpad=20)
    #pearson correlation
    pr, pval = pearsonr(ccr, gerp)
    npr, npval = pearsonr(ngerps, nccrs)
    plt.tight_layout()
    sns.despine()
    plt.xlabel("CCR\n" + str(ct) + " out of " + str(total) +
               " unique regions in red box")
    plt.ylabel("Mean GERP++ score")
    print "Pearson's r: " + str(pr) + "\np-value: " + str(pval)
    print "Pearson's r >=20: " + str(npr) + "\np-value: " + str(npval)
    plt.savefig(sys.argv[2], bbox_inches='tight')

    # making table for supplement
    f = open(sys.argv[3], 'w')
Exemplo n.º 20
0
    def draw(
        cls,
        series_defs: List[SeriesDef],
        raw_samples: List[CurveData],
        fit_samples: List[CurveData],
        tick_labels: Dict[str, str],
        fit_data: FitData,
        result_entries: List[AnalysisResultData],
        style: Optional[PlotterStyle] = None,
        axis: Optional["matplotlib.axes.Axes"] = None,
    ) -> "pyplot.Figure":
        """Create a fit result of all curves in the single canvas.

        Args:
            series_defs: List of definition for each curve.
            raw_samples: List of raw sample data for each curve.
            fit_samples: List of formatted sample data for each curve.
            tick_labels: Dictionary of axis label information. Axis units and label for x and y
                value should be explained.
            fit_data: fit data generated by the analysis.
            result_entries: List of analysis result data entries. If
                :py:class:`~qiskit_experiments.database_service.db_fitval.FitVal` object is a value,
                that entry will be shown in the fit report.
            style: Optional. A configuration object to modify the appearance of the figure.
            axis: Optional. A matplotlib Axis object.

        Returns:
            A matplotlib figure of the curve fit result.
        """
        if axis is None:
            axis = get_non_gui_ax()

            # update image size to experiment default
            figure = axis.get_figure()
            figure.set_size_inches(*style.figsize)
        else:
            figure = axis.get_figure()

        # draw all curves on the same canvas
        for series_def, raw_samp, fit_samp in zip(series_defs, raw_samples, fit_samples):
            draw_single_curve_mpl(
                axis=axis,
                series_def=series_def,
                raw_sample=raw_samp,
                fit_sample=fit_samp,
                fit_data=fit_data,
            )

        # add legend
        if len(series_defs) > 1:
            axis.legend(loc=style.legend_loc)

        # get axis scaling factor
        for this_axis in ("x", "y"):
            sub_axis = getattr(axis, this_axis + "axis")
            unit = tick_labels[this_axis + "val_unit"]
            label = tick_labels[this_axis + "label"]
            if unit:
                maxv = np.max(np.abs(sub_axis.get_data_interval()))
                scaled_maxv, prefix = detach_prefix(maxv, decimal=3)
                prefactor = scaled_maxv / maxv
                # pylint: disable=cell-var-from-loop
                sub_axis.set_major_formatter(FuncFormatter(lambda x, p: f"{x * prefactor: .3g}"))
                sub_axis.set_label_text(f"{label} [{prefix}{unit}]", fontsize=style.axis_label_size)
            else:
                sub_axis.set_label_text(label, fontsize=style.axis_label_size)
                axis.ticklabel_format(axis=this_axis, style="sci", scilimits=(-3, 3))

        if tick_labels["xlim"]:
            axis.set_xlim(tick_labels["xlim"])

        if tick_labels["ylim"]:
            axis.set_ylim(tick_labels["ylim"])

        # write analysis report
        if fit_data:
            report_str = write_fit_report(result_entries)
            report_str += r"Fit $\chi^2$ = " + f"{fit_data.reduced_chisq: .4g}"

            report_handler = axis.text(
                *style.fit_report_rpos,
                report_str,
                ha="center",
                va="top",
                size=style.fit_report_text_size,
                transform=axis.transAxes,
            )

            bbox_props = dict(boxstyle="square, pad=0.3", fc="white", ec="black", lw=1, alpha=0.8)
            report_handler.set_bbox(bbox_props)

        axis.tick_params(labelsize=style.tick_label_size)
        axis.grid(True)

        return figure
Exemplo n.º 21
0
def plot_figure(filename,
                varname,
                vmin=None,
                vmax=None,
                figsize=None,
                ax=None):
    """
    Plot the given variable for each file from a simulation
    """

    global verbose, title_info

    if verbose > 1:
        print('Plotting {} from file {}'.format(varname, filename))
    axis_type = 1  # 0 - off, 1 - left+bottom, 2 - all

    if ax is not None:
        fig = ax.get_figure()
    elif figsize is None:
        fig, ax = plt.subplots(1, 1, figsize=(8, 8))
    else:
        fig, ax = plt.subplots(1, 1, figsize=figsize)

    if verbose > 1:
        print('Adjusting subplot')

    if axis_type == 0:
        plt.subplots_adjust(left=0,
                            right=0.9,
                            bottom=0.05,
                            top=0.92,
                            wspace=0,
                            hspace=0)
    else:
        plt.subplots_adjust(left=0.1,
                            right=0.9,
                            bottom=0.1,
                            top=0.92,
                            wspace=0,
                            hspace=0)
    plt.set_cmap(cm.coolwarm)

    if verbose > 1:
        print('Reading data')

    # Draw initial plot
    f = filename
    data = sdf.read(f)
    var = data.__dict__[varname]
    grid = var.grid
    vdata = var.data

    i0 = 1
    i1 = 0
    if len(grid.dims) == 2:
        x = grid.data[i0]
        y = grid.data[i1]
    else:
        if (grid.dims[0] == 1):
            orig = var.grid.name.replace('/', '_') + '_Orig'
            grid = data.__dict__[orig]
            i0 = 2
            i1 = 1
            x = grid.data[i0]
            y = grid.data[i1]
        elif (grid.dims[1] == 1):
            i0 = 0
            i1 = 1
            x = grid.data[0][:, 0, :]
            y = grid.data[1][:, 0, :]
        else:
            i0 = 0
            i1 = 2
            x = grid.data[0][:, :, 0]
            y = grid.data[1][:, :, 0]
            z = grid.data[2][:, :, 0]
            x = np.sqrt(x * x + y * y)
            y = z

    if verbose > 1:
        print('Scaling data')

    vdata = vdata

    if vmin is None:
        vmin, vmax = get_var_range_from_sdf_files((filename, ), varname)

    if verbose > 1:
        print('Plotting data')
    im = ax.pcolormesh(x, y, vdata, vmin=vmin, vmax=vmax)

    if verbose > 1:
        print('Plotting axes')

    xmult, xsym = get_si_prefix(np.max(x) - np.min(x))
    ymult, ysym = get_si_prefix(np.max(y) - np.min(y))
    if verbose > 1:
        print('Scale axis by {} ({}, {})'.format(xmult, np.min(x), np.max(x)))

    ax.xaxis.set_major_formatter(
        FuncFormatter(lambda x, y: '{:.2f}'.format(x * xmult)))
    ax.yaxis.set_major_formatter(
        FuncFormatter(lambda x, y: '{:.2f}'.format(x * ymult)))

    ax.set_xlabel(grid.labels[i0] + ' $(' + xsym + grid.units[i0] + ')$')
    ax.set_ylabel(grid.labels[i1] + ' $(' + ysym + grid.units[i1] + ')$')
    ax.axis('image')

    if axis_type == 0:
        ax.axis('off')
    elif axis_type == 1:
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.get_xaxis().tick_bottom()
        ax.get_yaxis().tick_left()

    # Add colorbar
    mult, sym = get_si_prefix(vmax - vmin)
    if verbose > 0 and plot_figure.first:
        plot_figure.first = False
        print('Scale colorbar by {} ({}, {})'.format(mult, vmin, vmax))

    data_label = var.name + ' $(' + sym + var.units + ')$'
    if title_info:
        legend_label = ''
    else:
        legend_label = data_label

    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", "5%", pad="5%")
    cbar = fig.colorbar(
        im,
        cax=cax,
        ax=ax,
        label=legend_label,
        format=FuncFormatter(lambda x, y: '{:.2f}'.format(x * mult)))

    if title_info:
        title_label = data_label + ', '
    else:
        title_label = ''

    fig.fs = 'large'
    fig.suptitle(
        title_label +
        'step={}, time={}'.format(data.Header['step'], data.Header['time']),
        fontsize=fig.fs,
        y=0.98)
    fig.sca(ax)
    fig.canvas.draw()

    if verbose > 1:
        print('Done plotting {} from file {}'.format(varname, filename))

    return im, ax
pstring = lambda i: ("+" if i[0] != '-' else "") + str(i)
BOX_Change = pstring("{0:.2%}".format(
    float(sub(r"[^\d.]", "", series.at[number_of_rows - 1, "BOX Price"])) /
    float(sub(r"[^\d.]", "", series.at[0, "BOX Price"])) - 1))
BTC_Change = pstring("{0:.2%}".format(
    float(sub(r"[^\d.]", "", series.at[number_of_rows - 1, "BTC Price"])) /
    float(sub(r"[^\d.]", "", series.at[0, "BTC Price"])) - 1))
# EOS_Change = pstring("{0:.2%}".format(float(sub(r"[^\d.]", "", series.at[number_of_rows - 1, "EOS Price"]))/float(sub(r"[^\d.]", "", series.at[0, "EOS Price"])) - 1))
# XIN_Change = pstring("{0:.2%}".format(float(sub(r"[^\d.]", "", series.at[number_of_rows - 1, "XIN Price"]))/float(sub(r"[^\d.]", "", series.at[0, "XIN Price"])) - 1))
RIBOX_Change = pstring("{0:.2%}".format(
    float(series.at[number_of_rows - 1, "BOX Value Accumulated"] /
          series.at[number_of_rows - 1, "Total Invested"] - 1)))

# draw the figure
ax = plt.gca()
ax.yaxis.set_major_formatter(FuncFormatter(lambda y, _: '{:.0%}'.format(y)))
# series.plot(kind='line', x='Date', y='BTC', ax=ax, figsize = (20,10), color="red")
# series.plot(kind='line', x='Date', y='EOS', ax=ax, figsize = (20,10), color="brown")
# series.plot(kind='line', x='Date', y='XIN', ax=ax, figsize = (20,10), color="purple")
series.plot(kind='line',
            x='Date',
            y='BOX',
            ax=ax,
            figsize=(20, 10),
            color="green")
series.plot(kind='line',
            x='Date',
            y='RI-BOX',
            ax=ax,
            figsize=(20, 10),
            color="blue")
Exemplo n.º 23
0
def plot_hist(lengths,
              x_name,
              y_name,
              show=True,
              img_out=None,
              title=None,
              bin_width=10):
    """
    Given a list of TCP payload lengths (or some other kind of 1D data),
    plot a historam showing the distribution of these payload lengths.
    
    :param list lengths: a list of integer payload lengths.
    :param str x_name: the name of the x-axis property.
    :param str y_name: the name of the y-axis property.
    :param bool show: if True, show the plot through a GUI interface.
    :param str img_out: if set, output the plot to the path specified.
    :param str title: if set, display the title as specified in string.
    :param int bin_width: if set decides the width of bins, 10 by default.
    :returns: True if plot successfully, False otherwise.
    """

    if any([not isinstance(x, int) or x < 0 for x in lengths]):
        return False

    if not isinstance(x_name, str) or not isinstance(y_name, str):
        return False

    if title and not isinstance(title, str):
        return False

    bins = floor(max(lengths) / bin_width)
    weights = np.ones_like(lengths) / (len(lengths))

    n, bins, patches = plt.hist(lengths,
                                bins=bins,
                                range=(0, max(lengths)),
                                weights=weights,
                                facecolor='g',
                                alpha=0.75)
    plt.xlabel(x_name)
    plt.ylabel(y_name)

    if title:
        plt.title(title)

    plt.grid(color='k',
             which='both',
             axis='both',
             alpha=0.25,
             linestyle='dashed')
    plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))

    if img_out is not None:
        out_path = utils.get_full_path(img_out)
        if out_path:
            plt.savefig(os.path.expanduser(out_path), dpi=200)

    if show:
        plt.show()

    return True
Exemplo n.º 24
0
 def genMatplotlibFig(self, file_path=None):
     nRow, nCol = 3, 3
     Fig = Figure(figsize=(min(32, 16 + (nCol - 1) * 8), 8 * nRow))
     xData = np.arange(1, self._Output["统计数据"].shape[0] - 1)
     xTickLabels = [str(iInd) for iInd in self._Output["统计数据"].index[:-2]]
     PercentageFormatter = FuncFormatter(_QS_formatMatplotlibPercentage)
     FloatFormatter = FuncFormatter(lambda x, pos: '%.2f' % (x, ))
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 1), xData, xTickLabels,
                        self._Output["统计数据"]["年化超额收益率"].iloc[:-2],
                        PercentageFormatter,
                        self._Output["统计数据"]["胜率"].iloc[:-2],
                        PercentageFormatter)
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 2), xData, xTickLabels,
                        self._Output["统计数据"]["信息比率"].iloc[:-2],
                        PercentageFormatter, None)
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 3), xData, xTickLabels,
                        self._Output["统计数据"]["超额最大回撤率"].iloc[:-2],
                        PercentageFormatter, None)
     _QS_plotStatistics(
         Fig.add_subplot(nRow, nCol, 4), xData, xTickLabels,
         self._Output["统计数据"]["年化收益率"].iloc[:-2], PercentageFormatter,
         pd.Series(self._Output["统计数据"].loc["市场", "年化收益率"],
                   index=self._Output["统计数据"].index[:-2],
                   name="市场"), PercentageFormatter, False)
     _QS_plotStatistics(
         Fig.add_subplot(nRow, nCol, 5), xData, xTickLabels,
         self._Output["统计数据"]["Sharpe比率"].iloc[:-2], FloatFormatter,
         pd.Series(self._Output["统计数据"].loc["市场", "Sharpe比率"],
                   index=self._Output["统计数据"].index[:-2],
                   name="市场"), FloatFormatter, False)
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 6), xData, xTickLabels,
                        self._Output["统计数据"]["平均换手率"].iloc[:-2],
                        PercentageFormatter, None)
     Axes = Fig.add_subplot(nRow, nCol, 7)
     Axes.xaxis_date()
     Axes.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))
     Axes.plot(self._Output["净值"].index,
               self._Output["净值"].iloc[:, 0].values,
               label=str(self._Output["净值"].iloc[:, 0].name),
               color="indianred",
               lw=2.5)
     Axes.plot(self._Output["净值"].index,
               self._Output["净值"].iloc[:, -3].values,
               label=str(self._Output["净值"].iloc[:, -3].name),
               color="steelblue",
               lw=2.5)
     Axes.plot(self._Output["净值"].index,
               self._Output["净值"]["市场"].values,
               label="市场",
               color="forestgreen",
               lw=2.5)
     Axes.legend(loc='best')
     Axes = Fig.add_subplot(nRow, nCol, 8)
     xData = np.arange(0, self._Output["净值"].shape[0])
     xTicks = np.arange(0, self._Output["净值"].shape[0],
                        max(1, int(self._Output["净值"].shape[0] / 8)))
     xTickLabels = [
         self._Output["净值"].index[i].strftime("%Y-%m-%d") for i in xTicks
     ]
     Axes.plot(xData,
               self._Output["净值"]["L-S"].values,
               label="多空净值",
               color="indianred",
               lw=2.5)
     Axes.legend(loc='upper left')
     RAxes = Axes.twinx()
     RAxes.yaxis.set_major_formatter(PercentageFormatter)
     RAxes.bar(xData,
               self._Output["收益率"]["L-S"].values,
               label="多空收益率",
               color="steelblue")
     RAxes.legend(loc="upper right")
     Axes.set_xticks(xTicks)
     Axes.set_xticklabels(xTickLabels)
     Axes = Fig.add_subplot(nRow, nCol, 9)
     Axes.xaxis_date()
     Axes.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))
     for i in range(self._Output["净值"].shape[1] - 2):
         Axes.plot(self._Output["净值"].index,
                   self._Output["净值"].iloc[:, i].values,
                   label=str(self._Output["净值"].iloc[:, i].name),
                   lw=2.5)
     Axes.legend(loc='best')
     if file_path is not None:
         Fig.savefig(file_path, dpi=150, bbox_inches='tight')
     return Fig
Exemplo n.º 25
0
def onpick(event):
    global pl

    global ind
    global fig2

    ind = event.ind
    ind = ind[0]
    #x_data = event.xdata
    #y_data = event.ydata

    #find ind of closest lat/lon
    #ind = modules.find_nearest_point_index(obs_lons,obs_lats,x_data,y_data)

    try:
        for i in range(len(pl)):
            pl.pop(0).remove()
            first_run = False

    except:
        first_run = True
        pass

    pl = m.plot([X[ind]], [Y[ind]],
                'o',
                ms=12,
                alpha=0.6,
                color='yellow',
                zorder=20)

    #get model timeseries for site clicked
    lat_n, lon_n = modules.obs_model_gridbox(lat_e, lon_e, obs_lats[ind],
                                             obs_lons[ind])
    model_var_pick = model_var[:, lat_n, lon_n]
    model_var_pick = model_var_pick * 1e9
    model_var_mask = np.ma.masked_where(model_var_pick <= 0, model_var_pick)

    if model_name == 'MACC':
        model_time_pd = pd.date_range(start=model_datetimes[0],
                                      end=model_datetimes[-1],
                                      freq='H')
        count = 0
        valids = []
        for i in range(len(model_time_pd)):
            if count == 0:
                valids.append(i)
                count += 1
            elif count == 2:
                count = 0
            else:
                count += 1
        model_time_pd = model_time_pd[valids]
        model_var_pd = pd.Series(model_var_mask, index=model_time_pd)
    else:
        model_time_pd = pd.date_range(start=model_datetimes[0],
                                      end=model_datetimes[-1],
                                      freq='H')
        model_var_pd = pd.Series(model_var_mask, index=model_time_pd)

    #get obs timeseries for site clicked
    ref = obs_refs[ind]
    obs_ts_group = obs_root_grp.groups[ref]
    obs_var = obs_ts_group.variables[species.lower()][:]
    group = obs_ts_group.process_group
    lat = obs_ts_group.latitude
    lon = obs_ts_group.longitude
    lon = obs_ts_group.longitude
    alt = obs_ts_group.altitude
    complete = obs_ts_group.completeness
    a_class = obs_ts_group.anthrome_class
    r_class = obs_ts_group.raw_class
    continent = loc_dict[tags[ind]]
    country = obs_ts_group.country

    obs_var_mask = np.ma.masked_where(obs_var <= 0, obs_var)
    obs_time_pd = pd.date_range(start=obs_datetimes[0],
                                end=obs_datetimes[-1],
                                freq='H')
    obs_var_pd = pd.Series(obs_var_mask, index=obs_time_pd)

    #create sine wave from amp/phase
    obs_date_l = obs_date.astype(int)
    obs_time_l = obs_time.astype(int)
    obs_times = modules.date_process(obs_date_l, obs_time_l, start_year)
    obs_times = np.array(obs_times)
    pi2 = np.pi * 2

    #convert phases to radians
    calc = pi2 / 6.
    obs_ha_phase_r = obs_ha_phase[ind] * calc
    calc = pi2 / 12.
    obs_annual_phase_r = obs_annual_phase[ind] * calc

    ha_obs_wave = obs_ha_mag[ind] * (np.cos((pi2 * obs_times / (365.25 / 2.)) -
                                            (obs_ha_phase_r)))
    annual_obs_wave = obs_annual_mag[ind] * (np.cos((pi2 * obs_times /
                                                     (365.25)) -
                                                    (obs_annual_phase_r)))
    seasonal_obs_wave = (ha_obs_wave + annual_obs_wave) + obs_ave[ind]
    obs_seasonal_wave_pd = pd.Series(seasonal_obs_wave, index=obs_time_pd)

    #create sine wave from amp/phase
    mod_date_l = model_date.astype(int)
    mod_time_l = model_time.astype(int)
    mod_times = modules.date_process(mod_date_l, mod_time_l, start_year)
    mod_times = np.array(mod_times)
    pi2 = np.pi * 2

    #convert phases to radians
    calc = pi2 / 6.
    model_ha_phase_r = model_ha_phase[ind] * calc
    calc = pi2 / 12.
    model_annual_phase_r = model_annual_phase[ind] * calc

    ha_model_wave = model_ha_mag[ind] * (np.cos((pi2 * mod_times /
                                                 (365.25 / 2.)) -
                                                (model_ha_phase_r)))
    annual_model_wave = model_annual_mag[ind] * (np.cos(
        (pi2 * mod_times / (365.25)) - (model_annual_phase_r)))
    seasonal_model_wave = (ha_model_wave + annual_model_wave) + model_ave[ind]
    model_seasonal_wave_pd = pd.Series(seasonal_model_wave,
                                       index=model_time_pd)

    #get spectra data
    site_group_obs = root_grp_obs_spec.groups[ref]
    site_group_mod = root_grp_mod_spec.groups[ref]

    obs_period = site_group_obs.variables['period'][:]
    mod_period = site_group_mod.variables['period'][:]

    obs_amp = site_group_obs.variables['amplitude'][:]
    mod_amp = site_group_mod.variables['amplitude'][:]

    fig.canvas.draw()

    if first_run == False:
        plt.close(fig2)
        fig2, (axo, axo2) = plt.subplots(2, figsize=(24, 12))
        fig2.patch.set_facecolor('white')

        #fig2 = plt.figure()

        axo.plot_date(obs_time_pd.to_pydatetime(),
                      obs_var_pd,
                      color='black',
                      markersize=3,
                      label='Observations')
        axo.plot_date(model_time_pd.to_pydatetime(),
                      model_var_pd,
                      color='red',
                      alpha=0.5,
                      markersize=3,
                      label='%s %s %s %s' %
                      (model_name, version, grid_size, met),
                      markeredgecolor='None')
        axo.plot_date(obs_time_pd.to_pydatetime(),
                      obs_seasonal_wave_pd,
                      color='yellow',
                      markersize=3,
                      label='Obs Seasonal Waveform',
                      markeredgecolor='None')
        axo.plot_date(model_time_pd.to_pydatetime(),
                      model_seasonal_wave_pd,
                      color='green',
                      markersize=3,
                      label='Model Seasonal Waveform',
                      markeredgecolor='None')

        axo2.loglog(obs_period, obs_amp, color='black', label='Obs')
        axo2.loglog(mod_period,
                    mod_amp,
                    color='red',
                    label='%s %s %s %s' %
                    (model_name, version, grid_size, met))

        axo2.text(0.01,
                  0.95,
                  'Obs D Amp = %8.2f ppb' % (obs_daily_mag[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.92,
                  'Model D Amp = %8.2f ppb' % (model_daily_mag[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        axo2.text(0.01,
                  0.85,
                  'Obs HA Amp = %8.2f ppb' % (obs_ha_mag[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.82,
                  'Model HA Amp = %8.2f ppb' % (model_ha_mag[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        axo2.text(0.01,
                  0.75,
                  'Obs A Amp = %8.2f ppb' % (obs_annual_mag[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.72,
                  'Model A Amp = %8.2f ppb' % (model_annual_mag[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        axo2.text(0.01,
                  0.55,
                  'Obs D Phase = %8.2f' % (obs_daily_phase[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.52,
                  'Model D Phase = %8.2f' % (model_daily_phase[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        axo2.text(0.01,
                  0.45,
                  'Obs HA Phase = %8.2f' % (obs_ha_phase[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.42,
                  'Model HA Phase = %8.2f' % (model_ha_phase[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        obs_a_ph = obs_annual_phase[ind]
        mod_a_ph = model_annual_phase[ind]

        if obs_a_ph > 12:
            obs_a_ph = obs_a_ph - 12.
        if mod_a_ph > 12:
            mod_a_ph = mod_a_ph - 12.

        axo2.text(0.01,
                  0.35,
                  'Obs A Phase = %8.2f' % (obs_a_ph),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.32,
                  'Model A Phase = %8.2f' % (mod_a_ph),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        axo2.text(0.01,
                  0.15,
                  'Obs Ave = %8.2f ppb' % (obs_ave[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.12,
                  'Model Ave = %8.2f ppb' % (model_ave[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        axo2.axvline(1., ymin=0, ymax=1, color='blue', linestyle='--')
        axo2.axvline(182.625, ymin=0, ymax=1, color='blue', linestyle='--')
        axo2.axvline(365.25, ymin=0, ymax=1, color='blue', linestyle='--')

        axo2.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
        axo2.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
        plt.gca().xaxis.set_major_formatter(FuncFormatter(xformatter))
        plt.gca().yaxis.set_major_formatter(FuncFormatter(yformatter))

        axo.set_title(
            'Site = %s, Country = %s, Continent = %s, Process Group = %s, Lat = %s, Lon = %s, Alt = %sm,\n Data Completeness = %s%%, Anthrome Class = %s, Raw Class = %s, Grid Index = %s,%s'
            % (ref, country, continent, group, lat, lon, alt, complete,
               a_class, r_class, lat_n, lon_n))

        plt.legend(loc='lower right')
        plt.tight_layout()
        axo.grid()
        axo2.grid()

        plt.show()
    else:
        #fig2 = plt.figure()
        fig2, (axo, axo2) = plt.subplots(2, figsize=(24, 12))
        fig2.patch.set_facecolor('white')

        axo.plot_date(obs_time_pd.to_pydatetime(),
                      obs_var_pd,
                      color='black',
                      markersize=3,
                      label='Observations')
        axo.plot_date(model_time_pd.to_pydatetime(),
                      model_var_pd,
                      color='red',
                      markersize=3,
                      alpha=0.5,
                      label='%s %s %s %s' %
                      (model_name, version, grid_size, met),
                      markeredgecolor='None')
        axo.plot_date(obs_time_pd.to_pydatetime(),
                      obs_seasonal_wave_pd,
                      color='yellow',
                      markersize=3,
                      label='Obs Seasonal Waveform',
                      markeredgecolor='None')
        axo.plot_date(model_time_pd.to_pydatetime(),
                      model_seasonal_wave_pd,
                      color='green',
                      markersize=3,
                      label='Model Seasonal Waveform',
                      markeredgecolor='None')

        axo2.loglog(obs_period, obs_amp, color='black', label='Obs')
        axo2.loglog(mod_period,
                    mod_amp,
                    color='red',
                    label='%s %s %s %s' %
                    (model_name, version, grid_size, met))

        axo2.text(0.01,
                  0.95,
                  'Obs D Amp = %8.2f ppb' % (obs_daily_mag[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.92,
                  'Model D Amp = %8.2f ppb' % (model_daily_mag[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        axo2.text(0.01,
                  0.85,
                  'Obs HA Amp = %8.2f ppb' % (obs_ha_mag[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.82,
                  'Model HA Amp = %8.2f ppb' % (model_ha_mag[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        axo2.text(0.01,
                  0.75,
                  'Obs A Amp = %8.2f ppb' % (obs_annual_mag[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.72,
                  'Model A Amp = %8.2f ppb' % (model_annual_mag[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        axo2.text(0.01,
                  0.55,
                  'Obs D Phase = %8.2f' % (obs_daily_phase[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.52,
                  'Model D Phase = %8.2f' % (model_daily_phase[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        axo2.text(0.01,
                  0.45,
                  'Obs HA Phase = %8.2f' % (obs_ha_phase[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.42,
                  'Model HA Phase = %8.2f' % (model_ha_phase[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        obs_a_ph = obs_annual_phase[ind]
        mod_a_ph = model_annual_phase[ind]

        if obs_a_ph > 12:
            obs_a_ph = obs_a_ph - 12.
        if mod_a_ph > 12:
            mod_a_ph = mod_a_ph - 12.

        axo2.text(0.01,
                  0.35,
                  'Obs A Phase = %8.2f' % (obs_a_ph),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.32,
                  'Model A Phase = %8.2f' % (mod_a_ph),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        axo2.text(0.01,
                  0.15,
                  'Obs Ave = %8.2f ppb' % (obs_ave[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold')
        axo2.text(0.01,
                  0.12,
                  'Model Ave = %8.2f ppb' % (model_ave[ind]),
                  transform=axo2.transAxes,
                  fontweight='bold',
                  color='red')

        axo2.axvline(1., ymin=0, ymax=1, color='blue', linestyle='--')
        axo2.axvline(182.625, ymin=0, ymax=1, color='blue', linestyle='--')
        axo2.axvline(365.25, ymin=0, ymax=1, color='blue', linestyle='--')

        axo2.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
        axo2.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
        plt.gca().xaxis.set_major_formatter(FuncFormatter(xformatter))
        plt.gca().yaxis.set_major_formatter(FuncFormatter(yformatter))

        axo.set_title(
            'Site = %s, Country = %s, Continent = %s, Process Group = %s, Lat = %s, Lon = %s, Alt = %sm,\n Data Completeness = %s%%, Anthrome Class = %s, Raw Class = %s, Grid Index = %s,%s'
            % (ref, country, continent, group, lat, lon, alt, complete,
               a_class, r_class, lat_n, lon_n))

        plt.legend(loc='lower right')
        plt.tight_layout()
        axo.grid()
        axo2.grid()

        plt.show()
Exemplo n.º 26
0
 def genMatplotlibFig(self, file_path=None):
     nRow, nCol = 8, 3
     Fig = Figure(figsize=(min(32, 16 + (nCol - 1) * 8), 8 * nRow))
     xData = np.arange(1, self._Output["统计数据"]["Top"].shape[0] + 1)
     xTickLabels = [str(iInd) for iInd in self._Output["统计数据"]["Top"].index]
     PercentageFormatter = FuncFormatter(_QS_formatMatplotlibPercentage)
     FloatFormatter = FuncFormatter(lambda x, pos: '%.2f' % (x, ))
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 1),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["Top"]["年化超额收益率"],
                        PercentageFormatter,
                        self._Output["统计数据"]["Top"]["胜率"],
                        PercentageFormatter,
                        True,
                        title="Top 组合年化超额收益率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 2),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["Top"]["信息比率"],
                        PercentageFormatter,
                        None,
                        title="Top 组合信息比率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 3),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["Top"]["超额最大回撤率"],
                        PercentageFormatter,
                        None,
                        title="Top 组合超额最大回撤率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 4),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["Top"]["年化收益率"],
                        PercentageFormatter,
                        None,
                        title="Top 组合年化收益率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 5),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["Top"]["Sharpe比率"],
                        FloatFormatter,
                        None,
                        title="Top 组合 Sharpe 比率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 6),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["Top"]["平均换手率"],
                        PercentageFormatter,
                        None,
                        title="Top 组合平均换手率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 7),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["Bottom"]["年化超额收益率"],
                        PercentageFormatter,
                        self._Output["统计数据"]["Bottom"]["胜率"],
                        PercentageFormatter,
                        True,
                        title="Bottom 组合年化超额收益率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 8),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["Bottom"]["信息比率"],
                        PercentageFormatter,
                        None,
                        title="Bottom 组合信息比率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 9),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["Bottom"]["超额最大回撤率"],
                        PercentageFormatter,
                        None,
                        title="Bottom 组合超额最大回撤率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 10),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["Bottom"]["年化收益率"],
                        PercentageFormatter,
                        None,
                        title="Bottom 组合年化收益率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 11),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["Bottom"]["Sharpe比率"],
                        FloatFormatter,
                        None,
                        title="Bottom 组合 Sharpe 比率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 12),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["Bottom"]["平均换手率"],
                        PercentageFormatter,
                        None,
                        title="Bottom 组合平均换手率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 13),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["L-S"]["年化收益率"],
                        PercentageFormatter,
                        None,
                        title="L-S 组合年化收益率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 14),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["L-S"]["Sharpe比率"],
                        FloatFormatter,
                        None,
                        title="L-S 组合 Sharpe 比率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 15),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["L-S"]["最大回撤率"],
                        PercentageFormatter,
                        None,
                        title="L-S 组合最大回撤Æ率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 16),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["市场"]["年化收益率"],
                        PercentageFormatter,
                        None,
                        title="市场组合年化收益率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 17),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["市场"]["Sharpe比率"],
                        FloatFormatter,
                        None,
                        title="市场组合 Sharpe 比率")
     _QS_plotStatistics(Fig.add_subplot(nRow, nCol, 18),
                        xData,
                        xTickLabels,
                        self._Output["统计数据"]["市场"]["最大回撤率"],
                        PercentageFormatter,
                        None,
                        title="市场组合最大回撤率")
     Axes = Fig.add_subplot(nRow, nCol, 19)
     Axes.xaxis_date()
     Axes.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))
     for i in range(self._Output["净值"]["Top"].shape[1]):
         Axes.plot(self._Output["净值"]["Top"].index,
                   self._Output["净值"]["Top"].iloc[:, i].values,
                   label=str(self._Output["净值"]["Top"].columns[i]),
                   lw=2.5)
     Axes.legend(loc='best')
     Axes.set_title("Top 组合净值")
     Axes = Fig.add_subplot(nRow, nCol, 20)
     Axes.xaxis_date()
     Axes.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))
     for i in range(self._Output["净值"]["Bottom"].shape[1]):
         Axes.plot(self._Output["净值"]["Bottom"].index,
                   self._Output["净值"]["Bottom"].iloc[:, i].values,
                   label=str(self._Output["净值"]["Bottom"].columns[i]),
                   lw=2.5)
     Axes.legend(loc='best')
     Axes.set_title("Bottom 组合净值")
     Axes = Fig.add_subplot(nRow, nCol, 21)
     Axes.xaxis_date()
     Axes.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))
     for i in range(self._Output["净值"]["市场"].shape[1]):
         Axes.plot(self._Output["净值"]["市场"].index,
                   self._Output["净值"]["市场"].iloc[:, i].values,
                   label=str(self._Output["净值"]["市场"].columns[i]),
                   lw=2.5)
     Axes.legend(loc='best')
     Axes.set_title("市场组合净值")
     Axes = Fig.add_subplot(nRow, nCol, 22)
     Axes.xaxis_date()
     Axes.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))
     for i in range(self._Output["超额净值"]["Top"].shape[1]):
         Axes.plot(self._Output["超额净值"]["Top"].index,
                   self._Output["超额净值"]["Top"].iloc[:, i].values,
                   label=str(self._Output["超额净值"]["Top"].columns[i]),
                   lw=2.5)
     Axes.legend(loc='best')
     Axes.set_title("Top 组合超额净值")
     Axes = Fig.add_subplot(nRow, nCol, 23)
     Axes.xaxis_date()
     Axes.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))
     for i in range(self._Output["超额净值"]["Bottom"].shape[1]):
         Axes.plot(self._Output["超额净值"]["Bottom"].index,
                   self._Output["超额净值"]["Bottom"].iloc[:, i].values,
                   label=str(self._Output["超额净值"]["Bottom"].columns[i]),
                   lw=2.5)
     Axes.legend(loc='best')
     Axes.set_title("Bottom 组合超额净值")
     Axes = Fig.add_subplot(nRow, nCol, 24)
     Axes.xaxis_date()
     Axes.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))
     for i in range(self._Output["净值"]["L-S"].shape[1]):
         Axes.plot(self._Output["净值"]["L-S"].index,
                   self._Output["净值"]["L-S"].iloc[:, i].values,
                   label=str(self._Output["净值"]["L-S"].columns[i]),
                   lw=2.5)
     Axes.legend(loc='best')
     Axes.set_title("L-S 组合净值")
     if file_path is not None:
         Fig.savefig(file_path, dpi=150, bbox_inches='tight')
     return Fig
Exemplo n.º 27
0
def plot_streamflow():
    plot_utils.apply_plot_params(width_pt=None, width_cm=19, height_cm=10, font_size=12)
    labels = ["Glacier-only", "All"]
    colors = ["r", "b"]
    paths = [
        "/skynet3_exec2/aganji/glacier_katja/watroute_gemera/discharge_stat_glac_00_99_2000_01_01_00_00.nc",
        "/skynet3_exec2/aganji/glacier_katja/watroute_gemera/discharge_stat_both_00_992000_01_01_00_00.nc"]

    infocell_path = "/skynet3_exec2/aganji/glacier_katja/watroute_gemera/infocell.nc"

    start_year = 2000
    end_year = 2099


    with Dataset(paths[0]) as ds:
        acc_area = ds.variables["accumulation_area"][:]
        lons = ds.variables["longitude"][:]
        lats = ds.variables["latitude"][:]
        x_index = ds.variables["x_index"][:]
        y_index = ds.variables["y_index"][:]

    with Dataset(infocell_path) as ds:
        fldr = ds.variables["flow_direction_value"][:]

    driver = ogr.GetDriverByName('ESRI Shapefile')
    data_source = driver.Open(path_to_basin_shape, 0)

    assert isinstance(data_source, ogr.DataSource)

    geom = None

    print(data_source.GetLayerCount())

    layer = data_source.GetLayer()
    assert isinstance(layer, ogr.Layer)

    print(layer.GetFeatureCount())
    for feature in layer:
        assert isinstance(feature, ogr.Feature)
        geom = feature.geometry()

        assert isinstance(geom, ogr.Geometry)
        # print(str(geom))

        # geom = ogr.CreateGeometryFromWkt(geom.ExportToWkt())

    i, j = get_outlet_indices(geom, acc_area, lons, lats)
    print("Accumulation area at the outlet (according to flow directions): {}".format(acc_area[i, j]))


    cell_manager = CellManager(flow_dirs=fldr, lons2d=lons, lats2d=lats, accumulation_area_km2=acc_area)

    model_mask = cell_manager.get_mask_of_upstream_cells_connected_with_by_indices(i, j)


    cell_index = np.where((x_index == i) & (y_index == j))[0][0]

    print(cell_index)

    if not img_folder.is_dir():
        img_folder.mkdir(parents=True)

    # Do the plotting
    fig = plt.figure()
    gs = gridspec.GridSpec(1, 2, wspace=0.0)

    # Plot the hydrograph
    ax = fig.add_subplot(gs[0, 0])

    for p, c, label in zip(paths, colors, labels):
        with Dataset(p) as ds:
            stfl = ds.variables["water_discharge_accumulated"][:, cell_index]

            time = ds.variables["time"][:].astype(str)
            time = [datetime.strptime("".join(ts), "%Y_%m_%d_%H_%M") for ts in time]
            df = pd.DataFrame(index=time, data=stfl)

            # remove 29th of February
            df = df.select(lambda d: not (d.month == 2 and d.day == 29) and (start_year <= d.year <= end_year))

            df = df.groupby(lambda d: datetime(2001, d.month, d.day)).mean()

            ax.plot(df.index, df.values, c, lw=2, label=label)

    ax.xaxis.set_major_formatter(FuncFormatter(lambda tickval, pos: num2date(tickval).strftime("%b")[0]))
    ax.xaxis.set_major_locator(MonthLocator())
    ax.legend(loc="upper left", bbox_to_anchor=(1.05, 1), borderaxespad=0)
    ax.set_title("{}-{}".format(start_year, end_year))

    # Plot the point position
    ax = fig.add_subplot(gs[0, 1])
    bsm = get_basemap_glaciers_nw_america()
    x, y = bsm(lons[i, j], lats[i, j])
    bsm.scatter(x, y, c="b", ax=ax, zorder=10)
    bsm.drawcoastlines()
    bsm.readshapefile(path_to_basin_shape.replace(".shp", ""), "basin", color="m", linewidth=2, zorder=5)

    # xx, yy = bsm(lons, lats)
    # cmap = cm.get_cmap("gray_r", 10)
    # bsm.pcolormesh(xx, yy, model_mask * 0.5, cmap=cmap, vmin=0, vmax=1)

    bsm.drawrivers(ax=ax, zorder=9, color="b")


    plt.savefig(str(img_folder.joinpath("stfl_at_outlets.pdf")), bbox_inches="tight")
    plt.close(fig)
mu = changes.mean()
vD = changes.std()

# Create an array of 10000 random walks - each with 90 steps
k = np.cumprod(1 + np.random.normal(mu, vD, (10000, 90)), 1) * end_price

# The last step as an array. In this case, useful to answer the question: "In 90 days, where could this go?"
l = k[:, -1]

# Plot
fig, ax = plt.subplots(2, 1, figsize=(11, 8.5))
ax[0].plot(k.T, linewidth=0.5)
ax[0].set_title("Forward Modeling; N = {:,}; {:,} Walks; {:,} Periods".format(
    len(s), *k.shape))
ax[0].get_yaxis().set_major_formatter(
    FuncFormatter(lambda a, b: '${:,.0f}'.format(a * 1e-0)))
ax[0].set_xlim(0, k.T.shape[0])

# Add a dotted line to trace the mean across the walks
ax[0].plot([_.mean() for _ in k.T],
           color='k',
           linewidth=1,
           linestyle=':',
           label='Expected Value')

# Annotate
ax[0].annotate('CAGR:  {}\nvD:    {}\nmu:    {}'.format(
    round(cagr, 8), round(vD, 8), round(mu, 8)),
               xy=(0, 45),
               xycoords='axes pixels',
               xytext=(4, 0),
Exemplo n.º 29
0
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
from networkx.drawing.nx_agraph import graphviz_layout

from opendht import *
from dht.network import DhtNetwork, DhtNetworkSubProcess

############
#  Common  #
############

# matplotlib display format for bits (b, Kb, Mb)
bit_format = None
Kbit_format = FuncFormatter(lambda x, pos: '%1.1f' % (x * 1024**-1) + 'Kb')
Mbit_format = FuncFormatter(lambda x, pos: '%1.1f' % (x * 1024**-2) + 'Mb')


def random_str_val(size=1024):
    """Creates a random string value of specified size.

    @param size:  Size, in bytes, of the value.
    @type  size:  int

    @return:  Random string value
    @rtype :  str
    """
    return ''.join(random.choice(string.hexdigits) for _ in range(size))

Exemplo n.º 30
0
def plot_rolling_returns(returns,
                         factor_returns=None,
                         live_start_date=None,
                         cone_std=None,
                         legend_loc='best',
                         volatility_match=False,
                         cone_function=timeseries.forecast_cone_bootstrap,
                         ax=None, **kwargs):
    """
    Plots cumulative rolling returns versus some benchmarks'.

    Backtest returns are in green, and out-of-sample (live trading)
    returns are in red.

    Additionally, a non-parametric cone plot may be added to the
    out-of-sample returns region.

    Parameters
    ----------
    returns : pd.Series
        Daily returns of the strategy, noncumulative.
         - See full explanation in tears.create_full_tear_sheet.
    factor_returns : pd.Series, optional
        Daily noncumulative returns of a risk factor.
         - This is in the same style as returns.
    live_start_date : datetime, optional
        The date when the strategy began live trading, after
        its backtest period. This date should be normalized.
    cone_std : float, or tuple, optional
        If float, The standard deviation to use for the cone plots.
        If tuple, Tuple of standard deviation values to use for the cone plots
         - See timeseries.forecast_cone_bounds for more details.
    legend_loc : matplotlib.loc, optional
        The location of the legend on the plot.
    volatility_match : bool, optional
        Whether to normalize the volatility of the returns to those of the
        benchmark returns. This helps compare strategies with different
        volatilities. Requires passing of benchmark_rets.
    cone_function : function, optional
        Function to use when generating forecast probability cone.
        The function signiture must follow the form:
        def cone(in_sample_returns (pd.Series),
                 days_to_project_forward (int),
                 cone_std= (float, or tuple),
                 starting_value= (int, or float))
        See timeseries.forecast_cone_bootstrap for an example.
    ax : matplotlib.Axes, optional
        Axes upon which to plot.
    **kwargs, optional
        Passed to plotting function.

    Returns
    -------
    ax : matplotlib.Axes
        The axes that were plotted on.

"""
    if ax is None:
        ax = plt.gca()

    ax.set_ylabel('Cumulative returns')
    ax.set_xlabel('')

    if volatility_match and factor_returns is None:
        raise ValueError('volatility_match requires passing of'
                         'factor_returns.')
    elif volatility_match and factor_returns is not None:
        bmark_vol = factor_returns.loc[returns.index].std()
        returns = (returns / returns.std()) * bmark_vol

    cum_rets = timeseries.cum_returns(returns, 1.0)

    y_axis_formatter = FuncFormatter(utils.one_dec_places)
    ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))

    if factor_returns is not None:
        cum_factor_returns = timeseries.cum_returns(
            factor_returns[cum_rets.index], 1.0)
        cum_factor_returns.plot(lw=2, color='gray',
                                label=factor_returns.name, alpha=0.60,
                                ax=ax, **kwargs)

    if live_start_date is not None:
        live_start_date = utils.get_utc_timestamp(live_start_date)
        is_cum_returns = cum_rets.loc[cum_rets.index < live_start_date]
        oos_cum_returns = cum_rets.loc[cum_rets.index >= live_start_date]
    else:
        is_cum_returns = cum_rets
        oos_cum_returns = pd.Series([])

    is_cum_returns.plot(lw=3, color='forestgreen', alpha=0.6,
                        label='Backtest', ax=ax, **kwargs)

    if len(oos_cum_returns) > 0:
        oos_cum_returns.plot(lw=4, color='red', alpha=0.6,
                             label='Live', ax=ax, **kwargs)

        if cone_std is not None:
            if isinstance(cone_std, (float, int)):
                cone_std = [cone_std]

            is_returns = returns.loc[returns.index < live_start_date]
            cone_bounds = cone_function(
                is_returns,
                len(oos_cum_returns),
                cone_std=cone_std,
                starting_value=is_cum_returns[-1])

            cone_bounds = cone_bounds.set_index(oos_cum_returns.index)

            for std in cone_std:
                ax.fill_between(cone_bounds.index,
                                cone_bounds[float(std)],
                                cone_bounds[float(-std)],
                                color='steelblue', alpha=0.5)

    if legend_loc is not None:
        ax.legend(loc=legend_loc)
    ax.axhline(1.0, linestyle='--', color='black', lw=2)

    return ax