Python matplotlib.pyplot.barh() Examples

The following are 30 code examples of matplotlib.pyplot.barh(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module matplotlib.pyplot , or try the search function .
Example #1
Source File: predict.py    From Transfer-Learning with MIT License 6 votes vote down vote up
def plot_preds(image, preds):
    """Displays image and the top-n predicted probabilities in a bar graph
    Args:
        image: PIL image
        preds: list of predicted labels and their probabilities
    """
    
    """# For Spyder
    plt.imshow(image)
    plt.axis('off')"""

    plt.figure()
    labels = ("cat", "dog")
    plt.barh([0, 1], preds, alpha=0.5)
    plt.yticks([0, 1], labels)
    plt.xlabel('Probability')
    plt.xlim(0,1.01)
    plt.tight_layout()
    plt.savefig('out.png') 
Example #2
Source File: iozone_xls_to_graph.py    From oxfs with MIT License 6 votes vote down vote up
def draw_one(self, xls, title, color):
        title = '{} ({})'.format(title, xls.columns[0])
        column_a = xls[xls.columns[0]]
        column_c = xls[xls.columns[2]]

        ticks = [column_a[x] for x in range(3, 16)]
        kbps = [self.float2(column_c[x]) for x in range(3, 16)]
        plt.barh(range(16 - 3), kbps, height=0.2, color=color, alpha=0.8)
        plt.yticks(range(16 - 3), ticks)
        plt.xlim(0, max(kbps) * 1.2)
        plt.xlabel("Speed")
        plt.title(title)
        for x, y in enumerate(kbps):
            plt.text(y + 1000, x - 0.1, '%s KB/s' % y)

        plt.show() 
Example #3
Source File: views.py    From API-Manager with GNU Affero General Public License v3.0 6 votes vote down vote up
def plot_bar_chart(self, data):
        x = []
        y = []
        for item in data:
            y.append(item['count'])
            x.append(item['Implemented_by_partial_function'])
        plt.barh(x, y)
        plt.title("Top apis", fontsize=10)
        plt.xlabel("Number of API Calls", fontsize=8)
        plt.xticks([])
        plt.ylabel("Partial function", fontsize=8)
        plt.tick_params(axis='y', labelsize=8)
        for i, j in zip(y, x):
            plt.text(i, j, str(i), clip_on=True, ha='center',va='center', fontsize=8)
        plt.tight_layout()
        buf = BytesIO()
        plt.savefig(buf, format='png')
        image_base64 = base64.b64encode(buf.getvalue()).decode('utf-8').replace('\n', '')
        buf.close()
        # Clear the previous plot.
        plt.gcf().clear()
        return image_base64 
Example #4
Source File: views.py    From API-Manager with GNU Affero General Public License v3.0 6 votes vote down vote up
def plot_topconsumer_bar_chart(self, data):
        x = []
        y = []
        for item in data:
            y.append(item['count'])
            x.append(item['app_name'])
        plt.barh(x, y)
        plt.title("Top consumers", fontsize=10)
        plt.xlabel("Number of API Calls", fontsize=8)
        plt.xticks([])
        plt.ylabel("Consumers", fontsize=8)
        plt.tick_params(axis='y', labelsize=8)
        for i, j in zip(y, x):
            plt.text(i, j, str(i), clip_on=True, ha='center',va='center', fontsize=8)
        plt.tight_layout()
        buf = BytesIO()
        plt.savefig(buf, format='png')
        image_base64 = base64.b64encode(buf.getvalue()).decode('utf-8').replace('\n', '')
        buf.close()
        # Clear the previous plot.
        plt.gcf().clear()
        return image_base64 
Example #5
Source File: model_utils.py    From aiexamples with Apache License 2.0 6 votes vote down vote up
def plot_parameter_statistic(model, layer_types=['Dense', 'Conv2D'], trainable=True, non_trainable=False, outputs=False):
    parameter_count = []
    names = []
    for l in model.layers:
        if l.__class__.__name__ not in layer_types:
            continue
        count = 0
        if outputs:
            count += np.sum([np.sum([np.prod(s[1:]) for s in n.output_shapes]) for n in l._inbound_nodes])
        if trainable:
            count += np.sum([K.count_params(p) for p in set(l.trainable_weights)])
        if non_trainable:
            count += np.sum([K.count_params(p) for p in set(l.non_trainable_weights)])
        parameter_count.append(count)
        names.append(l.name)
    
    y = range(len(names))
    plt.figure(figsize=[12,max(len(y)//4,1)])
    plt.barh(y, parameter_count, align='center')
    plt.yticks(y, names)
    plt.ylim(y[0]-1, y[-1]+1)
    ax = plt.gca()
    ax.invert_yaxis()
    ax.xaxis.tick_top()
    plt.show() 
Example #6
Source File: inference.py    From brain-segmentation-pytorch with MIT License 6 votes vote down vote up
def plot_dsc(dsc_dist):
    y_positions = np.arange(len(dsc_dist))
    dsc_dist = sorted(dsc_dist.items(), key=lambda x: x[1])
    values = [x[1] for x in dsc_dist]
    labels = [x[0] for x in dsc_dist]
    labels = ["_".join(l.split("_")[1:-1]) for l in labels]
    fig = plt.figure(figsize=(12, 8))
    canvas = FigureCanvasAgg(fig)
    plt.barh(y_positions, values, align="center", color="skyblue")
    plt.yticks(y_positions, labels)
    plt.xticks(np.arange(0.0, 1.0, 0.1))
    plt.xlim([0.0, 1.0])
    plt.gca().axvline(np.mean(values), color="tomato", linewidth=2)
    plt.gca().axvline(np.median(values), color="forestgreen", linewidth=2)
    plt.xlabel("Dice coefficient", fontsize="x-large")
    plt.gca().xaxis.grid(color="silver", alpha=0.5, linestyle="--", linewidth=1)
    plt.tight_layout()
    canvas.draw()
    plt.close()
    s, (width, height) = canvas.print_to_buffer()
    return np.fromstring(s, np.uint8).reshape((height, width, 4)) 
Example #7
Source File: gantt.py    From gantt with MIT License 6 votes vote down vote up
def render(self):
        """ Prepare data for plotting
        """

        # init figure
        self.fig, self.ax = plt.subplots()
        self.ax.yaxis.grid(False)
        self.ax.xaxis.grid(True)

        # assemble colors
        colors = []
        for pkg in self.packages:
            colors.append(pkg.color)

        self.barlist = plt.barh(self.yPos, list(self.durations),
                                left=self.start,
                                align='center',
                                height=.5,
                                alpha=1,
                                color=colors)

        # format plot
        self.format()
        self.add_milestones()
        self.add_legend() 
Example #8
Source File: VisualizeBestFeatures.py    From ProFET with GNU General Public License v3.0 6 votes vote down vote up
def altPlotFeaturesImportance(X,y,featureNames,dataName):
    "http://nbviewer.ipython.org/github/cs109/2014/blob/master/homework-solutions/HW5-solutions.ipynb"
    clf = RandomForestClassifier(n_estimators=50)

    clf.fit(X,y)
    importance_list = clf.feature_importances_
    # name_list = df.columns #ORIG
    name_list=featureNames

    importance_list, name_list = zip(*sorted(zip(importance_list, name_list)))
    plt.barh(range(len(name_list)),importance_list,align='center')
    plt.yticks(range(len(name_list)),name_list)
    plt.xlabel('Relative Importance in the Random Forest')
    plt.ylabel('Features')
    plt.title('%s \n Relative Feature Importance' %(dataName))
    plt.grid('off')
    plt.ion()
    plt.show() 
Example #9
Source File: Show.py    From DOVE with GNU General Public License v3.0 5 votes vote down vote up
def draw_bar(labels,quants,indicate_path,xlabel,ylabel,title):
    width=0.35
    plt.figure(figsize=(8,(width+0.1)*len(quants)), dpi=300)
    # Bar Plot
    plt.cla()
    plt.clf()
    plt.barh(range(len(quants)),quants,tick_label=labels)
    plt.grid(True)
    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.savefig(indicate_path)
    plt.close() 
Example #10
Source File: main.py    From WxConn with MIT License 5 votes vote down vote up
def generate_city_pic(self, city_data):
        """
        生成城市数据图片
        因为plt在子线程中执行会出现自动弹出弹框并阻塞主线程的行为,plt行为均放在主线程中
        :param data:
        :return:
        """
        font = {'family': ['xkcd', 'Humor Sans', 'Comic Sans MS'],
                'weight': 'bold',
                'size': 12}
        matplotlib.rc('font', **font)
        cities = city_data['cities']
        city_people = city_data['city_people']

        # 绘制「性别分布」柱状图
        plt.barh(range(len(cities)), width=city_people, align='center', color=self.bar_color, alpha=0.8)
        # 添加轴标签
        plt.xlabel(u'Number of People')
        # 添加标题
        plt.title(u'Top %d Cities of your friends distributed' % len(cities), fontsize=self.title_font_size)
        # 添加刻度标签
        plt.yticks(range(len(cities)), cities)
        # 设置X轴的刻度范围
        plt.xlim([0, city_people[0] * 1.1])

        # 为每个条形图添加数值标签
        for x, y in enumerate(city_people):
            plt.text(y + len(str(y)), x, y, ha='center')

        # 显示图形
        plt.savefig(ALS.result_path + '/4.png')
        # todo 如果调用此处的关闭,就会导致应用本身也被关闭
        # plt.close()
        # plt.show() 
Example #11
Source File: iozone_xls_to_graph.py    From oxfs with MIT License 5 votes vote down vote up
def draw_compare(self):
        xls = self.oxfs_xls
        column_a = xls[xls.columns[0]]
        column_c = xls[xls.columns[2]]

        oxfs_ticks = [column_a[x] + '- oxfs' for x in range(3, 16)]
        oxfs_kbps = [self.float2(column_c[x]) for x in range(3, 16)]

        xls = self.sshfs_xls
        column_a = xls[xls.columns[0]]
        column_c = xls[xls.columns[2]]

        sshfs_ticks = [column_a[x] + '- sshfs' for x in range(3, 16)]
        sshfs_kbps = [self.float2(column_c[x]) for x in range(3, 16)]

        ticks = []
        kbps = []
        for i in range(0, len(oxfs_kbps)):
            ticks.append(oxfs_ticks[i])
            ticks.append(sshfs_ticks[i])
            kbps.append(oxfs_kbps[i])
            kbps.append(sshfs_kbps[i])

        barlist = plt.barh(range(len(kbps)), kbps, height=0.3, color='coral', alpha=0.8)
        for bar in barlist[1::2]:
            bar.set_color('slateblue')
        plt.yticks(range(len(ticks)), ticks)
        plt.xlim(0, max(kbps) * 1.2)
        for x, y in enumerate(kbps):
            plt.text(y + 1000, x - 0.1, '%s KB/s' % y)

        title = 'Oxfs Vs Sshfs ({})'.format(xls.columns[0])
        plt.title(title)
        plt.xlabel("Speed")

        plt.show() 
Example #12
Source File: evaluate_retrieval.py    From semantic-embeddings with MIT License 5 votes vote down vote up
def plot_performance(perf, kmax = 100, prec_type = 'LCS_HEIGHT', clip_ahp = None):
    
    import matplotlib.pyplot as plt
    
    plt.figure()
    plt.xlabel('k')
    plt.ylabel('Hierarchical Precision')
    plt.xlim(0, kmax)
    plt.ylim(0, 1)
    plt.grid()
    
    min_prec = 1.0
    for lbl, metrics in perf.items():
        precs = [metrics['P@{} ({})'.format(k, prec_type)] for k in range(1, kmax+1)]
        plt.plot(np.arange(1, kmax + 1), precs, label = lbl)
        min_prec = min(min_prec, min(precs))
    
    min_prec = np.floor(min_prec * 20) / 20
    if min_prec >= 0.3:
        plt.ylim(min_prec, 1)
    
    plt.legend(fontsize = 'x-small')
    
    
    plt.figure()
    plt.xlabel('Mean Average Hierarchical Precision')
    plt.yticks([])
    plt.grid(axis = 'x')
    
    for i, (lbl, metrics) in enumerate(perf.items()):
        mAHP = metrics['AHP{} ({})'.format('@{}'.format(clip_ahp) if clip_ahp else '', prec_type)]
        plt.barh(i + 0.5, mAHP, 0.8)
        plt.text(0.01, i + 0.5, lbl, verticalalignment = 'center', horizontalalignment = 'left', color = 'white', fontsize = 'small')
        plt.text(mAHP - 0.01, i + 0.5, '{:.1%}'.format(mAHP), verticalalignment = 'center', horizontalalignment = 'right', color = 'white')
    
    
    plt.show() 
Example #13
Source File: graphicsData.py    From tieba-zhuaqu with GNU General Public License v3.0 5 votes vote down vote up
def barHonGraphics(xLabel,yLabel,xValueList,yValueList,graphicTitle='图例',xWidth=0.5):
    plt.barh(numpy.arange(len(xValueList)), yValueList, alpha=0.4)
    plt.yticks(numpy.arange(len(xValueList)), xValueList,fontproperties=font_set)
    plt.xlabel(yLabel,fontproperties=font_set)
    plt.ylabel(xLabel,fontproperties=font_set)
    plt.title(graphicTitle,fontproperties=font_set)

    plt.show()
	
#折线图:蓝色粗线 
Example #14
Source File: graphicsData.py    From tieba-zhuaqu with GNU General Public License v3.0 5 votes vote down vote up
def barHonGraphics(xLabel,yLabel,xValueList,yValueList,graphicTitle='图例',xWidth=0.5):
    plt.barh(numpy.arange(len(xValueList)), yValueList, alpha=0.4)
    plt.yticks(numpy.arange(len(xValueList)), xValueList,fontproperties=font_set)
    plt.xlabel(yLabel,fontproperties=font_set)
    plt.ylabel(xLabel,fontproperties=font_set)
    plt.title(graphicTitle,fontproperties=font_set)

    plt.show()
	
#折线图:蓝色粗线 
Example #15
Source File: graphicsData.py    From tieba-zhuaqu with GNU General Public License v3.0 5 votes vote down vote up
def barHonGraphics(xLabel,yLabel,xValueList,yValueList,graphicTitle='图例',xWidth=0.5):
    plt.barh(numpy.arange(len(xValueList)), yValueList, alpha=0.4)
    plt.yticks(numpy.arange(len(xValueList)), xValueList,fontproperties=font_set)
    plt.xlabel(yLabel,fontproperties=font_set)
    plt.ylabel(xLabel,fontproperties=font_set)
    plt.title(graphicTitle,fontproperties=font_set)

    plt.show()
	
#折线图:蓝色粗线 
Example #16
Source File: graphicsData.py    From tieba-zhuaqu with GNU General Public License v3.0 5 votes vote down vote up
def barHonGraphics(xLabel,yLabel,xValueList,yValueList,graphicTitle='图例',xWidth=0.5):
    plt.barh(numpy.arange(len(xValueList)), yValueList, alpha=0.4)
    plt.yticks(numpy.arange(len(xValueList)), xValueList,fontproperties=font_set)
    plt.xlabel(yLabel,fontproperties=font_set)
    plt.ylabel(xLabel,fontproperties=font_set)
    plt.title(graphicTitle,fontproperties=font_set)

    plt.show()
	
#折线图:蓝色粗线 
Example #17
Source File: features.py    From bartpy with MIT License 5 votes vote down vote up
def plot_feature_split_proportions(model: SklearnModel, ax=None):
    if ax is None:
        _, ax = plt.subplots(1, 1)
    proportions = feature_split_proportions(model)

    y_pos = np.arange(len(proportions))
    name, count = list(proportions.keys()), list(proportions.values())
    props = pd.DataFrame({"name": name, "counts": count}).sort_values("name", ascending=True)
    plt.barh(y_pos, props.counts, align='center', alpha=0.5)
    plt.yticks(y_pos, props.name)
    plt.xlabel('Proportion of all splits')
    plt.ylabel('Feature')
    plt.title('Proportion of Splits Made on Each Variable')
    return ax 
Example #18
Source File: JupyterNotebook.py    From big-data-analytics with MIT License 5 votes vote down vote up
def plot(counts):
    labels = map(lambda x: x[0], counts)
    values = map(lambda y: y[1], counts)
    plt.barh(range(len(values)), values, color='green')
    plt.yticks(range(len(values)), labels)
    plt.show() 
Example #19
Source File: plot_utils.py    From pygef with MIT License 5 votes vote down vote up
def add_grouped_classification(
    fig, df_group, depth_max, depth_min, title_group, num_col, z_NAP
):
    """
    Add to the plot the selected classification.

    :param fig: Original figure.
    :param depth_max: Maximum depth.
    :param depth_min: Minimum depth.
    :return: Complete figure.
    """
    ax = fig.add_subplot(1, num_col, 4)
    df = df_group
    for i, layer in enumerate(df["soil_type"]):
        if z_NAP:
            plt.barh(
                y=df["z_centr_NAP"][i],
                height=df["thickness"][i],
                width=5,
                color=df["colour"][i],
                label=layer,
            )
        else:
            plt.barh(
                y=df["z_centr"][i],
                height=df["thickness"][i],
                width=5,
                color=df["colour"][i],
                label=layer,
            )
    ax.set_xticks([])
    ax.set_title(f"{title_group} classification")
    plt.ylim(depth_max, depth_min)
    return fig 
Example #20
Source File: analysis.py    From pwptemp with GNU Lesser General Public License v3.0 5 votes vote down vote up
def plot(effect, how):
    import matplotlib.pyplot as plt
    if how == 1:
        values = [effect.t2, abs(effect.cc), effect.hs, effect.t1]
        bars = ['Tf: %1.2f°C' % effect.t2, 'Convection and Conduction: %1.2f°C' % effect.cc, 'Heat Source Term: %1.2f°C'
                % effect.hs, 'To: %1.2f°C' % effect.t1]
        position = range(4)
        plt.barh(position, values, color=['blue', 'red', 'green', 'blue'])
        if effect.t1 > effect.t2:
            plt.barh(0, abs(effect.cc + effect.hs), color='red', left=effect.t2)
            plt.text(effect.t2, 0, '%1.2f°C' % (effect.cc + effect.hs))
        else:
            plt.barh(0, abs(effect.cc + effect.hs), color='green', left=effect.t1)
            plt.text(effect.t1, 0, '%1.2f°C' % (effect.cc + effect.hs))
        plt.yticks(position, ['Tf', 'CC', 'HS', 'To'])
        plt.title('Temperature contribution of main factors at %1.1fh - %1.1fm' % (effect.time, effect.length),
                  fontweight='bold')
        for i, v in enumerate(values):
            plt.text(v/8, i, bars[i])
        plt.xlabel('Temperature, °C')
        plt.show()

    if how == 2:
        labels = ['pipe rotation in Qp', 'friction in Qp', 'pipe rotation in Qa', 'friction in Qa']
        effects = [effect.ds_rot1, effect.fric1, effect.ds_rot2, effect.fric2]
        plt.pie(effects, startangle=90)
        plt.legend(loc=0, labels=['%s, %1.2f %%' % (l, s) for l, s in zip(labels, effects)])
        title = 'Effect of factors in heat source terms. Qp/Qa = %1.2f' % effect.hsr
        plt.title(title)
        plt.show() 
Example #21
Source File: analysis.py    From twitter-intelligence with MIT License 5 votes vote down vote up
def analysis_hashtag():
    with sqlite3.connect(db_path) as db:
        conn = db
        c = conn.cursor()
        c.execute("SELECT hashtag from Tweet")
        hashtag_list = []
        for row in c.fetchall():
            if (row != ('',)):
                if " " in ''.join(row):
                    for m in ''.join(row).split(' '):
                        hashtag_list.append(m)
                else:
                    signle_item = ''.join(row)
                    hashtag_list.append(signle_item)

        counter = Counter(hashtag_list).most_common(10)
        pl.rcdefaults()

        keys = []
        performance = []

        for i in counter:
            performance.append(i[1])
            keys.append(i[0])

        pl.rcdefaults()
        y_pos = np.arange(len(keys))
        error = np.random.rand(len(keys))

        pl.barh(y_pos, performance, xerr=error, align='center', alpha=0.4, )
        pl.yticks(y_pos, keys)
        pl.xlabel('quantity')
        pl.title('hashtags')
        print(colored("[INFO] Showing graph of hashtag analysis", "green"))
        pl.show() 
Example #22
Source File: oraclesplot.py    From actions-for-actions with GNU General Public License v3.0 5 votes vote down vote up
def main():
    newdata = make_data()
    setup_plot()
    allticks = []
    for i,(oracle,data) in enumerate(zip(ORACLES,newdata)):
        pos = i*(max(1,Nsub)+1)+np.array(range(max(1,Nsub)+1))
        handles = []
        for j,dat in enumerate(data):
            h = plt.barh(pos[1]+j,dat,color=COLORS[j],edgecolor=COLORS[j])
            handles.append(h)
        allticks.extend(zip([x for x in pos], ['', oracle['name'],'']))
    finalize_plot(allticks,handles)
    plt.savefig(plotname+'.pdf')
    plt.show() 
Example #23
Source File: PipeTasks.py    From ProFET with GNU General Public License v3.0 5 votes vote down vote up
def Feature_Importance_plot (est,names):
    'http://nbviewer.ipython.org/github/pprett/pydata-gbrt-tutorial/blob/master/gbrt-tutorial.ipynb'
    fx_imp = pd.Series(est.feature_importances_, index=names)
    fx_imp /= fx_imp.max()  # normalize
    fx_imp.sort()
    fx_imp.plot(kind='barh', figsize=FIGSIZE) 
Example #24
Source File: PipeTasks.py    From ProFET with GNU General Public License v3.0 5 votes vote down vote up
def PlotFeaturesImportance(X,y,featureNames):
    '''

    Plot the relative contribution/importance of the features.
    Best to reduce to top X features first - for interpretability
    Code example from:
    http://bugra.github.io/work/notes/2014-11-22/an-introduction-to-supervised-learning-scikit-learn/
    '''

    gbc = GradientBoostingClassifier(n_estimators=100)
    gbc.fit(X, y)
    # Get Feature Importance from the classifier
    feature_importance = gbc.feature_importances_
    # Normalize The Features
    feature_importance = 100.0 * (feature_importance / feature_importance.max())
    sorted_idx = np.argsort(feature_importance)
    pos = np.arange(sorted_idx.shape[0]) + .5
    plt.figure(figsize=(16, 12))
    plt.barh(pos, feature_importance[sorted_idx], align='center', color='#7A68A6')

    #plt.yticks(pos, np.asanyarray(df.columns.tolist())[sorted_idx]) #ORIG
    plt.yticks(pos, np.asanyarray(featureNames)[sorted_idx])

    plt.xlabel('Relative Importance')
    plt.title('Features Importance')
    plt.show() 
Example #25
Source File: VisualizeBestFeatures.py    From ProFET with GNU General Public License v3.0 5 votes vote down vote up
def PlotFeaturesImportance(X,y,featureNames,dataName):
    '''
    Plot the relative contribution/importance of the features.
    Best to reduce to top X features first - for interpretability
    Code example from:
    http://bugra.github.io/work/notes/2014-11-22/an-introduction-to-supervised-learning-scikit-learn/
    '''
    gbc = GradientBoostingClassifier(n_estimators=40)
    gbc.fit(X, y)
    # Get Feature Importance from the classifier
    feature_importance = gbc.feature_importances_
    # Normalize The Features
    feature_importance = 100 * (feature_importance / feature_importance.max())
    sorted_idx = numpy.argsort(feature_importance)
    pos = numpy.arange(sorted_idx.shape[0]) + 4.5
    # pos = numpy.arange(sorted_idx.shape[0])
    # plt.figure(figsize=(16, 12))
    plt.figure(figsize=(14, 9), dpi=250)
    plt.barh(pos, feature_importance[sorted_idx], align='center', color='#7A68A6')
    #plt.yticks(pos, numpy.asanyarray(df.columns.tolist())[sorted_idx]) #ORIG
    plt.yticks(pos, numpy.asanyarray(featureNames)[sorted_idx])

    plt.xlabel('Relative Importance')
    plt.title('%s: Top Features' %(dataName))
    plt.grid('off')
    plt.ion()
    plt.show()
    plt.savefig(str(dataName)+'TopFeatures.png',dpi=200) 
Example #26
Source File: dell_tests.py    From mmvt with GNU General Public License v3.0 5 votes vote down vote up
def calc_groups_dist_to_dura(elc_name, output_fol, threshold):
    (electrodes, names, hemis, threshold) = utils.load(op.join(output_fol, '{}_electrodes.pkl'.format(int(threshold))))
    groups, noise = utils.load(op.join(output_fol, '{}_groups.pkl'.format(int(threshold))))
    elc_ind = names.index(elc_name)
    group, in_group_ind = find_electrode_group(elc_ind, groups)

    verts = {}
    for hemi in ['lh', 'rh']:
        # verts[hemi], _ = nib.freesurfer.read_geometry(op.join(subject_fol, 'surf', '{}.dural'.format(hemi)))
        verts[hemi], _ = nib.freesurfer.read_geometry(op.join(subject_fol, 'bem', 'watershed',  'mg105_brain_surface'))

    dists = cdist([electrodes[elc_ind]], verts[hemis[elc_ind]])
    close_verts_dists = np.min(dists, axis=1)
    close_verts_ind = np.argmin(dists, axis=1)
    print('{}: {} ({})'.format(names[elc_ind], close_verts_dists, verts[hemis[elc_ind]][close_verts_ind]))

    mean_dists = []
    for group in groups:
        dists = cdist(electrodes[group], verts[hemis[group[0]]])
        close_verts_dists = np.min(dists, axis=1)
        print('{}-{}: {}'.format(names[group[0]], names[group[-1]], close_verts_dists))
        mean_dists.append(np.max(close_verts_dists))
    plt.barh(np.arange(len(groups)), mean_dists, align='center', alpha=0.5)
    plt.yticks(np.arange(len(groups)), ('{}-{}'.format(names[group[0]], names[group[-1]]) for group in groups))
    plt.title('max groups dist to dural surface')
    plt.show()
    print('asdf') 
Example #27
Source File: plot.py    From VerticaPy with Apache License 2.0 5 votes vote down vote up
def plot_importance(coeff_importances: dict, 
					coeff_sign: dict = {},
					print_legend: bool = True):
	check_types([
		("coeff_importances", coeff_importances, [dict], False),
		("coeff_sign", coeff_sign, [dict], False),
		("print_legend", print_legend, [bool], False)])
	coefficients, importances, signs = [], [], []
	for coeff in coeff_importances:
		coefficients += [coeff]
		importances += [coeff_importances[coeff]]
		signs += [coeff_sign[coeff]] if (coeff in coeff_sign) else [1]
	importances, coefficients, signs = zip( * sorted(zip(importances, coefficients, signs)))
	plt.figure(figsize = (12, int(len(importances) / 2) + 1))
	plt.rcParams['axes.facecolor'] = '#F5F5F5'
	color = []
	for item in signs:
		color += ['#263133'] if (item == 1) else ['#FE5016']
	plt.barh(range(0, len(importances)), importances, 0.9, color = color, alpha = 0.86)
	if (print_legend):
		orange = mpatches.Patch(color = '#FE5016', label = 'sign -')
		blue = mpatches.Patch(color = '#263133', label = 'sign +')
		plt.legend(handles = [orange,blue], loc = "lower right")
	plt.ylabel("Features")
	plt.xlabel("Importance")
	plt.gca().xaxis.grid()
	plt.gca().set_axisbelow(True)
	plt.yticks(range(0, len(importances)), coefficients)
	plt.show()
#---# 
Example #28
Source File: plot.py    From VerticaPy with Apache License 2.0 5 votes vote down vote up
def bar(vdf,
		method: str = "density",
		of = None,
		max_cardinality: int = 6,
		bins: int = 0,
		h: float = 0,
		color: str = '#263133'):
	x, y, z, h, is_categorical = compute_plot_variables(vdf, method = method, of = of, max_cardinality = max_cardinality, bins = bins, h = h)
	plt.figure(figsize = (14, min(len(x), 600))) if isnotebook() else plt.figure(figsize = (10, 6))
	plt.rcParams['axes.facecolor'] = '#F5F5F5'
	plt.barh(x, y, h, color = color, alpha = 0.86)
	plt.ylabel(vdf.alias)
	plt.gca().xaxis.grid()
	plt.gca().set_axisbelow(True)
	if (is_categorical):
		if (vdf.category() == "text"):
			new_z = []
			for item in z:
				new_z += [item[0:47] + "..."] if (len(str(item)) > 50) else [item]
		else:
			new_z = z
		plt.yticks(x,new_z)
		plt.subplots_adjust(left = max(0.1, min(len(max([str(item) for item in z], key = len)), 20) / 80.0))
	if (method == "density"):
		plt.xlabel('Density')
		plt.title('Distribution of {}'.format(vdf.alias))
	elif ((method in ["avg", "min", "max", "sum"] or '%' in method) and (of != None)):
		aggregate = "{}({})".format(method.upper(), of)
		plt.ylabel(aggregate)
		plt.title('{} group by {}'.format(aggregate, vdf.alias))
	else:
		plt.xlabel('Frequency')
		plt.title('Count by {}'.format(vdf.alias))
	plt.show()
#---# 
Example #29
Source File: importance.py    From mljar-supervised with MIT License 4 votes vote down vote up
def compute_and_plot(
        model,
        X_validation,
        y_validation,
        model_file_path,
        learner_name,
        metric_name=None,
        ml_task=None,
    ):

        # for scoring check https://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter
        if ml_task == BINARY_CLASSIFICATION:
            scoring = "neg_log_loss"
        elif ml_task == MULTICLASS_CLASSIFICATION:
            scoring = "neg_log_loss"
        else:
            scoring = "neg_mean_squared_error"

        importance = permutation_importance(
            model,
            X_validation,
            y_validation,
            scoring=scoring,
            n_jobs=-1,
            random_state=12,
        )

        sorted_idx = importance["importances_mean"].argsort()

        # save detailed importance
        df_imp = pd.DataFrame(
            {
                "feature": X_validation.columns[sorted_idx],
                "mean_importance": importance["importances_mean"][sorted_idx],
            }
        )
        df_imp.to_csv(
            os.path.join(model_file_path, f"{learner_name}_importance.csv"), index=False
        )

        """
        # Do not plot. We will plot aggregate of all importances.
        # limit number of column in the plot
        if len(sorted_idx) > 50:
            sorted_idx = sorted_idx[-50:]
        plt.figure(figsize=(10, 7))
        plt.barh(
            X_validation.columns[sorted_idx], importance["importances_mean"][sorted_idx]
        )
        plt.xlabel("Mean of feature importance")
        plt.tight_layout(pad=2.0)
        plot_path = os.path.join(model_file_path, f"{learner_name}_importance.png")
        plt.savefig(plot_path)
        plt.close("all")
        """ 
Example #30
Source File: compare_models.py    From AmusingPythonCodes with MIT License 4 votes vote down vote up
def get_head_ngram_statistics(questions, correct_model1, correct_model2, correct_model1_and_model2, correct_model1_and_not_model2, correct_model2_and_not_model1, output_dir, num_grams=2, top_count=25):
    # Head ngram statistics
    head_ngrams = get_head_ngrams(questions, num_grams)

    # Get head_ngram_frequencies (hnf)
    hnf_all = get_head_ngram_frequencies(questions, head_ngrams, num_grams)
    hnf_correct_model1 = get_head_ngram_frequencies({qid: questions[qid] for qid in correct_model1}, head_ngrams, num_grams)
    hnf_correct_model2 = get_head_ngram_frequencies({qid: questions[qid] for qid in correct_model2}, head_ngrams, num_grams)
    hnf_correct_model1_and_model2 = get_head_ngram_frequencies({qid: questions[qid] for qid in correct_model1_and_model2}, head_ngrams, num_grams)
    hnf_correct_model1_and_not_model2 = get_head_ngram_frequencies({qid: questions[qid] for qid in correct_model1_and_not_model2}, head_ngrams, num_grams)
    hnf_correct_model2_and_not_model1 = get_head_ngram_frequencies({qid: questions[qid] for qid in correct_model2_and_not_model1}, head_ngrams, num_grams)

    sorted_bigrams_all = sorted(hnf_all.items(), key=lambda x: x[1], reverse=True)
    top_bigrams = [x[0] for x in sorted_bigrams_all[0:top_count]]

    counts_total = [hnf_all[x] for x in top_bigrams]
    counts_model1 = [hnf_correct_model1[x] for x in top_bigrams]
    counts_model2 = [hnf_correct_model2[x] for x in top_bigrams]
    counts_model1_and_model2 = [hnf_correct_model1_and_model2[x] for x in top_bigrams]
    counts_model1_and_not_model2 = [hnf_correct_model1_and_not_model2[x] for x in top_bigrams]
    counts_model2_and_not_model1 = [hnf_correct_model2_and_not_model1[x] for x in top_bigrams]

    top_bigrams_with_counts = []
    for cc in range(len(top_bigrams)):
        top_bigrams_with_counts.append('{0} ({1})'.format(top_bigrams[cc], counts_total[cc]))

    plt.clf()
    fig, ax = plt.subplots(figsize=(6, 10))

    ylocs = list(range(top_count))
    counts_model1_percent = 100 * np.array(counts_model1) / np.array(counts_total)
    plt.barh([top_count - x for x in ylocs], counts_model1_percent, height=0.4, alpha=0.5, color='#EE3224', label=top_bigrams)
    counts_model2_percent = 100 * np.array(counts_model2) / np.array(counts_total)
    plt.barh([top_count - x+0.4 for x in ylocs], counts_model2_percent, height=0.4, alpha=0.5, color='#2432EE', label=top_bigrams  )
    ax.set_yticks([top_count - x + 0.4 for x in ylocs])
    ax.set_yticklabels(top_bigrams_with_counts)
    ax.set_ylim([0.5, top_count+1])
    ax.set_xlim([0, 100])
    plt.subplots_adjust(left=0.28, right=0.9, top=0.9, bottom=0.1)
    plt.xlabel('Percentage of questions with correct answers')
    plt.ylabel('Top N-grams')
    plt.savefig(os.path.join(output_dir, 'ngram_stats_{0}.png'.format(num_grams)))
    plt.close()