我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用matplotlib.pyplot.bar()。
def get_feature_importance(list_of_features): n_estimators=10000 random_state=0 n_jobs=4 x_train=data_frame[list_of_features] y_train=data_frame.iloc[:,-1] feat_labels= data_frame.columns[1:] forest = BaggingRegressor(n_estimators=n_estimators,random_state=random_state,n_jobs=n_jobs) forest.fit(x_train,y_train) importances=forest.feature_importances_ indices = np.argsort(importances)[::-1] for f in range(x_train.shape[1]): print("%2d) %-*s %f" % (f+1,30,feat_labels[indices[f]], importances[indices[f]])) plt.title("Feature Importance") plt.bar(range(x_train.shape[1]),importances[indices],color='lightblue',align='center') plt.xticks(range(x_train.shape[1]),feat_labels[indices],rotation=90) plt.xlim([-1,x_train.shape[1]]) plt.tight_layout() plt.show()
def plot_bar_chart(label_to_value, title, x_label, y_label): """ Plots a bar chart from a dict. Args: label_to_value: A dict mapping ints or strings to numerical values (int or float). title: A string representing the title of the graph. x_label: A string representing the label for the x-axis. y_label: A string representing the label for the y-axis. """ n = len(label_to_value) labels = sorted(label_to_value.keys()) values = [label_to_value[label] for label in labels] plt.title(title) plt.xlabel(x_label) plt.ylabel(y_label) plt.bar(range(n), values, align='center') plt.xticks(range(n), labels, rotation='vertical', fontsize='7') plt.gcf().subplots_adjust(bottom=0.2) # make room for x-axis labels plt.show()
def Energy_Flow(Time_Series): Energy_Flow = {'Energy_Demand':0, 'Lost Load':0, 'Energy PV':0,'Curtailment':0, 'Energy Diesel':0, 'Discharge energy from the Battery':0, 'Charge energy to the Battery':0} for v in Energy_Flow.keys(): if v == 'Energy PV': Energy_Flow[v] = round((Time_Series[v].sum() - Time_Series['Curtailment'].sum()- Time_Series['Charge energy to the Battery'].sum())/1000000, 2) else: Energy_Flow[v] = round((Time_Series[v].sum())/1000000, 2) c = ['From Generator', 'To Battery', 'Demand', 'From PV', 'From Battery', 'Curtailment', 'Lost Load'] plt.figure() plt.bar((1,2,3,4,5,6,7), Energy_Flow.values(), color= 'b', alpha=0.3, align='center') plt.xticks((1.2,2.2,3.2,4.2,5.2,6.2,7.2), c) plt.xlabel('Technology') plt.ylabel('Energy Flow (MWh)') plt.tick_params(axis='x', which='both', bottom='off', top='off', labelbottom='on') plt.xticks(rotation=-30) plt.savefig('Results/Energy_Flow.png', bbox_inches='tight') plt.show() return Energy_Flow
def show(self): keys = [] values = [] for (k, v) in self.letter_db.iteritems(): total = v['total'] right = v['right'] keys.append(k) values.append(100 * float(right / float(total))) groups = len(self.letter_db) index = np.arange(groups) width = 0.5 opacity = 0.4 plt.bar(index, values, linewidth = width, alpha = opacity, color = 'b', label = 'right rate') plt.xlabel('letter') plt.ylabel('predict rgith rate (%)') plt.title('Writer identify: letter right rate') plt.xticks(index + width, keys) plt.ylim(0, 100) plt.legend() plt.show()
def plot_histogram(counter, label, plot=None): import matplotlib.pyplot as plt plt.figure() nums = list(counter.keys()) counts = list(counter.values()) indices = range(len(counts)) bars = plt.bar(indices, counts, align="center") plt.xticks(indices, nums) top = 1.06 * max(counts) plt.ylim(min(counts), top) plt.xlabel("number of %s" % label) plt.ylabel("count") for bar in bars: count = bar.get_height() plt.text(bar.get_x() + bar.get_width() / 2., count, "%.1f%%" % (100.0 * count / sum(counts)), ha="center", va="bottom") if plot: plt.savefig(plot + "histogram_" + label + ".png") else: plt.show()
def disp_gap_bydate(self): gaps_mean = self.gapdf.groupby('time_date')['gap'].mean() gaps_mean.plot(kind='bar') plt.ylabel('Mean of gap') plt.title('Date/Gap Correlation') # for i in gaps_mean.index: # plt.plot([i,i], [0, gaps_mean[i]], 'k-') plt.show() return # def drawGapDistribution(self): # self.gapdf[self.gapdf['gapdf'] < 10]['gapdf'].hist(bins=50) # # sns.distplot(self.gapdf['gapdf']); # # sns.distplot(self.gapdf['gapdf'], hist=True, kde=False, rug=False) # # plt.hist(self.gapdf['gapdf']) # plt.show() # return # def drawGapCorrelation(self): # _, (ax1, ax2) = plt.subplots(nrows=2, ncols=1) # res = self.gapdf.groupby('start_district_id')['gapdf'].sum() # ax1.bar(res.index, res.values) # res = self.gapdf.groupby('time_slotid')['gapdf'].sum() # ax2.bar(res.index.map(lambda x: x[11:]), res.values) # plt.show() # return
def show_weather_bydate(self): self.weathdf['gap'] = self.weathdf['time_slotid'].apply(self.find_gap_by_timeslot) by_date = self.weathdf.groupby('time_date') size = len(by_date) col_len = row_len = math.ceil(math.sqrt(size)) count = 1 for name, group in by_date: ax=plt.subplot(row_len, col_len, count) # temp = np.empty(group['time_id'].shape[0]) # temp.fill(2) # ax.plot(group['time_id'], group['gap']/group['gap'].max(), 'r', alpha=0.75) # ax.plot(group['time_id'], group['weather']/group['weather'].max()) ax.bar(group['time_id'], group['weather'], width=1) ax.set_title(name) count = count + 1 # plt.bar(group['time_id'], np.full(group['time_id'].shape[0], 5), width=1) plt.show() return
def main(): rh, gh, bh, bincen, feature_vec = color_hist(image, nbins=32, bins_range=(0, 256)) # Plot a figure with all three bar charts if rh is not None: fig = plt.figure(figsize=(12, 3)) plt.subplot(131) plt.bar(bincen, rh[0]) plt.xlim(0, 256) plt.title('R Histogram') plt.subplot(132) plt.bar(bincen, gh[0]) plt.xlim(0, 256) plt.title('G Histogram') plt.subplot(133) plt.bar(bincen, bh[0]) plt.xlim(0, 256) plt.title('B Histogram') fig.tight_layout() plt.show() else: print('Your function is returning None for at least one variable...')
def visualize_document_topic_probs(self, outfile): plots = [] height_cumulative = numpy.zeros(self.rows) #fig = pyplot.figure(figsize=(21, 10), dpi=550) for column in range(self.columns): color = pyplot.cm.coolwarm(column/self.columns, 1) if column == 0: p = pyplot.bar(self.ind, self.document_topics_raw[:, column], self.barwidth, color=color) else: p = pyplot.bar(self.ind, self.document_topics_raw[:, column], self.barwidth, bottom=height_cumulative, color=color) height_cumulative += self.document_topics_raw[:, column] plots.append(p) pyplot.ylim((0, 1)) pyplot.ylabel('Topics') pyplot.title('Topic distribution of CLS papers') pyplot.xticks(self.ind+self.barwidth/2, self.document_names, rotation='vertical', size = 10) pyplot.yticks(numpy.arange(0, 1, 10)) pyplot.legend([p[0] for p in plots], self.topic_labels, bbox_to_anchor=(1, 1)) self.fig.tight_layout() pyplot.savefig(outfile)
def plot_feature_importances(forest, patch_name_nfeatures, layer_name): importances = forest.feature_importances_ n_features = len(importances) plt.figure() plt.title("Feature importances (layer %s)" % str(layer_name)) bar_list = plt.bar(range(n_features), importances, color="r", align="center") if n_features < 50: plt.xticks(range(n_features), range(n_features)) plt.xlim([-1, n_features]) PATCH_COLORS = ["orangered", "orange", "green", "purple", "cyan", "blue", "red", "yellow"] bar_id = 0 patches = list() for i, (patch_name, n_bars) in enumerate(patch_name_nfeatures): patches.append(mpatches.Patch(color=PATCH_COLORS[i], label=patch_name)) for b in range(n_bars): bar_list[bar_id].set_color(PATCH_COLORS[i]) bar_id += 1 plt.legend(handles = patches)
def plotHist(self, vocabulary = None): print "Plotting histogram" if vocabulary is None: vocabulary = self.mega_histogram x_scalar = np.arange(self.n_clusters) y_scalar = np.array([abs(np.sum(vocabulary[:,h], dtype=np.int32)) for h in range(self.n_clusters)]) print y_scalar plt.bar(x_scalar, y_scalar) plt.xlabel("Visual Word Index") plt.ylabel("Frequency") plt.title("Complete Vocabulary Generated") plt.xticks(x_scalar + 0.4, x_scalar) plt.show()
def plot_gradients(self, foo=False): ''' Shows the difference between the computed gradients in the ANN modul and the numerically calculated gradients. ''' fig = plt.gcf() fig.canvas.set_window_title('Comparison of the computed gradients') numgrad, grad, qua, ok = ngc.compare_gradients(self.Net, self.inputdata_tr, self.outputdata_tr) print(qua, ok) y = numgrad-grad y2 = np.absolute(y) plt.bar(np.arange(1,len(y)+1), y) plt.grid(1) plt.xlabel('Gradient') plt.ylabel('Difference') plt.show() if foo: print('numgrad: ', numgrad) print('grad: ', grad) print('difference: ', y)
def plot_feature_importances(feature_importances, title, feature_names): # Normalize the importance values feature_importances = 100.0 * (feature_importances / max(feature_importances)) # Sort the values and flip them index_sorted = np.flipud(np.argsort(feature_importances)) # Arrange the X ticks pos = np.arange(index_sorted.shape[0]) + 0.5 # Plot the bar graph plt.figure() plt.bar(pos, feature_importances[index_sorted], align='center') plt.xticks(pos, feature_names[index_sorted]) plt.ylabel('Relative Importance') plt.title(title) plt.show()
def about_biographies_count(corpus): """ Finds how many items have/don't have a biography """ count = with_bio = characters = 0 for doc in load_scraped_items(corpus): count += 1 if doc.get('bio') and len(doc['bio']) > 5: with_bio += 1 characters += len(doc['bio']) print 'Total number of items:', count print 'Items with a biography %d (%.2f %%)' % (with_bio, 100. * with_bio / count) print 'Cumulative length of biographies: %d characters' % characters try: import matplotlib.pyplot as plt except ImportError: logger.warn('Cannot import matplotlib, skipping chart') return plt.bar([0, 1], [count - with_bio, with_bio], width=0.75) plt.xticks([0.375, 1.375], ['Without Biography', 'With Biography']) plt.grid(True, axis='y') plt.xlim((-0.5, 2.25)) plt.show()
def plot_Vavg(self,Vavg,Vbias,offset=None,axes=None): Iavg=self.ADU2I(Vavg,offset) lbl=str('V$_{bias}$ = %.2fV' % Vbias) plt.cla() if isinstance(axes,list) or isinstance(axes,np.ndarray): plt.axis(axes) plt.xlabel('TES number') plt.ylabel('I / $\mu$A') # plot markers with no lines plt.plot(Iavg,marker='D',drawstyle='steps-mid',linestyle='none',color='green',label=lbl) # plot bars up to the markers tes_axis=np.arange(self.NPIXELS)-0.25 plt.bar(tes_axis,height=Iavg,color='pink',width=0.5) plt.legend() plt.pause(0.01) return
def analyze(results): prompt = raw_input("[.] Type <c> to print or <p> to plot\n[>] ") if prompt.lower() == "c": for site, count in sites_count_sorted.items(): print site, count elif prompt.lower() == "p": plt.bar(range(len(results)), results.values(), align='edge') plt.xticks(rotation=45) plt.xticks(range(len(results)), results.keys()) plt.show() else: print "[.] Uh?" analyze (sites_count_sorted) #path to user's history database (Chrome)
def vis_bar_top(): evaluation_path = osp.join('output', 'evaluate_results') with open(osp.join(evaluation_path, 'imagenet_sl_results_pr_class.txt')) as f: scores = [] for line in f: c = {} words = line.strip().split(' ') words = [x.strip() for x in words] scores.append(float(words[1])) sort = sorted(scores, key=float) find_distribution_accuracy(sort) x = range(len(sort)) width = 1 plt.bar(x, sort, width) plt.show()
def plot(self, threshold=None, **kwargs): """Barplot of the MA coefficients.""" try: # Works under matplotlib 3. pyplot.bar(x=self.delays+.5, height=self.coefs, width=1., fill=False, **kwargs) except TypeError: # Works under matplotlib 2. pyplot.bar(left=self.delays+.5, height=self.coefs, width=1., fill=False, **kwargs) pyplot.xlabel('time') pyplot.ylabel('response') if threshold is not None: cumsum = numpy.cumsum(self.coefs) idx = numpy.where(cumsum > threshold*cumsum[-1])[0][0] pyplot.xlim(0., idx) idx, value = self.turningpoint pyplot.plot(idx, value, 'ro')
def plot(self, threshold=None, **kwargs): """Barplot of the ARMA response.""" try: # Works under matplotlib 3. pyplot.bar(x=self.ma.delays+.5, height=self.response, width=1., fill=False, **kwargs) except TypeError: # Works under matplotlib 2. pyplot.bar(left=self.ma.delays+.5, height=self.response, width=1., fill=False, **kwargs) pyplot.xlabel('time') pyplot.ylabel('response') if threshold is not None: cumsum = numpy.cumsum(self.response) idx = numpy.where(cumsum > threshold*cumsum[-1])[0][0] pyplot.xlim(0., idx)
def overlapped_bar(df, show=False, width=0.9, alpha=1.0, title='', xlabel='', ylabel='', **plot_kwargs): """Like a stacked bar chart except bars on top of each other with transparency""" xlabel = xlabel or df.index.name N = len(df) M = len(df.columns) indices = np.arange(N) colors = ['steelblue', 'firebrick', 'darksage', 'goldenrod', 'gray'] * int(M / 5. + 1) for i, label, color in zip(range(M), df.columns, colors): kwargs = plot_kwargs kwargs.update({'color': color, 'label': label}) plt.bar(indices, df[label], width=width, alpha=alpha if i else 1, **kwargs) plt.xticks(indices + .5 * width, ['{}'.format(idx) for idx in df.index.values]) plt.legend() plt.title(title) plt.xlabel(xlabel) if show: plt.show() return plt.gcf()
def testcase(self): #self.calc_open_day('603096') result=[] max_line=[] k=[] for i in self.codes: t,l=self.calc_open_day(i) if t is not None: result.append(t) max_line.append({i:l}) k.append(l) x=range(len(result)) #print x #print result plt.bar(x,result) plt.show() sum=0 for i in result: sum=sum+i avg=sum*1.00/len(result) print avg max_v=max(k) print max_v print max_line
def draw_dist_graph(clique_name, **kwargs): if not os.path.exists(kwargs['output_dir'] + clique_name + '.png'): try: doc_vector = kwargs['doc_vecs'][clique_name] except KeyError: return print('Drawing probability distribution graph for ' + clique_name) x_axis = [topic_id + 1 for topic_id, dist in enumerate(doc_vector)] y_axis = [dist for topic_id, dist in enumerate(doc_vector)] plt.bar(x_axis, y_axis, width=1, align='center', color='r') plt.xlabel('Topics') plt.ylabel('Probability') plt.title('Topic Distribution for clique') plt.xticks(np.arange(2, len(x_axis), 2), rotation='vertical', fontsize=7) plt.subplots_adjust(bottom=0.2) plt.ylim([0, np.max(y_axis) + .01]) plt.xlim([0, len(x_axis) + 1]) plt.savefig(kwargs['output_dir'] + clique_name) plt.close()
def draw_user_to_clique_graphs(distance_dir, dist_file): if not os.path.exists(distance_dir + dist_file + '.png'): print('Drawing community members distance from clique for: ' + dist_file) df = pd.read_csv(distance_dir + dist_file, sep='\t', header=None, names=['user', 'clique', 'distance']) x_axis = [str(row['user']) for idx, row in df.iterrows()] y_axis = [float(row['distance']) for idx, row in df.iterrows()] plt.figure(figsize=(20, 10)) plt.bar(np.arange(1, len(x_axis) + 1, 1), y_axis, width=1, align='center', color='r') plt.xlabel('Community Users') plt.ylabel('Divergence from Clique') plt.title('Distances from ' + dist_file + ' to the clique where grown from', fontsize=14, fontweight='bold') plt.xticks(np.arange(0, len(x_axis) + 1, 2), np.arange(0, len(x_axis) + 1, 2), rotation='vertical', fontsize=8) plt.subplots_adjust(bottom=0.2) plt.ylim([0, np.log(2) + .01]) plt.xlim([0, len(x_axis) + 1]) plt.savefig(distance_dir + dist_file) plt.close()
def community_size_distribution(): df = pd.read_csv('cliques', sep='\] ', engine='python', header=None) sizes = [len(ast.literal_eval(row[0])) for idx, row in df.iterrows()] df = pd.read_csv('communities', sep='\] ', engine='python', header=None) sizes += [len(ast.literal_eval(row[0])) for idx, row in df.iterrows()] x_axis = np.arange(0, max(sizes), 10) plt.bar(x_axis, bin_by_x_axis(sizes, x_axis), align='center', width=10, color='y') plt.xlabel('Community Size') plt.ylabel('Number of Communities') plt.title('Community Size Distribution') plt.xticks(x_axis, generate_x_ticks(x_axis), rotation='60', ha='right', fontsize='small') plt.xlim([-10, np.max(x_axis) + 10]) plt.tight_layout() plt.savefig('community_size_distribution') plt.close()
def plot_bars_two_sets(accs1, accs2, sets, labels, title, fignum, filename, auto_label=True): """ bar plot comparing two sets of results """ assert len(accs1) == len(accs2) and len(accs2) == len(labels), 'incompatible arguments in plot_bars_two_sets' plt.figure(fignum) ind = np.arange(len(labels)) width = 0.35 rects1 = plt.bar(ind, accs1, width, color='r', hatch='/', label=sets[0]) rects2 = plt.bar(ind + width, accs2, width, color='y', hatch='\\', label=sets[1]) plt.ylabel('Accuracy', size='large', fontweight='demibold') plt.title(title, fontweight='demibold') plt.xticks(ind + width, labels, size='large', fontweight='demibold') plt.legend(loc='upper left', prop={'size':12}) #plt.ylim([30,100]) if auto_label: autolabel(rects1) autolabel(rects2) #plt.show() plt.savefig(filename)
def plot_bars_two_sets_stacked(word_pos_accs, char_pos_accs, word_morph_accs, char_morph_accs, sets, labels, title, fignum, filename): """ bar plot comparing two sets of results """ assert len(word_pos_accs) == len(char_pos_accs) and len(char_pos_accs) == len(word_morph_accs) and len(word_morph_accs) == len(char_morph_accs), 'incompatible arguments in plot_bars_two_sets' plt.figure(fignum) ind = np.arange(len(labels)) width = 0.35 rects1 = plt.bar(ind, word_pos_accs, width, color='r', hatch='/', label=sets[0]) rects2 = plt.bar(ind, char_pos_accs-word_pos_accs, width, bottom=word_pos_accs, color='y', hatch='/', label=sets[1]) rects3 = plt.bar(ind + width, word_morph_accs, width, color='r', hatch='\\', label=sets[2]) rects4 = plt.bar(ind + width, char_morph_accs-word_morph_accs, width, bottom=word_morph_accs, color='y', hatch='\\', label=sets[3]) plt.ylabel('Accuracy', size='large', fontweight='demibold') plt.title(title, fontweight='demibold') plt.xticks(ind + width, labels, size='large', fontweight='demibold') plt.legend(loc='upper left', prop={'size':12}) #plt.ylim([30,100]) #plt.show() plt.savefig(filename)
def plot_bars_two_groups(group1, group2, groups, labels, title, fignum, filename): assert len(group1) == len(group2) and len(group2) == len(labels), 'incompatible arguments in plot_bars_two_groups' plt.figure(fignum) ind = np.arange(len(labels)) width = 1.0 rects1 = plt.bar(ind, group1, width, color='r', hatch='/', label=groups[0]) rects2 = plt.bar(ind+len(labels)+width, group2, width, color='y', hatch='\\', label=groups[1]) plt.ylabel('Change in Accuracy or BLEU', size='large', fontweight='demibold') plt.title(title, fontweight='demibold') ticks = np.concatenate((ind + width/2, len(labels) + ind + width + width/2)) #print ticks plt.xticks(ticks, labels + labels, size='large') plt.axhline(color='black') plt.legend() plt.tight_layout() #plt.show() plt.savefig(filename) #plot_bars_two_groups(word_diffs, char_diffs, ['Word', 'Char'], ['POS', 'Morph', 'BLEU'], 'Effect of Representation Layer', 3, 'layer-effect.png')
def plot_pdf_fit(self, label=None): import matplotlib.pyplot as plt from scipy.stats import exponweib, rayleigh from scipy import linspace, diff plt.bar(self.pdf[1][:len(self.pdf[0])], self.pdf[0], width=diff(self.pdf[1]), label=label, alpha=0.5, color='k') x = linspace(0, 50, 1000) plt.plot(x, exponweib.pdf(x, a=self.weibull_params[0], c=self.weibull_params[1], scale=self.weibull_params[3]), 'b--', label='Exponential Weibull pdf') plt.plot(x, rayleigh.pdf(x, scale=self.rayleigh_params[1]), 'r--', label='Rayleigh pdf') plt.title('Normalized distribution of wind speeds') plt.grid() plt.legend()
def my_colors(colornum): """ Returns a color that can be used in a plot. Define a number of colors that I would like to see in stacked bar graphs, lines, etc. """ mycolorlist=[ (1.0,0.4,0.4), (0.3,1.0,0.3), (0.3,0.3,1.0), (1.0,1.0,0.3), (0.4,0.9,1.0), (1.0,0.4,1.0), (0.5,0.8,0.5), (0.6,0.6,0.8)] num_colors=len(mycolorlist) return mycolorlist[(colornum%num_colors) - 1]
def plot_num_packets(self): """ Plot number of received packets for every mote. :return: """ motes = self.sort_by_motes() plt.figure() plt.bar([i for i in range(len(motes))], [len(mote) for mote in motes]) plt.xlabel('mote #') plt.ylabel('num packets received') plt.grid(True)
def distribution(labels, name): count = {} for label in labels: if (0 if label[0] == 10 else label[0]) in count: count[0 if label[0] == 10 else label[0]] += 1 else: count[0 if label[0] == 10 else label[0]] = 1 x = [] y = [] for k, v in count.items(): print(k, v) x.append(k) y.append(v) # draw x, y objects = x y_pos = np.arange(len(objects)) performance = y plt.bar(y_pos, performance, align='center', alpha=0.5) plt.xticks(y_pos, objects) plt.ylabel('Count') plt.title(name + ' Label Distribution') plt.show() ### todo: Try Gray-scale it!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
def feature_importances(X, forest_model): importances = forest_model.feature_importances_ indices = np.argsort(importances)[::-1] for feature in range(X.shape[1]): print("Feature: ", X.columns[feature]) print("Importance", importances[indices[feature]]) # Feature Importance Plot plt.figure() plt.title("Feature Importances") plt.bar(range(X.shape[1]), importances[indices], color='r', align='center') plt.xticks(range(X.shape[1]), indices) plt.xlim([-1, X.shape[1]]) plt.ylim([0, 1]) plt.show()
def draw_sub_graph(visit_total,domain_count,cname_count,ip_count,sub_graph_count): N = 5 ind = np.arange(1, N + 1) width = 0.7 plt.figure(1, figsize=(8, 6)) data = [domain_count,cname_count,ip_count,sub_graph_count,visit_total] plt.bar(ind, data, width, color='c', align='center') x_min, x_max = ind.min(), ind.max() plt.xlim(x_min - 1, x_max + 1) plt.ylabel('The numbers') plt.xlabel('Categories') plt.xticks(ind,('Domain','CNAME','IP','Sub_Graph','DNS Hits')) plt.yticks() # ??legend for a, b in zip(ind, data): plt.text(a, b, str(b)) plt.savefig('./graph/domain_overall.png', dpi=75) plt.show()
def plot_col_name(self, data, title): fig = plt.figure() fig.patch.set_facecolor('white') ax1 = fig.add_subplot(1, 1, 1) labels = [item[0] for item in data] values = [item[1] for item in data] index = np.arange(len(labels)) bar_width = 0.4 opacity = 0.4 rects = plt.bar(index, values, bar_width, alpha=opacity, color='b', label=title) autolabel(ax1, rects) plt.xticks(index + bar_width / 2, labels, rotation=-45) plt.legend() # ?? # plt.show() # ???images??? fpath = os.path.join(os.path.dirname(__file__), 'images', '%s.png' % title) plt.savefig(fpath)
def plot(self, clf, features): """ ?? feature importance :param clf: ???????? :param features: ??????? :return: """ features_len = len(features) importances = clf.feature_importances_ std = np.std([tree.feature_importances_ for tree in clf.estimators_], axis=0) indices = np.argsort(importances)[::-1] # ?? feature ranking print("\nFeature ranking:") for f in range(features_len): print("%d. %s (%f)" % (f + 1, features[indices[f]], importances[indices[f]])) # ?? feature importances ??? plt.figure() plt.title("Feature importances") plt.bar(range(features_len), importances[indices], color="r", yerr=std[indices], align="center") plt.xticks(range(features_len), np.array(features)[indices], rotation=-90) plt.xlim([-1, features_len]) plt.show()
def plotPerSampSepScore(perSampSepScore,fileName=None,tikzFileName=None,pklFileName=None): perSampSepScore = numpy.minimum(perSampSepScore,4.0) h,edges = numpy.histogram(perSampSepScore, bins=60) binWidth = edges[1]-edges[0] binCenters = (edges[:-1]+edges[1:])/2.0 barWidth = binWidth*0.9 fig = plt.figure() plt.bar(binCenters-barWidth/2,h,width=barWidth,log=True) if fileName is not None: if isinstance(fileName,list): for fn in fileName: plt.savefig(fn) else: plt.savefig(fileName) if tikzFileName is not None: tikz_save(tikzFileName, fig, figurewidth='\\figurewidth', figureheight='\\figureheight',show_info=False) if pklFileName is not None: with open(pklFileName,'wb') as f: data = {'perSampSepScore':perSampSepScore,'h':h,'edges':edges,'binWidth':binWidth,'binCenters':binCenters} cPickle.dump(data,f,protocol=cPickle.HIGHEST_PROTOCOL)
def correct_function(): # order is para-prim, para-comp, cheat-prim, cheat-comp, scenario-prim, scenario-comp SEMPRE = [85.04, 66.98, 77.5, 49.01, 60, 33] DEEP_SEMPRE = [95.23, 75.64, 50, 47.05, 42.85, 16.66] X = np.arange(3) width = (0.8-0.1)/4 s_p = [SEMPRE[0], SEMPRE[2], SEMPRE[4]] s_c = [SEMPRE[1], SEMPRE[3], SEMPRE[5]] d_p = [DEEP_SEMPRE[0], DEEP_SEMPRE[2], DEEP_SEMPRE[4]] d_c = [DEEP_SEMPRE[1], DEEP_SEMPRE[3], DEEP_SEMPRE[5]] plt.bar(X, s_p, width=width, color='#85c1e5') plt.bar(X+width, d_p, width=width, color='#254e7b') plt.bar(X+2*width+0.1, s_c, width=width, color='#85c1e5') plt.bar(X+3*width+0.1, d_c, width=width, color='#254e7b') width = (0.8-0.1)/4 plt.xticks(np.array([width, 3*width+0.1, 1+width, 1+3*width+0.1, 2+width, 2+3*width+0.1]), ["Prim.", "Comp.", "Prim.", "Comp.", "Prim.", "Comp."]) plt.text(0.4, -10, "Paraphrasing", ha='center', fontsize=18) plt.text(1.4, -10, "Scenarios", ha='center', fontsize=18) plt.text(2.4, -10, "Composition", ha='center', fontsize=18) plt.ylim(0, 100) plt.xlim(-0.1, 2.9) #plt.tight_layout() plt.legend(["SEMPRE", "Neural Net"], loc ="upper right") plt.savefig('./figures/correct-function.pdf')
def accuracy_against_sempre(): # order is para-prim, para-comp, cheat-prim, cheat-comp, scenario-prim, scenario-comp SEMPRE = [71.4, 50.2, 67.5, 33.3, 34.28, 30.5] DEEP_SEMPRE = [89.11, 55.27, 47.5, 29.4, 34.28, 16.66] X = np.arange(3) width = (0.8-0.1)/4 s_p = [SEMPRE[0], SEMPRE[2], SEMPRE[4]] s_c = [SEMPRE[1], SEMPRE[3], SEMPRE[5]] d_p = [DEEP_SEMPRE[0], DEEP_SEMPRE[2], DEEP_SEMPRE[4]] d_c = [DEEP_SEMPRE[1], DEEP_SEMPRE[3], DEEP_SEMPRE[5]] plt.bar(X, s_p, width=width, color='#85c1e5') plt.bar(X+width, d_p, width=width, color='#254e7b') plt.bar(X+2*width+0.1, s_c, width=width, color='#85c1e5') plt.bar(X+3*width+0.1, d_c, width=width, color='#254e7b') width = (0.8-0.1)/4 plt.xticks(np.array([width, 3*width+0.1, 1+width, 1+3*width+0.1, 2+width, 2+3*width+0.1]), ["Prim.", "Comp.", "Prim.", "Comp.", "Prim.", "Comp."]) plt.text(0.4, -10, "Paraphrasing", ha='center', fontsize=18) plt.text(1.4, -10, "Scenarios", ha='center', fontsize=18) plt.text(2.4, -10, "Composition", ha='center', fontsize=18) plt.ylim(0, 100) plt.xlim(-0.1, 2.9) #plt.tight_layout() plt.legend(["SEMPRE", "Neural Net"], loc ="upper right") plt.savefig('./figures/accuracy-combined.pdf')
def extensibility(): # order is new device acc, new device recall, new domain acc, new domain recall SEMPRE = [100 * 117./214., 100 * (10.+63.)/(15.+104.), 100 * (42.+232.)/(535.+75.), 100 * (32.+136.)/(286.+48.)] DEEP_SEMPRE = [38, 47, 55, 74] X = np.arange(2) width = (0.8-0.1)/4 s_a = [SEMPRE[0], SEMPRE[2]] s_r = [SEMPRE[1], SEMPRE[3]] d_a = [DEEP_SEMPRE[0], DEEP_SEMPRE[2]] d_r = [DEEP_SEMPRE[1], DEEP_SEMPRE[3]] plt.bar(X, s_a, width=width, color='#85c1e5') plt.bar(X+width, d_a, width=width, color='#254e7b') plt.bar(X+2*width+0.1, s_r, width=width, color='#85c1e5') plt.bar(X+3*width+0.1, d_r, width=width, color='#254e7b') width = (0.8-0.1)/4 plt.xticks(np.array([width, 3*width+0.1, 1+width, 1+3*width+0.1, 2+width, 2+3*width+0.1]), ["Accuracy", "Recall", "Accuracy", "Recall"]) plt.text(0.4, -10, "New Device", ha='center', fontsize=18) plt.text(1.4, -10, "New Domain", ha='center', fontsize=18) plt.ylim(0, 100) plt.xlim(-0.1, 1.9) #plt.tight_layout() plt.legend(["SEMPRE", "Neural Net"], loc ="upper right") plt.savefig('./figures/extensibility.pdf')
def recall(): # order is para-prim, para-comp, cheat-prim, cheat-comp, scenario-prim, scenario-comp SEMPRE = [81.06, 55.33, 65.38, 34.69, 40.0, 38.46] DEEP_SEMPRE = [93.75, 65.93, 60.0, 30.61, 58.33, 22.72] X = np.arange(3) width = (0.8-0.1)/4 s_p = [SEMPRE[0], SEMPRE[2], SEMPRE[4]] s_c = [SEMPRE[1], SEMPRE[3], SEMPRE[5]] d_p = [DEEP_SEMPRE[0], DEEP_SEMPRE[2], DEEP_SEMPRE[4]] d_c = [DEEP_SEMPRE[1], DEEP_SEMPRE[3], DEEP_SEMPRE[5]] plt.bar(X, s_p, width=width, color='#85c1e5') plt.bar(X+width, d_p, width=width, color='#254e7b') plt.bar(X+2*width+0.1, s_c, width=width, color='#85c1e5') plt.bar(X+3*width+0.1, d_c, width=width, color='#254e7b') width = (0.8-0.1)/4 plt.xticks(np.array([width, 3*width+0.1, 1+width, 1+3*width+0.1, 2+width, 2+3*width+0.1]), ["Prim.", "Comp.", "Prim.", "Comp.", "Prim.", "Comp."]) plt.text(0.4, -10, "Paraphrasing", ha='center', fontsize=18) plt.text(1.4, -10, "Scenarios", ha='center', fontsize=18) plt.text(2.4, -10, "Composition", ha='center', fontsize=18) plt.ylim(0, 100) plt.xlim(-0.1, 2.9) #plt.tight_layout() plt.legend(["SEMPRE", "Neural Net"], loc ="upper right") plt.savefig('./figures/recall.pdf')
def dataset_train(): # 0 param, 1 param, 2 param, 3+ param base = [1388, 1285, 977, 307] paraphrasing = [1185, 2277, 1471, 900] ifttt = [1525, 645, 414, 2607] generated = [569, 2098, 2723, 4610] data = np.array([base, paraphrasing, ifttt, generated]) p_0 = data[:,0] p_1 = data[:,1] p_2 = data[:,2] p_3 = data[:,3] width = 0.7 X = np.arange(4) plt.bar(X, p_3, width=width, color='#ffffff', bottom=p_0+p_1+p_2) plt.bar(X, p_2, width=width, color='#cde6f4', bottom=p_0+p_1) plt.bar(X, p_1, width=width, color='#85c1e5', bottom=p_0) plt.bar(X, p_0, width=width, color='#254e7b') plt.xticks(X + width/2, ["Base +\n Author", "Paraphrasing", "IFTTT", "Generated"]) plt.xlim(-0.3, 4) plt.ylim(0, 11000) plt.tight_layout() plt.legend(["3+ Params", "2 Params", "1 Param", "0 Params"], loc='upper left') plt.savefig('./figures/dataset-train.pdf')
def bar_plt(label, ys, block=False): global figures if label not in figures: figures[label] = plt.bar(np.arange(len(ys)), ys, align='center') plt.title(label) # plt.tight_layout() # plt.axis('off') inds, = np.where([key == label for key in figures.keys()]) for rect, y in zip(figures[label], ys): rect.set_height(y) plt.draw() plt.show(block=block)
def bin_stats(x, y, stat='median', nbins=360): import scipy.stats #bin_range = (x.min(), x.max()) bin_range = (0., 360.) bin_width = (bin_range[1] - bin_range[0]) / nbins print("Computing 1-degree bin statistics: %s" % stat) bin_stat, bin_edges, bin_number = scipy.stats.binned_statistic(x, y, \ statistic=stat, bins=nbins, range=bin_range) bin_centers = bin_edges[:-1] + bin_width/2. bin_stat = np.ma.masked_invalid(bin_stat) """ #Mask bins in grid directions, can potentially contain biased stats badbins = [0, 45, 90, 180, 225, 270, 315] bin_stat = np.ma.masked_where(np.around(bin_edges[:-1]) % 45 == 0, bin_stat) bin_edges = np.ma.masked_where(np.around(bin_edges[:-1]) % 45 == 0, bin_edges) """ #Generate plots if False: plt.figure() #Need to pull out original values for each bin #Loop through unique bin numbers, pull out original values into list of lists #plt.boxplot(bin_stat, sym='') plt.xlim(*bin_range) plt.xticks(np.arange(bin_range[0],bin_range[1],30)) plt.ylabel('dh/tan(slope) (m)') plt.xlabel('Aspect (1-deg bins)') plt.figure() plt.bar(bin_centers, bin_count) plt.xlim(*bin_range) plt.xticks(np.arange(bin_range[0],bin_range[1],30)) plt.ylabel('Count') plt.xlabel('Aspect (1-deg bins)') plt.show() return bin_stat, bin_edges, bin_centers #Function for fitting Nuth and Kaab (2011)
def generateImage(itemCountByValue,itemName="", is_type=True): if True==is_type: plt.bar(range(len(itemCountByValue.keys())),itemCountByValue.values(), tick_label=itemCountByValue.keys()) else: plt.bar([float(k) for k in itemCountByValue.keys()], itemCountByValue.values()) plt.savefig("%s%s.png" % (image_file_path, itemName))
def histogram(values, num_bins, bounds, normalized=True, plot=False, color='b'): """Generate a histogram plot. Parameters ---------- values : :obj:`numpy.ndarray` An array of values to put in the histogram. num_bins : int The number equal-width bins in the histogram. bounds : :obj:`tuple` of float Two floats - a min and a max - that define the lower and upper ranges of the histogram, respectively. normalized : bool If True, the bins will show the percentage of elements they contain rather than raw counts. plot : bool If True, this function uses pyplot to plot the histogram. color : :obj:`str` The color identifier for the plotted bins. Returns ------- :obj:`tuple of `:obj:`numpy.ndarray` The values of the histogram and the bin edges as ndarrays. """ hist, bins = np.histogram(values, bins=num_bins, range=bounds) width = (bins[1] - bins[0]) if normalized: if np.sum(hist) > 0: hist = hist.astype(np.float32) / np.sum(hist) if plot: plt.bar(bins[:-1], hist, width=width, color=color) return hist, bins
def dict_nb_value_per_key(dic, show_plot=False): """ For a given dictionary (dic), compute for each key, the number of values Return a dictionary """ d={} for key, value in dic.items(): d[key]=int(len(value)) if show_plot: plt.bar(list(d.keys()), d.values()) plt.show() return(d)
def Percentage_Of_Use(Time_Series): ''' This model creates a plot with the percentage of the time that each technologies is activate during the analized time. :param Time_series: The results of the optimization model that depend of the periods. ''' # Creation of the technolgy dictonary PercentageOfUse= {'Lost Load':0, 'Energy PV':0,'Curtailment':0, 'Energy Diesel':0, 'Discharge energy from the Battery':0, 'Charge energy to the Battery':0} # Count the quantity of times each technology has energy production for v in PercentageOfUse.keys(): foo = 0 for i in range(len(Time_Series)): if Time_Series[v][i]>0: foo = foo + 1 PercentageOfUse[v] = (round((foo/float(len(Time_Series))), 3))*100 # Create the names in the plot c = ['From Generator', 'Curtailment', 'To Battery', 'From PV', 'From Battery', 'Lost Load'] # Create the bar plot plt.figure() plt.bar((1,2,3,4,5,6), PercentageOfUse.values(), color= 'b', alpha=0.5, align='center') plt.xticks((1.2,2.2,3.2,4.2,5.2,6.2), c) # Put the names and position for the ticks in the x axis plt.xticks(rotation=-30) # Rotate the ticks plt.xlabel('Technology') # Create a label for the x axis plt.tick_params(axis='x', which='both', bottom='off', top='off', labelbottom='on') plt.ylabel('Percentage of use (%)') # Create a label for the y axis plt.savefig('Results/Percentge_of_Use.png', bbox_inches='tight') # Save the plot plt.show() return PercentageOfUse
def bar(x, y, xlabel, ylabel, name, dpi=96): n = range(len(x)) plt.bar(n, y) plt.ylabel(ylabel) plt.xlabel(xlabel) plt.xticks(n, x, rotation=90, size='x-small') plt.savefig(cache_path(name), dpi=dpi, bbox_inches='tight') plt.close()
def stacked_column(measurements, ind, ax, columndata, colors, width, offset): """Construct list of stacked columns that could be added into the graph.""" bottom = np.zeros(measurements) column = [] for elem, color in zip(columndata, colors): column.append(ax.bar(ind + offset, elem, width, bottom=bottom, color=color)) bottom += elem return column