我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用matplotlib.pyplot.legend()。
def calc_auc(y_pred_proba, labels, exp_run_folder, classifier, fold): auc = roc_auc_score(labels, y_pred_proba) fpr, tpr, thresholds = roc_curve(labels, y_pred_proba) curve_roc = np.array([fpr, tpr]) dataile_id = open(exp_run_folder+'/data/roc_{}_{}.txt'.format(classifier, fold), 'w+') np.savetxt(dataile_id, curve_roc) dataile_id.close() plt.plot(fpr, tpr, label='ROC curve: AUC={0:0.2f}'.format(auc)) plt.xlabel('1-Specificity') plt.ylabel('Sensitivity') plt.ylim([0.0, 1.05]) plt.xlim([0.0, 1.0]) plt.grid(True) plt.title('ROC Fold {}'.format(fold)) plt.legend(loc="lower left") plt.savefig(exp_run_folder+'/data/roc_{}_{}.pdf'.format(classifier, fold), format='pdf') return auc
def plot_sent_trajectories(sents, decode_plot): font = {'family' : 'normal', 'size' : 14} matplotlib.rc('font', **font) i = 0 l = ["Portuguese","Catalan"] axes = plt.gca() #axes.set_xlim([xmin,xmax]) axes.set_ylim([-1,1]) for sent, enc in zip(sents, decode_plot): if i==2: continue i += 1 #times = np.arange(len(enc)) times = np.linspace(0,1,len(enc)) plt.plot(times, enc, label=l[i-1]) plt.title("Hidden Node Trajectories") plt.xlabel('timestep') plt.ylabel('trajectories') plt.legend(loc='best') plt.savefig("final_tests/cr_por_cat_hidden_cell_trajectories", bbox_inches="tight") plt.close()
def plot_ROC(test_labels, test_predictions): fpr, tpr, thresholds = metrics.roc_curve( test_labels, test_predictions, pos_label=1) auc = "%.2f" % metrics.auc(fpr, tpr) title = 'ROC Curve, AUC = '+str(auc) with plt.style.context(('ggplot')): fig, ax = plt.subplots() ax.plot(fpr, tpr, "#000099", label='ROC curve') ax.plot([0, 1], [0, 1], 'k--', label='Baseline') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.legend(loc='lower right') plt.title(title) return fig
def test(path_test, input_size, hidden_size, batch_size, save_dir, model_name, maxlen): db = read_data(path_test) X = create_sequences(db[:-maxlen], win_size=maxlen, step=maxlen) X = np.reshape(X, (X.shape[0], X.shape[1], input_size)) # build the model: 1 layer LSTM print('Build model...') model = Sequential() model.add(LSTM(hidden_size, return_sequences=False, input_shape=(maxlen, input_size))) model.add(Dense(maxlen)) model.load_weights(save_dir + model_name) model.compile(loss='mse', optimizer='adam') prediction = model.predict(X, batch_size, verbose=1) prediction = prediction.flatten() # prediction_container = np.array(prediction).flatten() Y = db[maxlen:] plt.plot(prediction, label='prediction') plt.plot(Y, label='true') plt.legend() plt.show()
def different_training_sets(): # base+author -> +paraphrasing -> +ifttt -> +generated train = [84.7, 93.2, 90.4, 91.99] test = [3.6, 37.4, 50.94, 55.4] train_recall = [66.6, 88.43, 92.63, 91.21] test_recall = [0.066, 49.05, 50.94, 75.47] #plt.newfigure() X = 1 + np.arange(4) plt.plot(X, train_recall, '--', color='#85c1e5') plt.plot(X, train, '-x', color='#6182a6') plt.plot(X, test_recall, '-o', color='#6182a6') plt.plot(X, test, '-', color='#052548') plt.ylim(0, 100) plt.xlim(0.5, 4.5) plt.xticks(X, ["Base + Author", "+ Paraphrasing", "+ IFTTT", "+ Generated"]) plt.tight_layout() plt.legend(["Train recall", "Train accuracy", "Test recall", "Test accuracy"], loc='lower right') plt.savefig('./figures/training-sets.pdf')
def plot_line_graph_multiple_lines(x, label_to_values, title, x_label, y_label): if not all(len(x) == len(values) for values in label_to_values.values()): raise ValueError('values of label_to_values must have length len(x)') colors = ['b','g','r','c','m','y','k'] line_styles = ['-','--',':'] for (i, label) in enumerate(sorted(label_to_values.keys())): color = colors[i%len(colors)] line_style = line_styles[(i//len(colors))%len(line_styles)] plt.plot(x, label_to_values[label], label=label, color=color, linestyle=line_style) plt.legend(loc='center left', bbox_to_anchor=(1,0.5), prop={'size':9}) plt.tight_layout(pad=9) plt.title(title) plt.xlabel(x_label) plt.ylabel(y_label) plt.show() # x_min, x_max for example proportion_initiated_by_user
def per_base_sequence_content_and_quality(fqbin, qualbin, outdir, figformat): fig, axs = plt.subplots(2, 2, sharex='col', sharey='row') lines = plot_nucleotide_diversity(axs[0, 0], fqbin) plot_nucleotide_diversity(axs[0, 1], fqbin, invert=True) l_Q = plot_qual(axs[1, 0], qualbin) plot_qual(axs[1, 1], qualbin, invert=True) plt.setp([a.get_xticklabels() for a in axs[0, :]], visible=False) plt.setp([a.get_yticklabels() for a in axs[:, 1]], visible=False) for ax in axs[:, 1]: ax.set_ylabel('', visible=False) for ax in axs[0, :]: ax.set_xlabel('', visible=False) # Since axes are shared I should only invert once. Twice will restore the original axis order! axs[0, 1].invert_xaxis() plt.suptitle("Per base sequence content and quality") axl = fig.add_axes([0.4, 0.4, 0.2, 0.2]) ax.plot() axl.axis('off') lines.append(l_Q) plt.legend(lines, ['A', 'T', 'G', 'C', 'Quality'], loc="center", ncol=5) plt.savefig(os.path.join(outdir, "PerBaseSequenceContentQuality." + figformat), format=figformat, dpi=500)
def create_graph(): logfile = 'result/log' xs = [] ys = [] ls = [] f = open(logfile, 'r') data = json.load(f) print(data) for d in data: xs.append(d["iteration"]) ys.append(d["main/accuracy"]) ls.append(d["main/loss"]) plt.clf() plt.cla() plt.hlines(1, 0, np.max(xs), colors='r', linestyles="dashed") # y=-1, 1?????? plt.title(r"loss/accuracy") plt.plot(xs, ys, label="accuracy") plt.plot(xs, ls, label="loss") plt.legend() plt.savefig("result/log.png")
def plot_traing_info(x, ylist, path): """ Loads log file and plot x and y values as provided by input. Saves as <path>/train_log.png """ file_name = os.path.join(path, __train_log_file_name) try: with open(file_name, "rb") as f: log = pickle.load(f) except IOError: # first time warnings.warn("There is no {} file here!!!".format(file_name)) return plt.figure() x_vals = log[x] for y in ylist: y_vals = log[y] if len(y_vals) != len(x_vals): warning.warn("One of y's: {} does not have the same length as x:{}".format(y, x)) plt.plot(x_vals, y_vals, label=y) # assert len(y_vals) == len(x_vals), "not the same len" plt.xlabel(x) plt.legend() #plt.show() plt.savefig(file_name[:-3]+'png', bbox_inches='tight') plt.close('all')
def plot_build_time_composition_graph(parseTimes, hashTimes, compileTimes, diffToBuildTime): # times in s fig, ax = plt.subplots() ax.stackplot(np.arange(1, len(parseTimes)+1), # x axis # [parseTimes, hashTimes, compileTimes, diffToBuildTime], [[i/60 for i in parseTimes], [i/60 for i in hashTimes], [i/60 for i in compileTimes], [i/60 for i in diffToBuildTime]], colors=[parseColor,hashColor,compileColor,remainColor], edgecolor='none') plt.xlim(1,len(parseTimes)) plt.xlabel('commits') plt.ylabel('time [min]') lgd = ax.legend([mpatches.Patch(color=remainColor), mpatches.Patch(color=compileColor), mpatches.Patch(color=hashColor), mpatches.Patch(color=parseColor)], ['remaining build time','compile time', 'hash time', 'parse time'], loc='center left', bbox_to_anchor=(1, 0.5)) fig.savefig(abs_path(BUILD_TIME_COMPOSITION_FILENAME), bbox_extra_artists=(lgd,), bbox_inches='tight') print_avg(parseTimes, 'parse') print_avg(hashTimes, 'hash') print_avg(compileTimes, 'compile') print_avg(diffToBuildTime, 'remainder')
def plotTimeMultiHistogram(parseTimes, hashTimes, compileTimes, filename): # times in ms bins = np.linspace(0, 5000, 50) data = np.vstack([parseTimes, hashTimes, compileTimes]).T fig, ax = plt.subplots() plt.hist(data, bins, alpha=0.7, label=['parsing', 'hashing', 'compiling'], color=[parseColor, hashColor, compileColor]) plt.legend(loc='upper right') plt.xlabel('time [ms]') plt.ylabel('#files') fig.savefig(filename) fig, ax = plt.subplots() boxplot_data = [[i/1000 for i in parseTimes], [i/1000 for i in hashTimes], [i/1000 for i in compileTimes]] # times to s plt.boxplot(boxplot_data, 0, 'rs', 0, [5, 95]) plt.xlabel('time [s]') plt.yticks([1, 2, 3], ['parsing', 'hashing', 'compiling']) #lgd = ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) # legend on the right fig.savefig(filename[:-4] + '_boxplots' + GRAPH_EXTENSION)
def plotChangesGraph(fileCounts, sameHashes, differentAstHashes, differentObjHashes): fig, ax = plt.subplots() #('#FFFF66','#FF0000','#3399FF','#008800') ax.plot(fileCounts, label='#objfiles', color='#EEAD0E')#'black') #ax.plot(sameHashes, label='unchanged')#, color='blue') ax.plot(differentAstHashes, label='astHash differs', color=compileColor)#'#000088') ax.plot(differentObjHashes, label='objHash differs', color=hashColor)#'#FFFF00') #'#0099FF') box = ax.get_position() lgd = ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) # legend on the right plt.xlabel('commits') plt.ylabel('#files') fig.savefig(abs_path(CHANGES_GRAPH_FILENAME), bbox_extra_artists=(lgd,), bbox_inches='tight') ################################################################################
def plot_ecdf(x, y, xlabel='attribute', legend='x'): """ Plot distribution ECDF x should be sorted, y typically from 1/len(x) to 1 TODO: function should be improved to plot multiple overlayed ecdfs """ plt.plot(x, y, marker='.', linestyle='none') # Make nice margins plt.margins(0.02) # Annotate the plot plt.legend((legend,), loc='lower right') _ = plt.xlabel(xlabel) _ = plt.ylabel('ECDF') # Display the plot plt.show()
def plot_gold(gold): #plt.xlim([0.2,1]) #plt.ylim([0.7,1]) x = [] y = [] for (wid,(sen, spe, n)) in gold.items(): if wid.startswith('S'): x.append(sen) y.append(spe) plt.scatter(x,y, c = 'r', marker = 'o', label = 'Novice') x = []; y = [] for (wid,(sen, spe, n)) in gold.items(): if wid.startswith('E'): x.append(sen) y.append(spe) plt.scatter(x,y, c = 'b', marker = 'x', label = 'Expert') plt.legend(loc = 'lower left') plt.xlabel("Sensitivity") plt.ylabel("Specificity")
def plotValResults(self, save_path=None, label=None): if label is not None: accs = self.training_val_results['acc'][label] aucs = self.training_val_results['auc'][label] else: accs = self.training_val_results['acc'] aucs = self.training_val_results['auc'] plt.figure() plt.plot([i * ACCURACY_LOGGED_EVERY_N_STEPS for i in range(len(accs))], accs) plt.plot([i * ACCURACY_LOGGED_EVERY_N_STEPS for i in range(len(aucs))], aucs) plt.xlabel('Training step') plt.ylabel('Validation accuracy') plt.legend(['Accuracy','AUC']) if save_path is None: plt.show() else: plt.savefig(save_path) plt.close()
def plotValResults(self, save_path=None, label=None): if label: accs = self.training_val_results_per_task['acc'][label] aucs = self.training_val_results_per_task['auc'][label] else: accs = self.training_val_results['acc'] aucs = self.training_val_results['auc'] plt.figure() plt.plot([i * self.accuracy_logged_every_n for i in range(len(accs))], accs) plt.plot([i * self.accuracy_logged_every_n for i in range(len(aucs))], aucs) plt.xlabel('Training step') plt.ylabel('Validation accuracy') plt.legend(['Accuracy','AUC']) if save_path is None: plt.show() else: plt.savefig(save_path)
def _plot_cmc(cmcs, colors, labels, title, fontsize=10, position=None): if position is None: position = 'lower right' # open new page for current plot figure = pyplot.figure() max_R = 0 # plot the CMC curves for i in range(len(cmcs)): probs = bob.measure.cmc(cmcs[i]) R = len(probs) pyplot.semilogx(range(1, R+1), probs, figure=figure, color=colors[i], label=labels[i]) max_R = max(R, max_R) # change axes accordingly ticks = [int(t) for t in pyplot.xticks()[0]] pyplot.xlabel('Rank') pyplot.ylabel('Probability') pyplot.xticks(ticks, [str(t) for t in ticks]) pyplot.axis([0, max_R, -0.01, 1.01]) pyplot.legend(loc=position, prop = {'size':fontsize}) pyplot.title(title) return figure
def _plot_epc(scores_dev, scores_eval, colors, labels, title, fontsize=10, position=None): if position is None: position = 'upper center' # open new page for current plot figure = pyplot.figure() # plot the DET curves for i in range(len(scores_dev)): x,y = bob.measure.epc(scores_dev[i][0], scores_dev[i][1], scores_eval[i][0], scores_eval[i][1], 100) pyplot.plot(x, y, color=colors[i], label=labels[i]) # change axes accordingly pyplot.xlabel('alpha') pyplot.ylabel('HTER') pyplot.title(title) pyplot.axis([-0.01, 1.01, -0.01, 0.51]) pyplot.grid(True) pyplot.legend(loc=position, prop = {'size':fontsize}) pyplot.title(title) return figure
def plot_feature_importances(feature_names, feature_importances, N=30): importances = list(zip(feature_names, list(feature_importances))) importances = pd.DataFrame(importances, columns=["Feature", "Importance"]) importances = importances.set_index("Feature") # Sort by the absolute value of the importance of the feature importances["sort"] = abs(importances["Importance"]) importances = importances.sort(columns="sort", ascending=False).drop("sort", axis=1) importances = importances[0:N] # Show the most important positive feature at the top of the graph importances = importances.sort(columns="Importance", ascending=True) with plt.style.context(('ggplot')): fig, ax = plt.subplots(figsize=(16,12)) ax.tick_params(labelsize=16) importances.plot(kind="barh", legend=False, ax=ax) ax.set_frame_on(False) ax.set_xlabel("Relative importance", fontsize=20) ax.set_ylabel("Feature name", fontsize=20) plt.tight_layout() plt.title("Most important features for attack", fontsize=20).set_position([.5, 0.99]) return fig
def tsne_cluster_cuisine(df,sublist): lenlist=[0] df_sub = df[df['cuisine']==sublist[0]] lenlist.append(df_sub.shape[0]) for cuisine in sublist[1:]: temp = df[df['cuisine']==cuisine] df_sub = pd.concat([df_sub, temp],axis=0,ignore_index=True) lenlist.append(df_sub.shape[0]) df_X = df_sub.drop(['cuisine','recipeName'],axis=1) print df_X.shape, lenlist dist = squareform(pdist(df_X, metric='cosine')) tsne = TSNE(metric='precomputed').fit_transform(dist) palette = sns.color_palette("hls", len(sublist)) plt.figure(figsize=(10,10)) for i,cuisine in enumerate(sublist): plt.scatter(tsne[lenlist[i]:lenlist[i+1],0],\ tsne[lenlist[i]:lenlist[i+1],1],c=palette[i],label=sublist[i]) plt.legend() #interactive plot with boken; set up for four categories, with color palette; pass in df for either ingredient or flavor
def plot_mean_debye(sol, ax): x = np.log10(sol[0]["data"]["tau"]) x = np.linspace(min(x), max(x),100) list_best_rtd = [100*np.sum([a*(x**i) for (i, a) in enumerate(s["params"]["a"])], axis=0) for s in sol] # list_best_rtd = [s["fit"]["best"] for s in sol] y = np.mean(list_best_rtd, axis=0) y_min = 100*np.sum([a*(x**i) for (i, a) in enumerate(sol[0]["params"]["a"] - sol[0]["params"]["a_std"])], axis=0) y_max = 100*np.sum([a*(x**i) for (i, a) in enumerate(sol[0]["params"]["a"] + sol[0]["params"]["a_std"])], axis=0) ax.errorbar(10**x[(x>-6)&(x<2)], y[(x>-6)&(x<2)], None, None, "-", color='blue',linewidth=2, label="Mean RTD", zorder=10) plt.plot(10**x[(x>-6)&(x<2)], y_min[(x>-6)&(x<2)], color='lightgray', alpha=1, zorder=-1, label="RTD range") plt.plot(10**x[(x>-6)&(x<2)], y_max[(x>-6)&(x<2)], color='lightgray', alpha=1, zorder=-1) plt.fill_between(sol[0]["data"]["tau"], 100*(sol[0]["params"]["m_"]-sol[0]["params"]["m__std"]) , 100*(sol[0]["params"]["m_"]+sol[0]["params"]["m__std"]), color='lightgray', alpha=1, zorder=-1, label="RTD SD") ax.set_xlabel("Relaxation time (s)", fontsize=14) ax.set_ylabel("Chargeability (%)", fontsize=14) plt.yticks(fontsize=14), plt.xticks(fontsize=14) plt.xscale("log") ax.set_xlim([1e-6, 1e1]) ax.set_ylim([0, 5.0]) ax.legend(loc=1, fontsize=12) # ax.set_title(title+" step method", fontsize=14)
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_info_retrieval(precisions, save_file): # markers = ["|", "D", "8", "v", "^", ">", "h", "H", "s", "*", "p", "d", "<"] markers = ["D", "p", 's', "*", "d", "8", "^", "H", "v", ">", "<", "h", "|"] ticks = zip(*zip(*precisions)[1][0])[0] plt.xticks(range(len(ticks)), ticks) new_x = interpolate.interp1d(ticks, range(len(ticks)))(ticks) i = 0 for model_name, val in precisions: fr, pr = zip(*val) plt.plot(new_x, pr, linestyle='-', alpha=0.7, marker=markers[i], markersize=8, label=model_name) i += 1 # plt.legend(model_name) plt.xlabel('Fraction of Retrieved Documents') plt.ylabel('Precision') legend = plt.legend(loc='upper right', shadow=True) plt.savefig(save_file) plt.show()
def plot_info_retrieval_by_length(precisions, save_file): markers = ["o", "v", "8", "s", "p", "*", "h", "H", "^", "x", "D"] ticks = zip(*zip(*precisions)[1][0])[0] plt.xticks(range(len(ticks)), ticks) new_x = interpolate.interp1d(ticks, range(len(ticks)))(ticks) i = 0 for model_name, val in precisions: fr, pr = zip(*val) plt.plot(new_x, pr, linestyle='-', alpha=0.6, marker=markers[i], markersize=6, label=model_name) i += 1 # plt.legend(model_name) plt.xlabel('Document Sorted by Length') plt.ylabel('Precision (%)') legend = plt.legend(loc='upper right', shadow=True) plt.savefig(save_file) plt.show()
def draw_results(self): metrics_log, cost_log = {}, {} for key, value in sorted(self._logs.items()): metrics_log[key], cost_log[key] = value[:-1], value[-1] for i, name in enumerate(sorted(self._metric_names)): plt.figure() plt.title("Metric Type: {}".format(name)) for key, log in sorted(metrics_log.items()): xs = np.arange(len(log[i])) + 1 plt.plot(xs, log[i], label="Data Type: {}".format(key)) plt.legend(loc=4) plt.show() plt.close() plt.figure() plt.title("Cost") for key, loss in sorted(cost_log.items()): xs = np.arange(len(loss)) + 1 plt.plot(xs, loss, label="Data Type: {}".format(key)) plt.legend() plt.show()
def main(plot=True, env_name="Taxi-v2", test_init_state=77): print("start training") n_sarsa2 = NstepSarsa(env_name, num_episodes=50000, n_steps=2, epsilon=0.1) n_sarsa3 = NstepSarsa(env_name, num_episodes=50000, n_steps=3, epsilon=0.1) n_sarsa4 = NstepSarsa(env_name, num_episodes=50000, n_steps=4, epsilon=0.1) # training n_sarsa2() n_sarsa3() n_sarsa4() print("testing") n_sarsa2.test(test_init_state) n_sarsa3.test(test_init_state) n_sarsa4.test(test_init_state) if plot: import matplotlib.pyplot as plt plt.plot(n_sarsa2.rewards, label="n_steps=2", alpha=0.5) plt.plot(n_sarsa3.rewards, label="n_steps=3", alpha=0.5) plt.plot(n_sarsa4.rewards, label="n_steps=4", alpha=0.5) plt.legend() plt.show()
def main(plot=True, env_name="Taxi-v2", test_init_state=77): print("start training") sarsa9 = Sarsa(env_name, alpha=0.9) sarsa5 = Sarsa(env_name, alpha=0.5) sarsa1 = Sarsa(env_name, alpha=0.1) # training sarsa9() sarsa5() sarsa1() print("testing") print("gamma=0.9") sarsa9.test(test_init_state) print("gamma=0.5") sarsa5.test(test_init_state) print("gamma=0.1") sarsa1.test(test_init_state) if plot: plt.plot(sarsa1.rewards, label="alpha=0.1", alpha=0.5) plt.plot(sarsa5.rewards, label="alpha=0.5", alpha=0.5) plt.plot(sarsa9.rewards, label="alpha=0.9", alpha=0.5) plt.legend() plt.show()
def main(plot=True, env_name="Taxi-v2", test_init_state=77): print("start training") ql9 = QLearing(env_name, alpha=0.9) ql5 = QLearing(env_name, alpha=0.5) ql1 = QLearing(env_name, alpha=0.1) # training ql9() ql5() ql1() ql9.test(test_init_state) ql5.test(test_init_state) ql1.test(test_init_state) if plot: import matplotlib.pyplot as plt plt.plot(ql1.rewards, label="alpha=0.1", alpha=0.5) plt.plot(ql5.rewards, label="alpha=0.5", alpha=0.5) plt.plot(ql9.rewards, label="alpha=0.9", alpha=0.5) plt.legend() plt.show()
def plot_cdf_model_and_meansh(self, cdfs, tag, cdf0_1s, aucs, bx, dx): plt.close("all") x = np.arange(0, bx, dx) fig, ax = plt.subplots(nrows=1, ncols=1) ax.plot(x, cdfs[0], label="CDF model") ax.plot(x, cdfs[1], label="CDF mean shape") ax.grid(True) plt.xlabel("NRMSE") plt.ylabel("Data proportion") plt.legend(loc=4, prop={'size': 8}, fancybox=True, shadow=True) plt.title( "CDF curve: " + tag + ". Model: CDF0.1: " + str(prec2 % cdf0_1s[0]) + " . AUC:" + str(prec2 % aucs[0]) + ".\n" + ". MSh: CDF0.1: " + str(prec2 % cdf0_1s[1]) + " . AUC:" + str(prec2 % aucs[1]) + ".\n") return fig
def plot_errors(self, valid, train, path, epoc=-1): '''Plot then save the figure of the error over the valid and train sets calculated during the gradient descent. ***** CLASSIFICATION The figure won't be displayed. valid: list of errors over the validation set train: list of errors over the train set path: path where to save the figure. ''' fig = plt.figure() train_gp, = plt.plot(train, '-r') valid_gp, = plt.plot(valid, '-*g') if epoc >= 0: epoc = epoc - 1 # ploting starts from 0 stop, = plt.plot([epoc, epoc], [0, max(valid + train) + 5], '--b', lw=2) plt.legend([train_gp, valid_gp, stop], ['train error', 'valid error', 'stop learning, epoch='+str(epoc + 1)], fancybox=True, shadow=True) else: plt.legend([train_gp, valid_gp], ['train error', 'valid error'], fancybox=True, shadow=True) plt.title('Train/valid error during the gradient descent') plt.xlabel(u"n° epoch") plt.ylabel('Error (100 - accuracy) %') fig.savefig(path, bbox_inches='tight') # to display the figure #plt.show()
def algo_specific_fit(self, printTopN): # print Feature Importance Scores table self.feature_imp = pd.Series( self.alg.feature_importances_, index=self.predictors ).sort_values(ascending=False) self.plot_feature_importance(printTopN) self.model_output['Feature_Importance'] = \ self.feature_imp.to_string() #Plot OOB estimates if subsample <1: if self.model_output['subsample']<1: plt.xlabel("GBM Iteration") plt.ylabel("Score") plt.plot( range(1, self.model_output['n_estimators']+1), self.alg.oob_improvement_ ) plt.legend(['oob_improvement_','train_score_'], loc='upper left') plt.show(block=False)
def plot_word_dist(info, words, start_year, end_year, one_minus=False, legend_loc='upper left'): colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k'] plot_info = {} for word in words: plot_info[word] = info[word] for title, data_dict in plot_info.iteritems(): x = []; y = [] for year, val in data_dict.iteritems(): if year >= start_year and year <= end_year: x.append(year) if one_minus: val = 1 - val y.append(val) color = colors.pop() plt.plot(x, smooth(np.array(y)), color=color) plt.scatter(x, y, marker='.', color=color) plt.legend(plot_info.keys(), loc=legend_loc) return plt
def plot_word_basic(info, words, start_year, end_year, datatype): colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k'] plot_info = {} for word in words: plot_info[word] = info[word] for title, data_dict in plot_info.iteritems(): x = []; y = [] for year, val in data_dict[datatype].iteritems(): if year >= start_year and year <= end_year: x.append(year) y.append(val) color = colors.pop() plt.plot(x, smooth(np.array(y)), color=color) plt.scatter(x, y, marker='.', color=color) plt.legend(plot_info.keys()) plt.show()
def plot_slice_xy(self, fig=None, ax=None): ''' Creates a plot of slices through the X and Y axes of the pupil Args: fig (pyplot.figure): Figure to draw plot in ax (pyplot.axis): Axis to draw plot in Returns: (pyplot.figure, pyplot.axis): Figure and axis containing the plot ''' u, x = self.slice_x _, y = self.slice_y fig, ax = share_fig_ax(fig, ax) x = convert_phase(x, self) y = convert_phase(y, self) ax.plot(u, x, lw=3, label='Slice X') ax.plot(u, y, lw=3, label='Slice Y') ax.set(xlabel=r'Pupil $\rho$ [mm]', ylabel=f'OPD [{self._opd_str}]') plt.legend() return fig, ax
def paramagg(data): ''' USE: paramagg(df) Provides an overview in one plot for a parameter scan. Useful to understand rough distribution of accuracacy and loss for both test and train. data = a pandas dataframe from hyperscan() ''' plt.figure(num=None, figsize=(8, 8), dpi=80, facecolor='w', edgecolor='k') plt.scatter(data.train_loss, data.train_acc, label='train') plt.scatter(data.test_loss, data.test_acc, label='test') plt.legend(loc='upper right') plt.tick_params(axis='both', which='major', pad=15) plt.xlabel('loss', fontsize=18, labelpad=15, color="gray") plt.ylabel('accuracy', fontsize=18, labelpad=15, color="gray") plt.show()
def plot_trace(n=0, lg=False): plt.plot(trueC[n], c=col[2], clip_on=False, zorder=5, label='Truth') plt.plot(solution, c=col[0], clip_on=False, zorder=7, label='Estimate') plt.plot(y, c=col[7], alpha=.7, lw=1, clip_on=False, zorder=-10, label='Data') if lg: plt.legend(frameon=False, ncol=3, loc=(.1, .62), columnspacing=.8) spks = np.append(0, solution[1:] - g * solution[:-1]) plt.text(800, 2.2, 'Correlation: %.3f' % (np.corrcoef(trueSpikes[n], spks)[0, 1]), size=24) plt.gca().set_xticklabels([]) simpleaxis(plt.gca()) plt.ylim(0, 2.85) plt.xlim(0, 1500) plt.yticks([0, 2], [0, 2]) plt.xticks([300, 600, 900, 1200], ['', '']) # init params
def make_plot(counts): """ Plot the counts for the positive and negative words for each timestep. Use plt.show() so that the plot will popup. """ positive = [] negative = [] for count in counts: for word in count: if word[0] == "positive": positive.append(word[1]) else: negative.append(word[1]) plt.axis([-1, len(positive), 0, max(max(positive),max(negative))+100]) pos, = plt.plot(positive, 'b-', marker = 'o', markersize = 10) neg, = plt.plot(negative, 'g-', marker = 'o', markersize = 10) plt.legend((pos,neg),('Positive','Negative'),loc=2) plt.xticks(np.arange(0, len(positive), 1)) plt.xlabel("Time Step") plt.ylabel("Word Count") plt.show()
def _plot_old_pred_data(old_pred_data, show_pred_plot, save_pred_plot, show_clarke_plot, save_clarke_plot, id_str, algorithm_str, minutes_str): actual_bg_array = old_pred_data.result_actual_bg_array actual_bg_time_array = old_pred_data.result_actual_bg_time_array pred_array = old_pred_data.result_pred_array pred_time_array = old_pred_data.result_pred_time_array #Root mean squared error rms = math.sqrt(metrics.mean_squared_error(actual_bg_array, pred_array)) print " Root Mean Squared Error: " + str(rms) print " Mean Absolute Error: " + str(metrics.mean_absolute_error(actual_bg_array, pred_array)) print " R^2 Coefficient of Determination: " + str(metrics.r2_score(actual_bg_array, pred_array)) plot, zone = ClarkeErrorGrid.clarke_error_grid(actual_bg_array, pred_array, id_str + " " + algorithm_str + " " + minutes_str) print " Percent A:{}".format(float(zone[0]) / (zone[0] + zone[1] + zone[2] + zone[3] + zone[4])) print " Percent C, D, E:{}".format(float(zone[2] + zone[3] + zone[4])/ (zone[0] + zone[1] + zone[2] + zone[3] + zone[4])) print " Zones are A:{}, B:{}, C:{}, D:{}, E:{}\n".format(zone[0],zone[1],zone[2],zone[3],zone[4]) if save_clarke_plot: plt.savefig(id_str + algorithm_str.replace(" ", "") + minutes_str + "clarke.png") if show_clarke_plot: plot.show() plt.clf() plt.plot(actual_bg_time_array, actual_bg_array, label="Actual BG", color='black', linestyle='-') plt.plot(pred_time_array, pred_array, label="BG Prediction", color='black', linestyle=':') plt.title(id_str + " " + algorithm_str + " " + minutes_str + " BG Analysis") plt.ylabel("Blood Glucose Level (mg/dl)") plt.xlabel("Time (minutes)") plt.legend(loc='upper left') # SHOW/SAVE PLOT DEPENDING ON THE BOOLEAN PARAMETER if save_pred_plot: plt.savefig(id_str + algorithm_str.replace(" ","") + minutes_str + "plot.png") if show_pred_plot: plt.show() #Function to analyze the old OpenAPS data
def plot_joints_step(self, stamp): if self.plots == '': return mean_joints = self.get_mean_joints() std_joints = self.get_std_joints() f = plt.figure(facecolor="white", figsize=(16, 12)) ax = f.add_subplot(111) ax.set_title('Mean +- {}std'.format(self.std_factor)) color_id = 0 for joint_id, joint_mean in enumerate(mean_joints): ax.plot(self.x, joint_mean, label='Joint {}'.format(joint_id), color=self.colors[color_id], linestyle='dashed') plt.fill_between(self.x, joint_mean - self.std_factor*std_joints[joint_id], joint_mean + self.std_factor*std_joints[joint_id], alpha=0.1, color=self.colors[color_id]) color_id = (color_id + 1) % len(self.colors) plt.legend(loc='upper left') self._mk_dirs() filename = '_'.join(['joints', stamp]) plt.savefig(join(self.plots, filename) + '.svg', dpi=100, transparent=False) plt.close('all')
def plot_demos(self): if self.plots == '': return yt = self.Y.transpose(2, 0, 1) for joint_id, joint in enumerate(yt): f = plt.figure(facecolor="white", figsize=(16, 12)) ax = f.add_subplot(111) ax.set_title('Joint {}'.format(joint_id)) for demo_id, demo in enumerate(joint): ax.plot(self.x, demo, label='Demo {}'.format(demo_id)) plt.legend() # Save or show plots self._mk_dirs() filename = 'demos_of_joint_{}'.format(joint_id) plt.savefig(join(self.plots, filename) + '.svg', dpi=100, transparent=False) plt.close('all')
def plot_segmented_hist(arr_middle, arr_tails): ''' Function to plot a histogram of scores, color coded tails to visualize sections of employers to be analyzed INPUT: arr_middle: Array-like, scores of employers not being analyzed (middle 90%) arr_tails: Array-like, scores of employers to be analyzed (>95%, <5%) OUTPUT: Histogram plot (saved in directory) ''' fig = plt.figure(figsize=(6, 4)) ax = fig.add_subplot(111) ax.set_title('Employers with Significant Scores', fontsize=14) ax.set_xlabel('Overall Score', fontsize=10) ax.set_ylabel('Observations', fontsize=10) ax.hist(arr_middle, bins=34, label='Middle 90%') ax.hist(arr_tails, bins=34, label='Outer 5% Tails') plt.legend(loc='best', fontsize=10) plt.tight_layout() plt.savefig('images/sig_scores.png')
def plot_learning_curve(_, history, folder, debug=True): arr = np.asarray( map(lambda k: [k['epoch'], k['train_loss'], k['valid_loss']], history)) plt.figure() plt.plot(arr[:, 0], arr[:, 1], 'r', marker='o', label='Training loss', linewidth=2.0) plt.plot(arr[:, 0], arr[:, 2], 'b', marker='o', label='Validation loss', linewidth=2.0) plt.xlabel('Epochs') plt.ylabel('Loss') plt.ylim([0.0, np.max(arr[:, 1:]) * 1.3]) plt.title('Learning curve') plt.legend() if not debug: plt.savefig('%s/learning_curve.png' % folder, bbox_inches='tight') plt.close()
def display_pr_curve(precision, recall): # following examples from sklearn # TODO: f1 operating point import pylab as plt # Plot Precision-Recall curve plt.clf() plt.plot(recall, precision, label='Precision-Recall curve') plt.xlabel('Recall') plt.ylabel('Precision') plt.ylim([0.0, 1.05]) plt.xlim([0.0, 1.0]) plt.title('Precision-Recall example: Max f1={0:0.2f}'.format(max_f1)) plt.legend(loc="lower left") plt.show()
def run(plotIt=True): M = discretize.TreeMesh([8, 8]) def refine(cell): xyz = cell.center dist = ((xyz - [0.25, 0.25])**2).sum()**0.5 if dist < 0.25: return 3 return 2 M.refine(refine) M.number() if plotIt: M.plotGrid(nodes=True, cells=True, facesX=True) plt.legend(( 'Grid', 'Cell Centers', 'Nodes', 'Hanging Nodes', 'X faces', 'Hanging X faces' ))
def plot_perstate(data, hidden_states): ''' Make, for each state, a plot of the data Parameters ---------- data : pandas DataFrame Data to plot hidden_states: iteretable the hidden states corresponding to the timesteps ''' num_states = max(hidden_states) + 1 fig, axs = plt.subplots( num_states, sharex=True, sharey=True, figsize=(15, 15)) colours = plt.cm.rainbow(np.linspace(0, 1, num_states)) for i, (ax, colour) in enumerate(zip(axs, colours)): # Use fancy indexing to plot data in each state. data_to_plot = data.copy() data_to_plot[hidden_states != i] = 0 data_to_plot.plot(ax=ax, legend=False) ax.set_title("{0}th hidden state".format(i)) ax.grid(True) plt.legend(bbox_to_anchor=(0, -1, 1, 1), loc='lower center') plt.show()
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 pd_show_with_entities(self, x_label, y_label, title_msg, ax, fig, plt, legend_list=[], show_plot=True, debug=False): plt.xlabel(x_label) plt.ylabel(y_label) ax.set_title(title_msg) if len(legend_list) == 0: ax.legend(loc="best", prop={"size":"medium"}) else: ax.legend(legend_list, loc="best", prop={"size": "medium"}) self.pd_add_footnote(fig) plt.tight_layout() if show_plot: plt.show() else: plt.plot() # end of pd_show_with_entities
def plot_surf_element(self,element='Ba'): ''' Plots surface evolution of certain element ''' for i in range(len(self.runs_H5_surf)): sefiles=se(self.runs_H5_surf[i]) y=[] mass=sefiles.get("mini") z=sefiles.get("zini") legend=str(mass)+"M$_{\odot}$ Z= "+str(z) for j in arange(0,len(sefiles.se.cycles)-1000,500): y.append(sefiles.get(j,"elem_massf",element)) plt.plot(arange(0,len(sefiles.se.cycles)-1000,500),y,label=legend) plt.xlabel('Cycles') plt.ylabel('X('+element+')')
def plot_surf_isotope(self,isotope='C-12'): ''' Plots surface evolution of certain isotope ''' for i in range(len(self.runs_H5_surf)): sefiles=se(self.runs_H5_surf[i]) y=[] mass=sefiles.get("mini") z=sefiles.get("zini") legend=str(mass)+"M$_{\odot}$ Z= "+str(z) for j in arange(0,len(sefiles.se.cycles)-1000,500): y.append(sefiles.get(j,"iso_massf",isotope)) plt.plot(arange(0,len(sefiles.se.cycles)-1000,500),y,label=legend) plt.xlabel('Cycles') plt.ylabel('X$_{'+isotope+'}$')