我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用matplotlib.pylab.gcf()。
def save_images(self, X, imgfile, density=False): ax = plt.axes() x = X[:, 0] y = X[:, 1] if density: xy = np.vstack([x,y]) z = scipy.stats.gaussian_kde(xy)(xy) ax.scatter(x, y, c=z, marker='o', edgecolor='') else: ax.scatter(x, y, marker='o', c=range(x.shape[0]), cmap=plt.cm.coolwarm) if self.collection is not None: self.collection.set_transform(ax.transData) ax.add_collection(self.collection) ax.text(x[0], y[0], str('start'), transform=ax.transAxes) ax.axis([-0.2, 1.2, -0.2, 1.2]) fig = plt.gcf() plt.savefig(imgfile) plt.close()
def show_feature_importance(gbdt, feature_names=None): importance = gbdt.get_fscore(fmap='xgb.fmap') importance = sorted(importance.items(), key=operator.itemgetter(1)) df = pd.DataFrame(importance, columns=['feature', 'fscore']) df['fscore'] = df['fscore'] / df['fscore'].sum() print "feature importance", df if feature_names is not None: used_features = df['feature'] unused_features = [f for f in feature_names if f not in used_features] print "[IDF]Unused features:", str(unused_features) plt.figure() df.plot() df.plot(kind='barh', x='feature', y='fscore', legend=False, figsize=(6, 10)) plt.title('XGBoost Feature Importance') plt.xlabel('relative importance') plt.gcf().savefig('feature_importance_xgb.png')
def main(args): e = Eligibility(length=args.length) if args.mode == "dexp": e.efunc_ = e.efunc_double_exp elif args.mode == "rect": e.efunc_ = e.efunc_rect elif args.mode == "ramp": e.efunc_ = e.efunc_ramp elif args.mode == "exp": e.efunc_ = e.efunc_exp e.gen_efunc_table() x = np.arange(args.length) print x et = e.efunc(x) # plot and test with array argument cmstr = "ko" pl.plot(x, et, cmstr, lw=1.) if args.mode == "rect": # negative time for readability without lines pl.plot(np.arange(-5, x[0]), np.zeros(5,), cmstr, lw=1.) # pl.plot([-10, -1, x[0]], [0, 0, et[0]], cmstr, lw=1.) pl.plot([x[-1], x[0] + args.length], [et[-1], 0.], cmstr, lw=1.) pl.plot(x + args.length, np.zeros((len(et))), cmstr, lw=1.) pl.ylim((-0.005, np.max(et) * 1.1)) # pl.plot(x, et, "k-", lw=1.) # pl.yticks([]) # line at zero # pl.axhline(0., c="black") pl.xlabel("t [steps]") pl.ylabel("Eligibility") if args.plotsave: pl.gcf().set_size_inches((6, 2)) pl.gcf().savefig("eligibility_window.pdf", dpi=300, bbox_inches="tight") pl.show() # check perf: loop, test with single integer arguments import time now = time.time() for i in range(100): for j in range(args.length): e.efunc(j) print "table took:", time.time() - now now = time.time() for i in range(100): for j in range(args.length): e.efunc_(j) print "feval took:", time.time() - now